FeaturedPower ShellSCCMScripts

Background job – examples

Use background job to run multiple script same time in background without waiting for session to complete

$computer = @('server1', 'server2', 'server3')

Start-Job -ScriptBlock { Get-Content $input } -InputObject $computer
Receive-Job -Name Job45 -Keep




$job = Start-Job { 
$computer = @('server1', 'server2', 'server3')
Get-hotfix -ComputerName $computer | Where { $_.HotFixID -eq "KB4504418" } } -PSVersion 5.1
Receive-Job $job


$jobWRM = invoke-command -computerName $computer -scriptblock {get-service winrm} -jobname WinRM -throttlelimit 16 -AsJob



start-job -ScriptBlock {Get-Process } -Name  'hh1'



$session = New-PSSession -ComputerName server1, server2, server3
$job = Invoke-Command -Session $session `
       -ScriptBlock {start-job -ScriptBlock {Get-hotfix | Where { $_.HotFixID -eq "KB4504418" }}  }
Get-Job



$session = New-PSSession -ComputerName server1, server2, server3
Invoke-Command -Session $session `
       -ScriptBlock {Get-hotfix | Where { $_.HotFixID -eq "KB4504418" }} -AsJob

$session = New-PSSession -ComputerName server1, server2, server3
Invoke-Command -Session $session `
              -ScriptBlock {start-job -ScriptBlock {Get-process }} -



$RemoteComputer = @('server1', 'server2', 'server3')
$Session = New-PSSession -ComputerName $RemoteComputer
Invoke-Command -Session $Session -ScriptBlock {Start-Job -ScriptBlock {Get-Process} -Name myJob}
Get-Job

$Result = Invoke-Command -Session $Session -ScriptBlock {Receive-Job -Name myJob -Keep}



########################################################################################################################
param (
    [Parameter(Mandatory)]
    [ValidateNotNullOrEmpty()]
    [string[]]$Servers = "C:\servers.txt"
)

$server = get-content $Servers
foreach ($s in $server) {
    $scriptBlock = {
        if (Test-Connection  -Quiet -Count 1) {
            $kb = 'KB5006669'
            $patch = Get-hotfix | Where { $_.HotFixID -eq $kb }
            if($patch ){
                        Write-output "$env:computername  - $kb - installed - $($patch.installedon)" 
                        #$patch.  | Sort-Object -Property HotFixID -ForegroundColor Green
                        }else {
                            Write-output "$env:computername  - $kb - Notinstalled" 
                        }

        }else{
          Write-Output "The $env:computername is not responding"
        }
    }
    invoke-command  -ScriptBlock $scriptBlock -AsJob -JobName hotfix
}

########################################################################################################################

 

Show More

Related Articles

Back to top button