Friday, January 12, 2018

Powershell and jobs

Powershell jobs are a bit of blunt instrument.

Here's two options for sending inputs for them
Scenario 1: Sending a string with all of our values and splitting them to an array when inside the scriptblock.
$inputString = "text1;text2;text3"
start-job -ArgumentList $inputString -ScriptBlock {
$listItems = @($args[0]).split(";")
write-output "I got $($listItems.count) items with me..."
foreach ($listItem in $listItems) {
write-output "Item: $listItem"
}
} -Name "Test"
get-job |where-object {$_.name -like "Test"} |receive-job -AutoRemoveJob -wait


Scenario2: Sending an object as input. Handy for configs and saves the need to define variablenames.
$ScriptPath = $(split-path -Parent $MyInvocation.MyCommand.Definition)
$inputs = new-object PSObject -Property @{url="Test48"; State="Added";scriptPath=$ScriptPath}
start-job -InputObject $inputs -ScriptBlock {
$data = @($input)
write-output "My output: $($data)"
write-output "url: $($data.url)"
write-output "state: $($data.state)"
write-output "scriptpath: $($data.scriptpath)"
} -Name "Test"
get-job |where-object {$_.name -like "Test"} |receive-job -AutoRemoveJob -wait

No comments:

Powershell and Uptimerobot

Uptimerobot can be quite tedious when you need to update many monitors at once. For example say you bought the license for Uptimerobot and n...