【问题标题】:How do I make a PowerShell profile if my profile path contains an apostrophe?如果我的配置文件路径包含撇号,如何制作 PowerShell 配置文件?
【发布时间】:2010-12-21 16:01:18
【问题描述】:

我正在尝试设置 PowerShell 配置文件,以便我的所有机器都有一个通用配置文件。我正在让每台机器的配置文件在我的 Dropbox 中运行一个脚本,这样我就可以更轻松地更新它们。

我认为问题在于我不太了解点源语法,但事实证明 PowerShell 真的不喜欢我的文档文件夹的路径带有撇号。完整路径为:

d:\Owen's Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

如果我删除该文件,PowerShell 会正常启动(当然,没有我想要的修改。)如果我创建该文件,PowerShell 启动时会出现错误:

The string starting:
At line:1 char:75
+ . 'D:\Owen`'s Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 <<<< '
is missing the terminator: '.
At line:1 char:76
+ . 'D:\Owen`'s Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1' <<<<
    + CategoryInfo          : ParserError: (:String) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

这不是我的错,因为脚本本身是空的。删除空文件会使它消失,创建新文件会使错误再次出现。 Microsoft 肯定预期带有撇号的配置文件路径吗?

更新 粗鲁。 Looks like an old issue. 也许没有解决方法。 :(

更新 2 我在 Twitter 上与 @Lee_Holmes 讨论过这个问题。显然,它已在一些超级棒的内部 Microsoft 版本中得到修复,但由于 PSH 现在是 Windows 组件,因此没有可用的修补程序和修复时间表。这使得 Roman Kuzmin 的答案低于最合适的答案(使用全局配置文件之一),或者可能是尚未提及的“重命名您的我的文档文件夹”。

【问题讨论】:

    标签: powershell


    【解决方案1】:

    报告了类似的错误并可能已确认: Powershell errors with apostrophe in path

    不过,由于 PowerShell 版本不同,错误消息有所不同。

    也许没有直接的解决方法:这个错误的调用不在我们的控制之下。如果重命名麻烦的目录不是一个选项,那么配置文件的替代位置可能是一个合适的解决方案。尝试将您的个人资料发送到$PsHome。

    > man about_profiles
    ...
    For example, the Windows PowerShell console supports the following basic
    profile files. The profiles are listed in precedence order. The first
    profile has the highest precedence. 
    
    Description                Path
    -----------                ----
    Current User, Current Host $Home\[My ]Documents\WindowsPowerShell\Profile.ps1
    Current User, All Hosts    $Home\[My ]Documents\Profile.ps1
    All Users, Current Host    $PsHome\Microsoft.PowerShell_profile.ps1
    All Users, All Hosts       $PsHome\Profile.ps1
    ...
    

    最后两个配置文件位置应该适合您(除非$PsHome 确实包含问题字符)。

    【讨论】:

      【解决方案2】:

      尝试使用以下转义序列:

      d:\Owen`'s Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
      

      【讨论】:

      • 你不懂。正在执行的任何代码行都不受我的控制。配置文件是empty,如果存在就会发生错误。我无法使用转义序列,因为我无处可写代码。
      • 我的错,除了从文件夹名称中删除撇号之外,我不确定在这种情况下如何解决此问题。看来微软实际上并没有预料到带有撇号的配置文件路径。
      【解决方案3】:

      我自己也遇到了这个问题。我正在运行 PSv5。我也尝试使用重音来绕过它,但我发现这对我不起作用。所以我想“让我们看看智能感知用它做了什么”,果然它找到了解决方案:

      使用两个撇号!

      PS> $path = 'd:\Owen''s Documents'
      PS> $path
      d:\Owen's Documents
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-07
        • 1970-01-01
        • 1970-01-01
        • 2015-08-22
        • 2023-01-23
        • 2014-10-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多