Powershell 中变量命令的 Psexec 问题

问题描述 投票:0回答:1

我希望通过 Powershell 脚本自动使用 Psexec。目的是将大量命令发送到服务器列表。到目前为止,当您了解 Psexec 的可能性时,并没有什么特别的。当我想将要在远程计算机上执行的命令放入变量中时,就会出现问题。一旦命令有空格,它就无法在远程计算机上执行,并且出现错误:找不到指定的路径。我尝试将变量放在“”或“”或``之间,但没有任何变化。

在脚本中,此命令运行良好:

PsExec.exe \\$SelectedServerIP -u my_user -p my_password ipconfig /all

但是这些不起作用:

$command = "ipconfig /all"
PsExec.exe \\$SelectedServerIP -u my_user -p my_password $command

$command = "ipconfig /all"
PsExec.exe \\$SelectedServerIP -u my_user -p my_password ""$command""

$command = ipconfig /all
PsExec.exe \\$SelectedServerIP -u my_user -p my_password ""$command""

$command = ipconfig /all
PsExec.exe \\$SelectedServerIP -u my_user -p my_password """$command"""

谢谢您的回答。

LG

powershell psexec
1个回答
0
投票

psexec
希望远程执行的命令行被指定为 单独参数,而不是单个字符串

因此,使用数组以编程方式定义这些参数:

$command = @('ipconfig', '/all')
PsExec.exe \\$SelectedServerIP -u my_user -p my_password $command

注:

  • 当将数组传递给外部程序时,PowerShell 会自动将其元素作为单独的参数传递;换句话说:它隐式执行splatting;为了概念清晰,您可以选择显式使用泼溅,但是,通过传递
    @command
    而不是
    $command
© www.soinside.com 2019 - 2024. All rights reserved.