proxb / PoshRSJob

Provides an alternative to PSjobs with greater performance and less overhead to run commands in the background, freeing up the console and allowing throttling on the jobs.
MIT License
541 stars 87 forks source link

Array in scriptbloc #202

Open Eperrial opened 5 years ago

Eperrial commented 5 years ago

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 "//////////////////////////////////////////////////////////////////////////////////////////////////"

copdips commented 5 years ago

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

Eperrial commented 5 years ago

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 `
copdips commented 5 years ago

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