【发布时间】:2015-06-25 01:38:06
【问题描述】:
感谢您抽出宝贵时间阅读本文。
我的问题如下:我正在尝试创建一个使用 powershell 执行以下操作的程序:
获取在 powershell 外部生成的表
使用表中的参数循环调用 powershell 脚本
powershell 脚本调用一种特殊类型的 .cmd 文件,然后在其上运行位于不同共享位置的命令。
现在我的问题在于第三点。
我目前正在使用以下方法来调用我的脚本(争论只是硬编码以使其正常工作,它们将由稍后第 2 步中的调用生成):
powershell.exe -ExecutionPolicy Bypass -Command {invoke-command -file \\sharedlocation\test5.ps1 -computername server1121 -argumentlist 7058,Jason}
test5.ps1的内部目前是:
param(
[Parameter(Mandatory=$true)]
[string] $Var1,
[Parameter(Mandatory=$true)]
[string] $Var2
)
$CommandsPath = "\\sharedlocation\testcommands.cmd"
$path = "C:\"+$Var1+"\TOOLS\"+$Var2+"launchtool.cmd"
$scriptPath = [scriptblock]::Create($path)
$out | invoke-command {PARAM($MyArg) $scriptPath } -ArgumentList $CommandsPath
我也尝试过使用
$CommandsPath = "\\sharedlocation\testcommands.cmd"
$path = "C:\"+$Var1+"\TOOLS\"+$Var2+"\launchtool.cmd & " + $CommandsPath
$scriptPath = [scriptblock]::Create($path)
$out | invoke-command {$scriptPath }
我还尝试使用硬编码的测试命令进行调用,而不是将它们放在文件中。
现在我的问题是在这两种情况下,它确实运行了 launchtool.cmd,但它没有通过 testcommands.cmd 文件。
但是当我在机器上运行时
C:\7058\TOOLS\Jason\launchtool.cmd & \\sharedlocation\testcommands.cmd
效果很好。
任何想法我做错了什么?
【问题讨论】:
-
你的问题似乎更多地与让 cmd 和 powershell 做同样的事情有关,而不是“多次远程调用”
-
什么是
$out,你为什么要把它传给Invoke-Command?
标签: powershell command-line-arguments remote-server