为什么命令在Windows CMD中工作而不是在PowerShell中工作?

为什么命令在Windows CMD中工作而不是在PowerShell中工作?,powershell,psexec,Powershell,Psexec,我有一个脚本,可以将可执行文件和.ps1保存到远程计算机列表中。 然后在每台计算机上运行可执行文件。 我必须使用.ps1调用可执行文件,因为它在静默运行时的设置方式 我注意到我的一个命令从命令行快速运行,但似乎挂起在我的脚本中。这有什么原因吗 命令是: psexec -s @C:\myscript\computers.txt cmd /c "Powershell Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass &

我有一个脚本,可以将可执行文件和.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"
  }
}
根本原因是因为它与cmd不同。在PowerShell中,如果参数以引号开头,那么它也将以引号结尾。但是,在cmd中,如果引号后没有分隔符(如空格或制表符),则该参数将继续。这意味着
“powershell someargs”另一个“
在powershell中有2个参数,但在cmd中只有1个。尝试从PowerShell回显命令,您将立即看到

PS D:\> echo 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
C:\Install.ps1
被拆分为一个不同的参数,而cmd被识别为一个参数:

D:\>type testparam.bat
@echo off
:loop
if [%1]==[] exit /b
echo [%1]
shift
goto :loop

D:\>testparam.bat psexec -s @C:\myscript\computers.txt cmd /c "ps -ExecutionPolicy Bypass AND ps -noninteractive -file "C:\Install.txt""
[psexec]
[-s]
[@C:\myscript\computers.txt]
[cmd]
[/c]
["ps -ExecutionPolicy Bypass AND ps -noninteractive -file "C:\Install.txt""]
正如您所看到的,中间的引号不是嵌套的引号,而是保留在命令中,然后
cmd/c
将有另一个奇怪的行为:剥离第一个和最后一个引号,并将剩余的内容作为一个命令运行

PowerShell还包括但仅限于每个参数的外部参数,然后再传递给命令


现在要解决这个问题,您必须使用以下任一方法在参数中包含双引号

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"
事实上,如果路径包含空格,如
“C:\some dir\Install.ps1”
,那么您的命令将在cmd和PowerShell中失败,因为现在cmd还将空格后的部分拆分为一个新参数。试试看

psexec -s @C:\myscript\computers.txt cmd /c "Powershell Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass && PowerShell -noninteractive -file "C:\some dir\Install.ps1""
根本原因是因为它与cmd不同。在PowerShell中,如果参数以引号开头,那么它也将以引号结尾。但是,在cmd中,如果引号后没有分隔符(如空格或制表符),则该参数将继续。这意味着
“powershell someargs”另一个“
在powershell中有2个参数,但在cmd中只有1个。尝试从PowerShell回显命令,您将立即看到

PS D:\> echo 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
C:\Install.ps1
被拆分为一个不同的参数,而cmd被识别为一个参数:

D:\>type testparam.bat
@echo off
:loop
if [%1]==[] exit /b
echo [%1]
shift
goto :loop

D:\>testparam.bat psexec -s @C:\myscript\computers.txt cmd /c "ps -ExecutionPolicy Bypass AND ps -noninteractive -file "C:\Install.txt""
[psexec]
[-s]
[@C:\myscript\computers.txt]
[cmd]
[/c]
["ps -ExecutionPolicy Bypass AND ps -noninteractive -file "C:\Install.txt""]
正如您所看到的,中间的引号不是嵌套的引号,而是保留在命令中,然后
cmd/c
将有另一个奇怪的行为:剥离第一个和最后一个引号,并将剩余的内容作为一个命令运行

PowerShell还包括但仅限于每个参数的外部参数,然后再传递给命令


现在要解决这个问题,您必须使用以下任一方法在参数中包含双引号

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"
事实上,如果路径包含空格,如
“C:\some dir\Install.ps1”
,那么您的命令将在cmd和PowerShell中失败,因为现在cmd还将空格后的部分拆分为一个新参数。试试看

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

非常感谢。我只是注意到它没有循环我的CopyFiles函数。它将program.exe和install.ps1复制到列表中的第一台计算机并停止。其他选项在计算机列表中循环。你知道这可能是什么原因吗?我不确定。也许你可以问另一个问题谢谢。我只是注意到它没有循环我的CopyFiles函数。它将program.exe和install.ps1复制到列表中的第一台计算机并停止。其他选项在计算机列表中循环。你知道这可能是什么原因吗?我不确定。也许你可以问另一个问题