I want to run [System.Net.Dns]::GetHostAddresses("mail") on remote computers and write it.
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
How can get $ip value from inside Invoke-Command for all jobs?
$ip = $ips[0]to just$ips[0], or$ips |Select-Object -First 1Select PSComputerName,iptoSelect PSComputerName,@{Name='ip';Expression='IPAddressToString'}also :)