Author Posts

January 1, 2012 at 12:00 am

by the_hutch at 2012-11-13 13:43:39

I have 4 background jobs running at one time. I can view the output easily enough with: Get-Job | Receive-Job I would like to assign this output to a variable. Is there any way to do this?

I tried this (didn't work):

$Output = Get-Job | Receive Job

Thanks in advance.

The whole code (with IP addresses obscured with ##) can be seen below:


cd C:\pinger\
$ErrorActionPreference = "silentlycontinue"
##Runs Pinger to collect IP addresses from each of the 4 Class B ranges
Start-Sleep 5

$job1 = {
.\pinger.exe ###.###.0.0-###.###.255.255 -h Range1.txt
}

write-host "Running ###.### subnet"
Start-Job -ScriptBlock $job1
While (Get-Job -State "Running") {Start-Sleep 30}

$job2 = {
.\pinger.exe ###.###.0.0-###.###.255.255 -h Range2.txt
}

write-host "Running ###.### subnet"
Start-Job -ScriptBlock $job2
While (Get-Job -State "Running") {Start-Sleep 30}

$job3 = {
.\pinger.exe ###.###.0.0-###.###.255.255 -h Range3.txt
}

write-host "Running ###.### subnet"
Start-Job -ScriptBlock $job3
While (Get-Job -State "Running") {Start-Sleep 30}

$job4 = {
.\pinger.exe ###.###.0.0-###.###.255.255 -h Range4.txt
}

write-host "Running ###.### subnet"
Start-Job -ScriptBlock $job4
While (Get-Job -State "Running") {Start-Sleep 30}

Get-Job | Stop-Job
Get-Job | Remove-Job

$Range1 = Get-Content .\Range1.txt
$Range2 = Get-Content .\Range2.txt
$Range3 = Get-Content .\Range3.txt
$Range4 = Get-Content .\Range4.txt

$job1 = {
param($Range1)
($Range1) | ForEach-Object -Process {
$IP = "$_"
##Run Commands to Pull Info From and assign output to String
$NSLookup = nslookup $IP
$strNS = "$NSLookup"
$NBTstat = nbtstat -A $IP
$strNBT = "$NBTstat"

##Get FQDN from nslookup
IF($strNS -like '*Name*')
{
$A1 = $strNS.Split(":")
$S1 = $A1[3]
$A2 = $S1.Split(" ")
$FQDN = $A2[4]
}
ELSE
{
$FQDN = "FQDN NOT FOUND"
}

##Get Hostname from FQDN
IF($strNS -like '*Name*')
{
$A1 = $FQDN.Split(".")
$NSHost = $A1[0]
}
ELSE
{
$NSHostName = "NS HOST NOT FOUND"
}

##Get MAC and Host from nbtstat
IF($strNBT -like '*Host not found*')
{
$MAC = "MAC NOT FOUND"
$NBTHost = "NBT HOST NOT FOUND"
}
ELSE
{
$A1 = $strNBT.Split("=")
$S1 = $A1[1]
$A2 = $S1.Split(" ")
$PreMAC = $A2[1]
$MAC = $PreMAC.Replace("-",":")

$A3 = $strNBT.Split(" ")
$S2 = $A3[73]
$A4 = $S2.Split("< ")
$NBTHost = $A4[0]
}
$all="$IP`t$NSHost`t$NBTHost`t$MAC`t$FQDN"
write-host "$all"
}
}

Start-Sleep 5
Start-Job -ScriptBlock $job1 -ArgumentList (,$Range1)

$job2 = {
param($Range2)
($Range2) | ForEach-Object -Process {
$IP = "$_"
##Run Commands to Pull Info From and assign output to String
$NSLookup = nslookup $IP
$strNS = "$NSLookup"
$NBTstat = nbtstat -A $IP
$strNBT = "$NBTstat"

##Get FQDN from nslookup
IF($strNS -like '*Name*')
{
$A1 = $strNS.Split(":")
$S1 = $A1[3]
$A2 = $S1.Split(" ")
$FQDN = $A2[4]
}
ELSE
{
$FQDN = "FQDN NOT FOUND"
}

##Get Hostname from FQDN
IF($strNS -like '*Name*')
{
$A1 = $FQDN.Split(".")
$HostName = $A1[0]
}
ELSE
{
$HostName = "HostName NOT FOUND"
}

##Get MAC and Host from nbtstat
IF($strNBT -like '*Host not found*')
{
$MAC = "MAC NOT FOUND"
$NBTHost = "NBT HOST NOT FOUND"
}
ELSE
{
$A1 = $strNBT.Split("=")
$S1 = $A1[1]
$A2 = $S1.Split(" ")
$PreMAC = $A2[1]
$MAC = $PreMAC.Replace("-",":")

$A3 = $strNBT.Split(" ")
$S2 = $A3[73]
$A4 = $S2.Split("< ")
$NBTHost = $A4[0]
}
$all="$IP`t$NSHost`t$NBTHost`t$MAC`t$FQDN"
write-host "$all"
}
}

Start-Sleep 5
Start-Job -ScriptBlock $job2 -ArgumentList (,$Range2)

$job3 = {
param($Range3)
($Range3) | ForEach-Object -Process {
$IP = "$_"
##Run Commands to Pull Info From and assign output to String
$NSLookup = nslookup $IP
$strNS = "$NSLookup"
$NBTstat = nbtstat -A $IP
$strNBT = "$NBTstat"

##Get FQDN from nslookup
IF($strNS -like '*Name*')
{
$A1 = $strNS.Split(":")
$S1 = $A1[3]
$A2 = $S1.Split(" ")
$FQDN = $A2[4]
}
ELSE
{
$FQDN = "FQDN NOT FOUND"
}

##Get Hostname from FQDN
IF($strNS -like '*Name*')
{
$A1 = $FQDN.Split(".")
$HostName = $A1[0]
}
ELSE
{
$HostName = "HostName NOT FOUND"
}

##Get MAC and Host from nbtstat
IF($strNBT -like '*Host not found*')
{
$MAC = "MAC NOT FOUND"
$NBTHost = "NBT HOST NOT FOUND"
}
ELSE
{
$A1 = $strNBT.Split("=")
$S1 = $A1[1]
$A2 = $S1.Split(" ")
$PreMAC = $A2[1]
$MAC = $PreMAC.Replace("-",":")

$A3 = $strNBT.Split(" ")
$S2 = $A3[73]
$A4 = $S2.Split("< ")
$NBTHost = $A4[0]
}
$all="$IP`t$NSHost`t$NBTHost`t$MAC`t$FQDN"
write-host "$all"
}
}

Start-Sleep 5
Start-Job -ScriptBlock $job3 -ArgumentList (,$Range3)

$job4 = {
param($Range4)
($Range4) | ForEach-Object -Process {
$IP = "$_"
##Run Commands to Pull Info From and assign output to String
$NSLookup = nslookup $IP
$strNS = "$NSLookup"
$NBTstat = nbtstat -A $IP
$strNBT = "$NBTstat"

##Get FQDN from nslookup
IF($strNS -like '*Name*')
{
$A1 = $strNS.Split(":")
$S1 = $A1[3]
$A2 = $S1.Split(" ")
$FQDN = $A2[4]
}
ELSE
{
$FQDN = "FQDN NOT FOUND"
}

##Get Hostname from FQDN
IF($strNS -like '*Name*')
{
$A1 = $FQDN.Split(".")
$HostName = $A1[0]
}
ELSE
{
$HostName = "HostName NOT FOUND"
}

##Get MAC and Host from nbtstat
IF($strNBT -like '*Host not found*')
{
$MAC = "MAC NOT FOUND"
$NBTHost = "NBT HOST NOT FOUND"
}
ELSE
{
$A1 = $strNBT.Split("=")
$S1 = $A1[1]
$A2 = $S1.Split(" ")
$PreMAC = $A2[1]
$MAC = $PreMAC.Replace("-",":")

$A3 = $strNBT.Split(" ")
$S2 = $A3[73]
$A4 = $S2.Split("< ")
$NBTHost = $A4[0]
}
$all="$IP`t$NSHost`t$NBTHost`t$MAC`t$FQDN"
write-host "$all"
}
}

Start-Sleep 5

Start-Job -ScriptBlock $job4 -ArgumentList (,$Range4)

While (Get-Job -State "Running") {Start-Sleep 30}

Write-Host "IP`tDNS_HostName`tNetBIOS_HostName`tMAC_Address`tFully_Qualified_Domain_Name"

#Display output from all jobs
Get-Job | Receive-Job

by DexterPOSH at 2012-11-14 03:39:40

Hi Justin,

When you use Receive-Job cmdlet once, the results are flushed out from the memory. In order to keep them you can use the -keep parameter of the cmdlet and then I think when you run the cmdlet second time it would display the result.
I saw your previous post and I think it works fine for you 🙂

A friendly advice :You could use function to do the repeated task again and again.

Hope it makes sense

by the_hutch at 2012-11-18 18:14:57

It wasn't actually a problem with seeing the output when using Receive-Job. It was that I couldn't assign the output to a variable, even if it was the first time using it. I figured it out though. Instead of using write-host in each job, I had to use write-output. But using that, it allowed my to assign the output to a variable with:

$A = (Get-Job | Receive-Job)

And thanks for the tip. I did a lot of cleanup afterwards.

by DonJ at 2012-11-19 07:21:51

Write-Host, generally speaking, is always bad. 🙂