PowerShell implementing -AsJob for a cmdlet
问题 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