PowerShell脚本不会将$ PSDEFAULTPARAMETERVALUE添加到$ profile

发布于 2025-02-11 20:39:53 字数 551 浏览 2 评论 0 原文

我正在编写一个快速的PowerShell脚本以导入模块并更新各种计算机上的一些默认参数。我遇到了一个问题,当我在脚本中添加 $ PSDEFAULTPARAMETERVALUES 到$ profile时,它将更改为 system.management.automation.defaultParameterDictionary ,然后引发了一个错误不被认为是cmdlet的名称。

中的代码

这是我的PS1脚本 add -content -path $ profile -value

“ $ psdefaultParametervalues = @{}” 以下是添加到profile

system.manage.automation.automation.defaultparameterdictionarydictionary dictionary dictionary dictionary = @{}

我已经尝试了从使用set-content到使用变量避免引用混乱的所有方法。

感谢您的帮助!

I'm writing a quick Powershell script to import modules and update some default parameters on various machines. I'm running into an issue where in my script when I add $PSDefaultParameterValues to the $profile it changes to System.Management.Automation.DefaultParameterDictionary which then throws an error of not being recognized as the name of a cmdlet.

Here is the code in my ps1 script

Add-Content -Path $PROFILE -Value "$PSDefaultParameterValues = @{}"

Here is what gets added to the profile

System.Management.Automation.DefaultParameterDictionary = @{}

I've tried everything from using Set-Content to using variables to avoid quotation confusion.

I appreciate the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

习ぎ惯性依靠 2025-02-18 20:39:54

使用单语引号

Add-Content -Path $PROFILE -Value '$PSDefaultParameterValues = @{}'

这些是字面字符串,因此任何变量都不会扩展。

Use single quotes

Add-Content -Path $PROFILE -Value '$PSDefaultParameterValues = @{}'

These are literal strings so any variables will not expand.

べ映画 2025-02-18 20:39:54

要添加到 tonypags的帮助答案

  • double> double> double> double quoted powershell strings(<代码“ ...” )是 expligable strings ,即,它们执行嵌入式变量引用的字符串interpolation 和子表达(例如 $(1 + 2)

  • 例如>'...')是 verbatim strings ,即它们的内容被用作-is(逐字化,从字面上)。


因此,自动 $ PSDEFAULTPARAMETERVALUE 变量在您的“ ...”中扩展了 字符串,这本质上意味着它被其 .toString()表示 - 在这种情况下,这仅是变量值的类型名称

  • 如果您的全部字符串被用来使用 verbatim ,请使用'...'...'引用,如Tonypags的答案所示。

  • 如果您确实需要扩展(字符串插值),但需要选择性地抑制 easse $ $ tarne ,使用`(backtick),powershell's 逃脱字符,如下所示:

      $ enc ='utf8'
     #在$ PSDEFAULTPARAMETERVALUS之前,请注意Backtick(`)。
     #注意:只有 * ofter *引用 
     #是否执行扩展。
     附加符号 -  path $ profile``````
                 -value“`$ psdefaultParameTervalues = @{'*:encoding'='$ eng'}”
     

To add to tonypags' helpful answer:

  • Double-quoted PowerShell strings ("...") are expandable strings, i.e they perform string interpolation of embedded variable references (e.g. $PSDefaultParameterValues) and subexpressions (e.g. $(1 + 2))

  • Single-quoted strings ('...') are verbatim strings, i.e. their content is used as-is (verbatim, literally).


Thus, the automatic $PSDefaultParameterValues variable was expanded in your "..." string, which in essence means it was replaced with its .ToString() representation - which in this case is simply the type name of the variable's value.

  • If your entire string is meant to be used verbatim, use '...' quoting, as shown in tonypags' answer.

  • If you do need expansions (string interpolation), but need to selectively suppress them, escape $ characters as `$, using ` (a backtick), PowerShell's escape character, as shown in the following example:

     $enc = 'utf8'
     # Note the backtick (`) before $PSDefaultParameterValues.
     # NOTE: It is only the *outer* quoting that determines 
     #       whether expansion is performed.
     Add-Content -Path $PROFILE `
                 -Value "`$PSDefaultParameterValues = @{ '*:Encoding' = '$enc' }"
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文