【问题标题】:Running a powershell script which resides on a remote machine?运行驻留在远程计算机上的 powershell 脚本?
【发布时间】:2020-10-10 01:32:59
【问题描述】:

我们的服务器上有一个 powershell 脚本。试图弄清楚如何使用带有 -ComputerName 的 Invoke-Command 来远程运行它,但是当我运行时:

Invoke-Command - ComputerName XXXX -FilePath YYYY

看起来 Invoke-Command 正在寻找本地文件。如何指定我要远程运行仅在远程机器上可用的脚本?

【问题讨论】:

  • 你的意思是它是否在网络共享上?如果是这样,请在 PSSession 时尝试 -Authenication Credssp。这允许双跳

标签: powershell


【解决方案1】:

您可以将-ScriptBlock 参数与dot sourcing 一起使用。

Invoke-Command -ComputerName machine1 -Scriptblock {
    . "C:\path\to\file.ps1"
}

编辑:添加使用凭据的版本

# Will bring up a dialogue box asking for credentials
$creds = get-credential
Invoke-Command -ComputerName machine1 -Credential $creds -Scriptblock {
    . "C:\path\to\file.ps1"
}

【讨论】:

  • 如何添加用户名/密码凭据?
猜你喜欢
  • 2014-07-26
  • 1970-01-01
  • 1970-01-01
  • 2019-12-03
  • 1970-01-01
  • 2015-11-06
  • 1970-01-01
  • 2021-08-22
  • 1970-01-01
相关资源
最近更新 更多