[PowerShell][Example]: Deferred workflows
So your creating a script, lets say that if it fails you want it to take some actions and you want all of the repair steps to come at the end. Or lets say you have a series of tasks, you want to let the user setup what is run before execution. Whatever your scenario wouldn't it be nice to just have an array of functions that you want to run and execute them? Well if your asking yourself that question then your not familiar with PowerShell and the & operator. Creating a variable with a function name and calling it with an & will allow you to run that function. Here's an example below:
function collectComputerInfo{
$machineInfo = @($env:ComputerName,$env:HomeDrive,$env:OS,$env:SystemRoot,$env:Processor_Architecture,$env:Processor_Identifier)
return $machineInfo
}
function collectUserInfo{
$userInfo = @($env:SESSIONNAME,$env:USERNAME,$env:USERPROFILE)
return $userInfo
}
function runWorkflows{
param($workflow)
foreach($task in $workflow){
$rslts = &$task
foreach ($rslt in $rslts){
Write-Host $rslt
}
}
}
function main{
param(
[Parameter(Mandatory=$true)]
[ValidateSet('computer','user','both')]
[String] $info = "both"
)
$workflowList = @()
switch($info){
user{
$workflowList += "collectUserInfo"
}
computer{
$workflowList += "collectComputerInfo"
}
both{
$workflowList += "collectUserInfo"
$workflowList += "collectComputerInfo"
}
}
runWorkflows $workflowList
}
Incase you missed it, the call is in the runWorkflows function.
$rslts = &$task
And there you have it. pretty simple. While I recognize this example may not be a good or even appropriate example, it's an example so figure out how you'll want to use it.