Is there a nice way to implement the switch parameter -AsJob in custom cmdlets, like Invoke-Command has?
The only way I thought about this is:
function Use-AsJob {
[CmdletBinding()]
[OutputType()]
param (
[Parameter(Mandatory = $true)]
[string]
$Message,
[switch]
$AsJob
)
# Wrap Script block in a variable
$myScriptBlock = {
# stuff
}
if ($AsJob) {
Invoke-Command -ScriptBlock $myScriptBlock -AsJob
}
else {
Invoke-Command -ScriptBlock $myScriptBlock
}
}
Is there a better approach? I couldn't find Microsoft docs on this, any lead helps.
If we make the following assumptions:
Then you can use the following boilerplate for any command:
Now you can do either: