Powershell 将参数从ps1发送到其他ps1
我有一个名为file.ps1的文件,其代码如下:Powershell 将参数从ps1发送到其他ps1,powershell,Powershell,我有一个名为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 但是 Star
$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。这是因为$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
``允许您在内部使用。这是因为$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=\\networkpath with spaces\file.ps1 Start Process powershell-ArgumentList-noprofile-file$Ps1Path$var-verb runasi如果您必须继续使用文件路径中的空格,我编辑我的答案。我爱你@JPBlanc。你愿意嫁给我吗?谢谢,谢谢,再次感谢!!!非常感谢您回复@JPBlanc。现在,新的windows powershell ISE在输入凭据后立即打开和关闭。可能是因为\\path\file.ps1包含空格并且在单引号内?我编辑了我的问题。我还尝试添加一个带有路径的新变量,但参数列表似乎只适用于双引号:$var=test$Ps1Path=\\networkpath with spaces\file.ps1 Start Process powershell-ArgumentList-noprofile-file$Ps1Path$var-verb runasi如果您必须继续使用文件路径中的空格,我编辑我的答案。我爱你@JPBlanc。你愿意嫁给我吗?谢谢,谢谢,再次感谢!!!