Run something in the background
$job = Start-Job -name BackgroundJob -ScriptBlock {
foreach($n in (0..100)) {
write-host "Hello World $n"
start-sleep -seconds 2
}
}
$job | select name,state
Get some output
$job | Receive-Job
Keep content
$job | Receive-Job -Keep
Wait for it
$job | Receive-Job -Wait
$job | select state,hasmoredata
Remove it
Get-job | Remove-Job
$job = Start-Job -ScriptBlock { Get-NetAdapter }
$netadap = $job | Receive-Job -Keep
$netadap | select InterfaceDescription
Waiting, Stopping for a job
$job = Start-Job -name BackgroundJob -ScriptBlock {
foreach($n in (0..100)) {
write-host "Hello World $n"
start-sleep -seconds 2
}
}
$job | wait-job
$job | stop-job
Testing a connection
$test = Test-Connection 192.168.1.1 -ErrorAction SilentlyContinue
$test #result
$test = Test-Connection 192.168.1.51 -ErrorAction SilentlyContinue
$test #$null cause no result, we do ignore the errors
As a job
$job = Test-Connection 192.168.1.1 -ErrorAction SilentlyContinue -asjob
while ($job.State -ne "Completed") { start-sleep 1 }
$result = $job | Receive-Job
$responses = $result | select ResponseTime | ? { $_.ResponseTime -ne $null }
if ($responses -eq $null) { write-host "Dead Fish" } else { write-host "It's alive"}
Let's make a network scanner
function test-subnet {
param(
$subnetprefix = "192.168.1",
$post = (1..254)
)
$jobs = @()
foreach($p in $post) {
$jobs += @{job=(Test-Connection "$subnetprefix.$p" -ErrorAction SilentlyContinue -asjob);name="$subnetprefix.$p"}
}
while (( $jobs.job | ? { $_.State -ne "Completed"}) -ne $null) { start-sleep -seconds 2}
foreach($job in $jobs) {
$result = $job.job | Receive-Job
$responses = $result | select ResponseTime | ? { $_.ResponseTime -ne $null }
if ($responses -ne $null) {
write-host ("{0:15} is alive" -f $job.name)
}
$job.job | remove-job
}
}
test-subnet
test-subnet -subnetprefix "8.8.8" -post @(0..10)
Scheduled Job
Jobs are nicely integrated in the shell but are less flexible via the taskscheduler (taskschd.msc)
$scriptblk = {
"hello world" | set-content c:\dev\hellofromjob
}
$trigger = New-JobTrigger -Once -at (get-date).AddMinutes(1)
Register-ScheduledJob -ScriptBlock $scriptblk -name "Hello world" -trigger $trigger
wait for it
while ((test-path C:\dev\hellofromjob) -eq $false) { write-host -NoNewline ".";sleep 1 }
get-item C:\dev\hellofromjob
More control
Get-ScheduledJob -Name "hello world" | Get-ScheduledJobOption
Get-ScheduledJob -Name "hello world" | Get-ScheduledJobOption | Set-ScheduledJobOption -StartIfOnBattery
Get-ScheduledJob -Name "hello world" | Get-ScheduledJobOption
Remove
Get-ScheduledJob -Name "hello world" | Unregister-ScheduledJob
Scheduled Task
Getting ride of taskschd.msc
get-scheduledtask
makes a task in taskschd.msc
Remove-Item C:\dev\hellofromjob
$script = 'c:\dev\hello fromjob.ps1'
@'
"hello world" | set-content c:\dev\hellofromjob
'@ > $script
$trigger = New-ScheduledTaskTrigger -once -At (get-date).AddMinutes(1)
$action = New-ScheduledTaskAction -execute "powershell.exe" -Argument "-NoProfile -NonInteractive -command ""& '$script'"""
$settings = New-ScheduledTaskSettingsSet -Hidden
$schedtask = Register-ScheduledTask -Description "My Scheduled Task" -Trigger $trigger -Action $action -taskname "HelloPS" -taskpath "\CustomPS\" -Settings $settings
$schedtask
wait for it
while ((test-path C:\dev\hellofromjob) -eq $false) { write-host -NoNewline ".";sleep 1 }
get-item C:\dev\hellofromjob
remove it
Get-ScheduledTask -TaskName "HelloPS" | Unregister-ScheduledTask -confirm:$false