【问题标题】:remote Multiple invocations using Powershell使用 Powershell 进行远程多次调用
【发布时间】:2015-06-25 01:38:06
【问题描述】:

感谢您抽出宝贵时间阅读本文。

我的问题如下:我正在尝试创建一个使用 powershell 执行以下操作的程序:

  1. 获取在 powershell 外部生成的表

  2. 使用表中的参数循环调用 powershell 脚本

  3. 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


【解决方案1】:

可以从 powershell 访问 UNC 路径吗?将 testcommands.cmd 复制到本地路径,试试看是否可行!

$CommandsPath = "\\sharedlocation\testcommands.cmd"
if(Test-Path $CommandsPath)
{
   $path = "C:\"+$Var1+"\TOOLS\"+$Var2+"\launchtool.cmd & " + $CommandsPath
   $scriptPath = [scriptblock]::Create($path)
   $out | invoke-command {$scriptPath } 
}

【讨论】:

  • 它既可以从共享路径访问,也可以在本地复制时访问。我尝试按照下面 Mrwaim 的建议进行操作,现在我正在运行 launchtool.cmd,但在 testcommands.cmd 部分拒绝访问,即使当我从服务器本身的 powershell 和我的帐户运行它时整个事情都正常工作using 可以完全访问所有机器(已确认)
  • 您可能需要检查远程机器中的执行策略并将其设置为remotesigned。 Set-ExecutionPolicy RemoteSigned -Force
【解决方案2】:

试试,invoke-expression "cmd.exe /c C:\7058\TOOLS\Jason\launchtool.cmd & \sharedlocation\testcommands.cmd"

cmd.exe /c 是我确保 cmd 和 powershell 一致性的最佳方式

【讨论】:

  • 当我对其进行硬编码时它实际上可以工作,问题是当我尝试使用生成的路径时。我将继续尝试使用 cmd.exe /c 我已对其进行了修改,使其如下所示:$CommandsPath = " \\sharedlocation\testcommands.cmd" $path = "cmd.exe /c C:\"+$Var1+"\TOOLS\"+$Var2+"launchtool.cmd '&'" + $CommandsPath Invoke-Expression $path 当我在服务器本身上运行它时,它实际上工作正常,但远程启动(使用具有适当权限的帐户) ) ,它仍然运行 launchtool.cmd 但第二部分拒绝访问。
  • 你可能需要 cmd /c "your-command here" - 意思是引用命令
猜你喜欢
  • 1970-01-01
  • 2010-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多