2017-05-31 147 views
0

我希望有时检查域中服务器的某些信息。在这个例子中,我尝试远程获取Windows版本(只有一个服务器,目前无环路):Powershell:Get-WmiObject无法在循环中作为运行环境运行

$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. 

看来,该脚本块{}不能看到变量$computer。 当我将变量-computer $computer替换为服务器的名称时,它正在工作。

+0

正确的参数是'-ComputerName' - 你正在使用-Co​​mputer? – Jelphy

+0

Computer和ComputerName是别名。我尝试了很长的名字和输出(错误)是一样的。 – Jerry1

回答

1

对于调用命令内的变量扩展使用PARAM块,并指定参数:

$computer = 'localhost' 
Invoke-Command {param($computer) Get-WmiObject Win32_OperatingSystem -Computername $computer } -ArgumentList $computer 

工作在PS V5。