繁体   English   中英

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

[英]Why does command work in Windows CMD and not PowerShell?

我有一个将可执行文件和 .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"
  }
}

根本原因是因为PowerShell有一个更标准的规则来解析与cmd不同的参数 在 PowerShell 中,如果参数以引号开头,那么它也会以引号结尾。 但是,在 cmd 中,如果引号后没有分隔符(如空格或制表符),则参数会继续。 这意味着"powershell someargs "another""在 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""

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM