Invoke-CMScript
Run a PowerShell script in Configuration Manager.
Syntax
Invoke-CMScript
[-Collection <IResultObject>]
[-CollectionId <String>]
[-CollectionName <String>]
[-Device <IResultObject[]>]
-InputObject <IResultObject>
[-PassThru]
[-ScriptParameter <Hashtable>]
[-DisableWildcardHandling]
[-ForceWildcardHandling]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
Invoke-CMScript
[-Collection <IResultObject>]
[-CollectionId <String>]
[-CollectionName <String>]
[-Device <IResultObject[]>]
[-PassThru]
-ScriptGuid <String>
[-ScriptParameter <Hashtable>]
[-DisableWildcardHandling]
[-ForceWildcardHandling]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
Description
Use this cmdlet to run a PowerShell script in Configuration Manager. These scripts are integrated and managed in Configuration Manager.
You can't run a script until it's approved. To approve scripts programmatically, use the Approve-CMScript cmdlet.
For more information, see Create and run PowerShell scripts from the Configuration Manager console.
Note
Run Configuration Manager cmdlets from the Configuration Manager site drive, for example PS XYZ:\>
. For more information, see getting started.
Examples
Example 1: Run a script by using its ID
This command runs the script with ID DF8E7546-FD66-4A3D-A129-53AF5AA54F80
.
Invoke-CMScript -ScriptGuid "DF8E7546-FD66-4A3D-A129-53AF5AA54F80"
Example 2: Run a script by using an object variable
The first command gets a script object by its ID, and stores it in the $ScriptObj variable. The second command runs the script stored in that variable.
$ScriptObj = Get-CMScript -Id "DF8E7546-FD66-4A3D-A129-53AF5AA54F80"
Invoke-CMScript -InputObject $ScriptObj
Example 3: Pass parameters to the target script
The first line stores parameters in a hashtable. The second line runs the script on the target device, passing the parameters in the hashtable.
$parameters = @{
"FolderName"="c:\test\test1"
"FileName"="test2"
}
Invoke-CMScript -ScriptGuid $scriptGuid -Device (Get-CMDevice -Name $targetPCName) -ScriptParameter $parameters
Parameters
-Collection
Specify a collection object to run this script. To get this object, use the Get-CMCollection cmdlet.
Type: | IResultObject |
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-CollectionId
Specify the ID of a collection to run this script.
Type: | String |
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-CollectionName
Specify the name of a collection to run this script.
Type: | String |
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-Confirm
Prompts you for confirmation before running the cmdlet.
Type: | SwitchParameter |
Aliases: | cf |
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-Device
Specify an object for a device to run this script. To get this object, use the Get-CMDevice cmdlet.
Type: | IResultObject[] |
Aliases: | Devices |
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-DisableWildcardHandling
This parameter treats wildcard characters as literal character values. You can't combine it with ForceWildcardHandling.
Type: | SwitchParameter |
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-ForceWildcardHandling
This parameter processes wildcard characters and may lead to unexpected behavior (not recommended). You can't combine it with DisableWildcardHandling.
Type: | SwitchParameter |
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-InputObject
Specify a script object to run. To get this object, use the Get-CMScript cmdlet.
Type: | IResultObject |
Position: | Named |
Default value: | None |
Required: | True |
Accept pipeline input: | True |
Accept wildcard characters: | False |
-PassThru
Returns an object representing the item with which you're working. By default, this cmdlet may not generate any output.
Type: | SwitchParameter |
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-ScheduleTime
Specify a UTC DateTime to schedule the script's execution for a later time.
Type: | DateTime |
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-ScriptGuid
Specify the ID of the script to run. The format is a standard GUID.
Type: | String |
Position: | Named |
Default value: | None |
Required: | True |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-ScriptParameter
Applies to version 2010 and later. Use this parameter to pass parameters to the target script. Specify a hashtable with the required parameters. For an example of usage, see Examples.
Type: | Hashtable |
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-WhatIf
Shows what would happen if the cmdlet runs. The cmdlet isn't run.
Type: | SwitchParameter |
Aliases: | wi |
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
Inputs
Microsoft.ConfigurationManagement.ManagementProvider.IResultObject
Outputs
System.Object