如何从 Invoke-command -Asjob 获取局部变量值?

问题描述 投票:0回答:0

我想在远程计算机上运行 [System.Net.Dns]::GetHostAddresses("mail") 并写入它。

Foreach($PCName in $PCNameArray) {
    $arrayJobs += Invoke-Command -Credential $cred -scriptblock { 
                param (
                    [String]$ip                )
                $IPs = [System.Net.Dns]::GetHostAddresses("mail")
                $ip = $ips[0]
    } -ComputerName $PCName -AsJob -ArgumentList $ip
}

Wait-Job $arrayJobs

$ResultArray=[System.Collections.ArrayList]@()

foreach ($job in $arrayJobs) {
    if ($job.State -eq 'Completed') {
        $ResultArray += Receive-Job $job
    } 
    Stop-Job $job
}

$ResultArray | Select PsComputerName,ip | Export-Csv $OutputFile -NoTypeInformation -Delimiter ';' -Encoding UTF8

如何从所有作业的 Invoke-Command 内部获取 $ip 值?

powershell invoke-command
© www.soinside.com 2019 - 2024. All rights reserved.