使用Invoke-Command传递脚本

时间:2016-04-28 16:54:24

标签: powershell powershell-v4.0

我有一个脚本,它使用Windows 2012域控制器上的一组cmdlet。我正在使用没有这些的Windows 7机器(例如Get-DHCPServerV4Lease)。

首先,我创建了一个脚本模块,并按如下方式应用它:

Invoke-Command -Computername <Server> -scriptblock {Get-ComputerStatus}

然后我收到一条错误消息,指出无法识别cmdlet。然后我把它转换成一个直接的PS1脚本:

icm -cn <server> -FilePath .\ComputerInfo.ps1 -ArgumentList "Computer"

运行此操作后,我收到一条错误消息,指出我在脚本中使用的各种cmdlet无法识别。

  

Get-DnsServerResourceRecord:术语&#39; Get-DnsServerResourceRecord&#39;   不被识别为cmdlet的名称,......

如何使用Invoke-Command针对DC运行脚本?

1 个答案:

答案 0 :(得分:1)

添加到Frode F的评论中,您需要先在目标计算机上安装相应的模块,您在Invoke-Command cmdlet中将其指定为<server>。然后在脚本中,您需要先导入该模块,然后才能执行通过该模块可用的cmdlet。

Invoke-Command -Computername <Server> -scriptblock 
{
    Import-Module  <moduleName> -ErrorAction SilentlyContinue
    Get-ComputerStatus
    #<Any othe cmdlets from the module>
}
相关问题