为什么命令在Windows CMD而不是PowerShell中有效?

时间:2017-11-10 04:29:46

标签: powershell psexec

我有一个脚本可以将可执行文件和.ps1保存到远程计算机列表中。 然后它在每台计算机上运行可执行文件。 我必须使用.ps1调用可执行文件,因为它在静默运行时的设置方式。

我注意到我的一个命令从命令行快速运行,但似乎挂在我的脚本中。有什么理由会发生这种情况吗?

命令是:

psexec -s @C:\myscript\computers.txt cmd /c "Powershell Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass && PowerShell -noninteractive -file "C:\Install.ps1""

这是我的整个脚本:

cls #clear screen

function CopyFiles {

    # Get .exe from source and place at destination workstation
    $source2="\\mainserver\program.exe"
    $destination2="\\$line\c$\program.exe"           # place .exe on C:\ directory of worstation
    Copy-Item -Recurse -Filter *.* -path $source2 -destination $destination2 -Force

    # Get .bat from source and place at destination workstation
    $source3="\\fileserver\Install.ps1"
    $destination3="\\$line\c$\Install.ps1"  # place .bat on C:\ directory of worstation
    Copy-Item -Recurse -Filter *.* -path $source3 -destination $destination3 -Force
}

$a = Get-Content "C:\myscript\computers.txt"
foreach($line in $a)
{

  "These options must run in numbered order."
  " "
  " "
  "1. Copy installer to remote computer(s)."
  "2. Remove application from remote computer(s)."
  "3. Install application from remote computer(s)."
  "4. Quit."
  " "
  "Type number and press Enter." 
  $UI = Read-Host -Prompt ' '

  If ($UI -eq 1) {
    CopyFiles
  } ELSEIF ($UI -eq 2) {
  psexec @C:\myscript\computers.txt -c "\\fileserver\Remove.bat"
  } ELSEIF ($UI -eq 3) {
  psexec -s @C:\myscript\computers.txt cmd /c "Powershell Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass && PowerShell -noninteractive -file "C:\Install.ps1""
  } ELSEIF ($UI -eq 4) {
  "Good Bye"
  }
}

1 个答案:

答案 0 :(得分:4)

根本原因与您的脚本无关。这是因为PowerShell has a more standard rule to parse parameters与cmd不同

尝试回显来自PowerShell的命令,您将看到它识别为2个参数

PS U:\> echo "Powershell Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass && PowerShell -noninteractive -file "C:\Install.ps1""
Powershell Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass && PowerShell -noninteractive -file
C:\Install.ps1

而cmd将其识别为一个

C:\Users>type testparam.bat
@echo off
echo %1
echo %2
echo %3
C:\Users>testparam "Powershell Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass && PowerShell -noninteractive -file "C:\Install.ps1""
"Powershell Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass && PowerShell -noninteractive -file "C:\Install.ps1""
ECHO is off.
ECHO is off.

您还可以在PowerShell中使用该.bat文件来验证

上述原因是PowerShell strips double quotes from parameters。现在要解决这个问题,你必须使用这些

中的任何一个在参数中包含双引号
psexec -s @C:\myscript\computers.txt cmd /c "Powershell Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass && PowerShell -noninteractive -file ""C:\Install.ps1"""
psexec -s @C:\myscript\computers.txt cmd /c "Powershell Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass && PowerShell -noninteractive -file `"C:\Install.ps1`""
psexec -s @C:\myscript\computers.txt cmd /c '"Powershell Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass && PowerShell -noninteractive -file "C:\Install.ps1"'
psexec -s @C:\myscript\computers.txt cmd /c --% "Powershell Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass && PowerShell -noninteractive -file "C:\Install.ps1""

或者,如果文件路径没有任何空格,您只需删除它们即可在cmd和PowerShell中使用

psexec -s @C:\myscript\computers.txt cmd /c "Powershell Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass && PowerShell -noninteractive -file C:\Install.ps1"

您可以找到有关这些报价here

的更多信息
相关问题