You have to pass in the server parameter. The job is run in a seperate process and can't access your $srv
string if you don't pass it as an argument. In PS3.0 you could use the "using" scope (ex. $using:srv
), but in PS2.0 you need to use param(..)
in your scriptblock and -Argumentlist
to send the objects. Try this:
$server=@("SERVER1","SERVER2")
foreach($srv in $server)
{
Start-Job -scriptblock {
param($serv)
$out=@()
$XX=Get-EventLog -ComputerName $serv -LogName "application" | Where-Object {$_.InstanceID -ge 1000 -and $_.InstanceID -lt 1005} | select -First 5
foreach($x in $XX){
$obj=New-Object PSObject
$obj|Add-Member -MemberType "NoteProperty" -Name Server -Value $srv
$obj|Add-Member -MemberType "NoteProperty" -Name Index -Value $x.Index
$obj|Add-Member -MemberType "NoteProperty" -Name EntryType -Value $x.EntryType
$obj|Add-Member -MemberType "NoteProperty" -Name Source -Value $x.Source
$obj|Add-Member -MemberType "NoteProperty" -Name InstanceID -Value $x.InstanceID
$obj|Add-Member -MemberType "NoteProperty" -Name Message -Value $x.Message
$out += $obj
}
$out
} -ArgumentList $srv
}
$A = Get-Job | Wait-Job | Receive-Job
$A | Out-GridView
If you want to exclude the runspaceid(one of the properties that start-job adds to specify where the object was created), try something like this :
$A | Select-Object -Property * -ExcludeProperty RunspaceID | Out-GridView