0

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?

6
  • Change $ip = $ips[0] to just $ips[0], or $ips |Select-Object -First 1 Commented Jul 28, 2023 at 8:26
  • I asked about something else, sorry if I confused you... I just want know IP of "mail" host inside of every job, and get this parameter to outside, to some $ResultArray jobs array, from where for example I want get: for Job1 - $Resultarray.PSComputername=Host1, Resultarray.IP=192.168.100.1 for Job2 - $Resultarray.PSComputername=Host2, Resultarray.IP=192.168.100.3 for Job3 - $Resultarray.PSComputername=Host3, Resultarray.IP=192.168.100.1 something like this Commented Jul 28, 2023 at 9:10
  • Yup, I understood that. Did you try what I suggested? You'll need to change Select PSComputerName,ip to Select PSComputerName,@{Name='ip';Expression='IPAddressToString'} also :) Commented Jul 28, 2023 at 9:13
  • please check my answer, I post modifyed code, but it doesnt work too Commented Jul 28, 2023 at 9:21
  • Please describe what happens - does it give you the wrong data in the csv? Does it fail with an error message? If so, what's the error message? I can't see your screen :) Commented Jul 28, 2023 at 9:23

1 Answer 1

0

As mentioned in the comments, all you have to do is to not assign the desired value to a variable, and instead let PowerShell return it:

Foreach($PCName in $PCNameArray) {
    $arrayJobs += Invoke-Command -Credential $cred -scriptblock { 
                param (
                    [String]$ip                )
                $IPs = [System.Net.Dns]::GetHostAddresses("mail")
                $ips[0] # don't assign, just evaluate
    } -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,@{Name='IP';Expression='IPAddressToString'} | Export-Csv $OutputFile -NoTypeInformation -Delimiter ';' -Encoding UTF8

The IPAddressToString property on the resulting [ipaddress] objects will give you the string representation, eg. 10.0.0.1

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.