PoshRSJob icon indicating copy to clipboard operation
PoshRSJob copied to clipboard

Array in scriptbloc

Open Eperrial opened this issue 6 years ago • 3 comments

Hello,

I am a student in DUT Networks and telecommunication and also a beginner in powershell. (FR : DUT / UK : DHE / US : AD ) After a few hours of searching the Internet, I would like to display the different tables in the RSJob.

image

i have this return on my terminal, someone can guide me ? :)

image

( the code on plain text if someone want to edit )

$ADCheck=@(Get-ADComputer -filter "*" -Properties IPv4address -SearchBase "OU=COMPUTERS,DC=test,DC=local" |Where-Object {$_.ipv4address} | Select-Object -ExpandProperty ipv4address ) $TableauEchP=@() $TableauRPES=@() $TableauRPRS=@() $ADCheck | Start-RSJob -Name {$_} -Throttle $env:NUMBER_OF_PROCESSORS -ScriptBlock { param($ip)[string] if(Test-Connection -Count 1 -ComputerName $ip -Quiet) { try { if(Get-Service -Name test) { $TableauRPRS += $ip } else { $TableauRPES += $ip } } catch{} } else { $TableauEchP += $ip } } Get-RSJob | Wait-RSJob Write-Host "//////////////////////////////////////////////////////////////////////////////////////////////////" Write-Host "This is IP as all test on TRUE" $TableauRPRS Write-Host "//////////////////////////////////////////////////////////////////////////////////////////////////" Write-Host "Ping : TRUE - Service : FALSE" $TableauRPES Write-Host "//////////////////////////////////////////////////////////////////////////////////////////////////" Write-Host "Ping : FALSE, check computer if is up" $TableauEchP Write-Host "//////////////////////////////////////////////////////////////////////////////////////////////////"

Eperrial avatar Aug 05 '19 10:08 Eperrial

The vars $tableaux is not aware in the threads (runspaces). You can try to use Receive-RSJob as shown in the readme.md

copdips avatar Aug 05 '19 11:08 copdips

it's not easy when it's the first time with powershell ! aha ^^

image

Thanks for this !

( the code on plain text if someone want to copy )

$ADCheck=@(Get-ADComputer -filter "*" -Properties IPv4address -SearchBase "OU=COMPUTERS,DC=adep,DC=local" |Where-Object {$_.ipv4address} | Select-Object -ExpandProperty ipv4address ) $oui = $ADCheck | Start-RSJob -Name {$_} -Throttle $env:NUMBER_OF_PROCESSORS -ScriptBlock { param([string]$ip) if(Test-Connection -Count 1 -ComputerName $ip -Quiet) { #Write-Host "Ping sur $ip Reussi" try { if(Get-Service -Name oui) { #Write-Host "L'ordinateur d'ip : $ip à bien reussi le test du ping et du service" [PSCustomObject]@{ Name = $ip Ping = "Ok" Service = "Ok" } } else { #Write-Host "L'ordinateur d'ip : $ip à échoué dans le test du service mais est accessible !" [PSCustomObject]@{ Name = $ip Ping = "Ok" Service = "Erreur" } } } catch{} } else { #Write-Host "Ping sur $ip : Echoue" [PSCustomObject]@{ Name = $ip Ping = "Erreur" Service = "Erreur" } } }| Wait-RSJob | Receive-RSJob

Eperrial avatar Aug 05 '19 14:08 Eperrial

glad to see this works for you, please close the issue if it is resolved.

copdips avatar Aug 06 '19 22:08 copdips