【问题标题】:Using "Invoke-Command" to run a local file on a remote machine使用“Invoke-Command”在远程机器上运行本地文件
【发布时间】:2016-02-26 16:03:38
【问题描述】:

我正在尝试使用 powershell 运行位于计算机 A 上的 bat 脚本并在计算机 B 上执行它

计算机 A 是调用 Invoke Command 函数的计算机

我正在尝试通过

Invoke-Command -ComputerName ComputerB -ScriptBlock{& "\\ComputerA\filepath\batch.bat"}  -Credential $Cred

当我运行它时,我收到了错误

The term '\\ComputerA\filepath\batch.bat' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the  name, or if a path was included, verify that the path is correct and try again.

我也尝试通过在计算机 B 上调用 cmd 来运行它,并将这个 bat 脚本作为参数传递,我没有收到任何错误,但我可以告诉脚本没有运行 我使用的命令是

Invoke-Command -ComputerName ComputerB -ScriptBlock{& "C:\Windows\System32\cmd.exe"} -ArgumentList "\\ComputerA\filepath\batch.bat" -Credential $Cred

当我进入计算机 B 并通过 cmd 手动调用脚本时,它可以工作

我确认 Invoke-Command 有效,因为我能够运行计算机 B 本地的程序

我试图不必在计算机 B 上放置任何文件,例如 psexec 或 bat 脚本本身,这就是我尝试这种方式的原因

谢谢

【问题讨论】:

  • 是的,我可以在两台计算机上本地运行它我可以进入 ComputerB 并通过 cmd 我可以输入 \\ComputerA\filepath\batch.bat 并让它运行良好我也可以通过 Windows 运行它探险家

标签: powershell cmd remote-debugging remoting powershell-remoting


【解决方案1】:

当您远程访问计算机(在本例中为 ComputerB)并尝试访问 ComputerA 上的某些内容时,您基本上是在尝试访问默认情况下不允许的网络资源。使用 Invoke-Command 中的 -EnableNetworkAccess 参数明确允许相同

【讨论】:

    【解决方案2】:

    这听起来像是“双跳”-/“第二跳”问题。您的凭据在远程会话中不可用(访问网络共享)。 解决方案:

    • 使用 CredSSP 身份验证
    • 在本地运行..
      • 将文件复制到远程计算机
      • 使用Invoke-Command -Scriptblock { & "c:\temp\batch.bat" }
      • 如有必要,删除远程计算机上的字段
    • 使用psexec.exe

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-18
      • 2021-07-10
      • 1970-01-01
      • 1970-01-01
      • 2018-12-02
      • 2022-07-01
      相关资源
      最近更新 更多