Workflow
Parallel
Foreach –parallel
Sequence
InlineScript
Checkpoint-Workflow
Suspend-Workflow
/Resume-Workflow
workflow WF-ParallelExample
{
parallel
{
Get-CimInstance –ClassName Win32_OperatingSystem
Get-Process –Name PowerShell*
Get-CimInstance –ClassName Win32_ComputerSystem
Get-Service –Name s*
}
}
workflow WF-ForeachAndSequenceExample
{
param([string[]]$computers)
foreach –parallel ($computer in $computers)
{
sequence
{
Get-WmiObject -Class Win32_ComputerSystem -PSComputerName $computer;
$disks = Get-WmiObject -Class Win32_LogicalDisk -Filter 'DriveType = 3' –PSComputerName $computer;
foreach -parallel ($disk in $disks)
{
sequence
{
$vol = Get-WmiObject -Class Win32_Volume -Filter "DriveLetter = '$($disk.DeviceID)'" –PSComputerName $computer
Invoke-WmiMethod -Path $($vol.__PATH) -Name DefragAnalysis
}
}
}
}
}
Execute
WF-ForeachAndSequenceExample -Computers @('server01', 'server02', 'server03')
workflow foreachpitest
{
param([string[]]$computers)
foreach –parallel ($computer in $computers)
{
InlineScript
{
Get-WmiObject –Class Win32_OperatingSystem –ComputerName $using:computer | Format-List
}
}
}
Example Workflow
workflow MyWorkflow
{
$total = 0
foreach ($num in 1..10)
{
Write-Output "Adding $num to total"
$total += $num
if ($num -eq 5)
{
Write-Output "Reached checkpoint"
Checkpoint-Workflow
}
}
Write-Output "Total is $total"
}
Start the Workflow
MyWorkflow -PSComputerName 'Server01' -AsJob
Suspend the Worflow
Suspend-Workflow -Name MyWorkflow
Resume the Workflow at the Checkpoint
Resume-Workflow -Name MyWorkflow
workflow Test-WF
{
param([array[]]$ServerList)
$ReturnArr = @()
foreach -parallel -ThrottleLimit 10 ($Server in $ServerList)
{
$returnName = $Server
$strCompName = $Server.Name
$Count = InlineScript {
(Get-Eventlog Security -ComputerName $Using:returnName -Newest 4000 | Where-Object { $_.EventID -eq '4624' }).count
}
$Workflow:ReturnArr += "$returnName,$Count"
}
$ReturnArr
}
$arrComputers = (get-adcomputer -filter * -server dc1.contoso.com:3268).Name
Get-date
$Stats = Measure-Command -Expression { $OutTest = Test-WF -ServerList $arrComputers }
Get-date
$outTest
$Stats