Powershell:Get-WmiObject 不能 运行 asJob 在循环中
Powershell: Get-WmiObject cannot run asJob in loop
有时我想从域中的服务器查看一些信息。在这个例子中,我试图远程获取 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.
似乎,脚本块 {} 看不到变量 $computer
。
当我将变量 -computer $computer
替换为服务器名称时,它正在工作。
对于 Invoke-Command 内部的变量扩展,使用参数块并指定参数:
$computer = 'localhost'
Invoke-Command {param($computer) Get-WmiObject Win32_OperatingSystem -Computername $computer } -ArgumentList $computer
正在研究 PS v5.
有时我想从域中的服务器查看一些信息。在这个例子中,我试图远程获取 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.
似乎,脚本块 {} 看不到变量 $computer
。
当我将变量 -computer $computer
替换为服务器名称时,它正在工作。
对于 Invoke-Command 内部的变量扩展,使用参数块并指定参数:
$computer = 'localhost'
Invoke-Command {param($computer) Get-WmiObject Win32_OperatingSystem -Computername $computer } -ArgumentList $computer
正在研究 PS v5.