【问题标题】:Passing bool parameter to powershell script shortcut将 bool 参数传递给 powershell 脚本快捷方式
【发布时间】:2013-07-09 11:33:19
【问题描述】:

我编写了一个带有 bool 参数的 powershell 脚本 我也有这个 powershell 脚本的 windows 快捷方式。 问题是每当我尝试从快捷方式运行脚本时,参数都会被解释为字符串,而不是布尔值,并且脚本会崩溃。

这是我最初放在快捷方式目标部分下的内容:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File "C:\Program Files\MySoftware\diagnostic\DiagnosticTool.ps1" $true

我已经在网上搜索了解决方案并尝试了以下选项:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File "C:\Program Files\MySoftware\diagnostic\DiagnosticTool.ps1" -copyAll:$true

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File "C:\Program Files\MySoftware\diagnostic\DiagnosticTool.ps1" -copyAll:`$$true

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File "C:\Program Files\MySoftware\diagnostic\DiagnosticTool.ps1" "`$$true"

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File "C:\Program Files\MySoftware\diagnostic\DiagnosticTool.ps1" `$$true

还有多种这样的变化。

这是我的问题:当从 Windows 快捷方式运行 a 时,有没有办法将 bool 参数的值发送到脚本?

【问题讨论】:

    标签: powershell parameters boolean shortcut


    【解决方案1】:

    试试这个:

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command "& 'C:\Program Files\MySoftware\diagnostic\DiagnosticTool.ps1'" -copyAll:$true
    

    使用-file 参数将bool 传递给脚本的开关参数尝试:

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -file "C:\Program Files\MySoftware\diagnostic\DiagnosticTool.ps1" {-All:$False}
    

    最后一个取自TechNet,但老实说我永远无法使用它。

    【讨论】:

    • 对不起,但程序文件中的空间与命令混淆它只是尝试运行脚本 c:\Program 并认为路径的其余部分是一个参数,而对于你的第二种方法,我有以下错误:Missing expression after unary operator '-'.At line:1 char:137 + C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File "C:\Program Files\MySoftware\diagnostic\DiagnosticTool.ps1" {- <<<< All:$true} + CategoryInfo : ParserError: (-:String) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : MissingExpressionAfterOperator
    • 行得通。非常感谢,虽然我真的不知道 & 在这种特殊情况下做了什么,但是谢谢。
    • @memoryofadream & 是“调用运算符”,它允许您运行存储在变量中并由字符串表示或包含在'/" 中作为字符串的命令
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-01
    • 2015-06-07
    • 2017-05-08
    • 2019-07-07
    • 2013-04-27
    相关资源
    最近更新 更多