我有时想从我们的域中的服务器检查一些信息。在此示例中,我尝试远程获取 Windows 版本(只有一台服务器,当前没有循环):
$cr=Get-Credential "domain\adm_user"
$computer="serverA"
Invoke-Command { Get-WmiObject -Class Win32_OperatingSystem -Namespace root/cimv2 -Computer $computer -Credential $cr | Format-List -Property Name, OSArchitecture, SerialNumber} -AsJob -ComputerName .
Get-Job | Wait-Job
Get-Job | Receive-Job
输出:
Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.
似乎,那个 scriptblock {} 看不到 variable $computer
。当我将变量替换-computer $computer
为服务器名称时,它正在工作。