【问题标题】:send parameter from a ps1 to other ps1 [duplicate]将参数从 ps1 发送到其他 ps1 [重复]
【发布时间】:2020-05-09 04:41:21
【问题描述】:

我有一个名为 file.ps1 的文件,代码如下:

$varsent=$args[0]
echo $varsent

当我尝试使用以下命令调用此 ps1 文件时,因为我需要以提升的权限执行 ps1:

$var = "test"
Start-Process powershell -ArgumentList '-noprofile -file "\\network path with spaces\file.ps1" $var ' -verb RunAs

结果是单词“$var”而不是“test”

但是有

Start-Process powershell -ArgumentList '-noprofile -file "\\network path with spaces\file.ps1" test ' -verb RunAs

结果是单词“测试”

我认为问题在于执行代码时 $var 不会被其值替换,但我不知道为什么。拜托,你能帮帮我吗?我必须使用 $var。

【问题讨论】:

    标签: powershell


    【解决方案1】:

    这是因为$var 没有在单引号字符串中解释,请尝试:

    $var = "test"
    Start-Process powershell -ArgumentList "-noprofile -file \\path\file.ps1 $var " -verb RunAs
    

    带空格:

    Start-Process powershell -ArgumentList "-noprofile -file `"C:\temp\Un petit test.ps1`" $var" -verb RunAs
    

    ``" 允许您在"" 中使用"

    【讨论】:

    • 非常感谢您回复@JPBlanc。现在一个新的 windows powershell ISE 在输入凭据后立即打开和关闭。会是因为 \\path\file.ps1 包含空格并且在单引号内吗?我编辑了我的问题。我还尝试使用路径添加一个新变量,但似乎参数列表仅适用于双引号:($var = "test" $Ps1Path = "\\network path with spaces\file.ps1" Start-Process powershell -ArgumentList "-noprofile -file $Ps1Path $var " -verb RunAs
    • 如果你的文件路径中有空格,你必须继续使用“”,我编辑我的答案。
    • 我爱你@JPBlanc。你愿意嫁给我吗?谢谢,谢谢,谢谢,再次感谢!!!
    猜你喜欢
    • 2023-04-05
    • 2012-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-08
    • 2020-12-16
    • 2015-12-04
    相关资源
    最近更新 更多