【问题标题】:Execute Powershell script on older version of PS在旧版 PS 上执行 Powershell 脚本
【发布时间】:2019-11-14 16:50:07
【问题描述】:

我正在使用 PowerShell 5.0,我想在另一台运行 PowerShell 2.0 的服务器上启动脚本。

我尝试使用Invoke-Command,但它不起作用,你知道是否有解决方案可以在我的电脑上“模拟”脚本?

Invoke-Command -ScriptBlock {Get-printer} -ComputerName server

术语 Get-Printer 未被识别为 CmdLet、函数、脚本文件或可运行程序的名称。检查名称的拼写,如果包含路径,请验证路径是否正确,然后重试。

【问题讨论】:

  • 不,Get-Printer 在任何仍在运行 powershell v2 的 PC 上都不作为 cmdlet 存在。

标签: powershell


【解决方案1】:

在这种情况下,使用参数ComputerName 会更容易。它将连接到远程机器并自行检索打印机。

Get-Printer -ComputerName 'Server'

另一种选择是使用WMI

Get-WmiObject -ClassName 'Win32_Printer' -ComputerName 'Server'

如果你真的想使用Invoke-Command:

Invoke-Command -ComputerName 'Server' -ScriptBlock {
    Get-WmiObject -ClassName 'Win32_Printer'
}

最后一个选项将在远程机器上执行代码。它很可能会起作用,因为 CmdLet Get-WmiObject 存在于远程计算机上旧版本的 PowerShell 中。

【讨论】:

    【解决方案2】:

    感谢您的回答,我知道 -ComputerName 的另一种方式, 我想优先使用 Invoke-command。

    我会试试 Get-wmiObject

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-02
      • 2021-11-06
      • 2012-09-18
      • 2012-06-14
      • 1970-01-01
      相关资源
      最近更新 更多