Invoke-PSCommandWithCallOperator.ps1
<#PSScriptInfo
.VERSION 1.0 .GUID 1c77f9c5-dbfa-47f8-917e-88ccd6533a47 .AUTHOR Azure Automation Team .COMPANYNAME Microsoft .COPYRIGHT .TAGS Azure Automation .LICENSEURI .PROJECTURI .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES #> <# .SYNOPSIS Runs a PowerShell command, with or without parameters. .DESCRIPTION This runbook will run a PowerShell command, with or without parameters, using the PowerShell call operator. In Azure Automation, this runbook is useful for running a PowerShell command on a hybrid runbook worker. .PARAMETER Command Required The name of the PowerShell command to run .PARAMETER Parameters Optional Parameters for the command. Format the value with JSON like this: '{"ComputerName":"localhost","Name":"lsass","Verbose":true}' .EXAMPLE .\Invoke-PSCommandWithCallOperator -Command "Get-Process" -Parameters '{"ComputerName":"localhost","Name":"lsass","Verbose":true}' .NOTES AUTHOR: Azure Automation Team LASTEDIT: 2016-11-8 #> # Returns whatever object(s) the invoked command returns [OutputType([object])] param ( [Parameter(Mandatory=$true)] [string] $Command, [Parameter(Mandatory=$false)] [string] $Parameters ) if ([string]::IsNullOrEmpty($Parameters)) { & $Command } else { # Convert parameters from json into hash table so they can be passed to the command $Params = @{} $ParamValues = ConvertFrom-Json $Parameters foreach ($Param in $ParamValues.PSObject.Properties) { $Params.Add($Param.Name,$Param.Value) } & $Command @Params } |