Note

Access to this page requires authorization. You can try signing in or .

Access to this page requires authorization. You can try .

Invoke-CMScript

Run a PowerShell script in Configuration Manager.

Syntax

ByInputObject

Invoke-CMScript
 -InputObject <IResultObject>
 [-Collection <IResultObject>]
 [-CollectionId <String>]
 [-CollectionName <String>]
 [-Device <IResultObject[]>]
 [-PassThru]
 [-ScriptParameter <Hashtable>]
 [-DisableWildcardHandling]
 [-ForceWildcardHandling]
 [-WhatIf]
 [-Confirm]
 [<CommonParameters>]

ByGuid

Invoke-CMScript
 -ScriptGuid <String>
 [-Collection <IResultObject>]
 [-CollectionId <String>]
 [-CollectionName <String>]
 [-Device <IResultObject[]>]
 [-PassThru]
 [-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.

Parameter properties

Type:IResultObject
Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

-CollectionId

Specify the ID of a collection to run this script.

Parameter properties

Type:String
Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

-CollectionName

Specify the name of a collection to run this script.

Parameter properties

Type:String
Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

-Confirm

Prompts you for confirmation before running the cmdlet.

Parameter properties

Type:SwitchParameter
Default value:None
Supports wildcards:False
DontShow:False
Aliases:cf

Parameter sets

-Device

Specify an object for a device to run this script. To get this object, use the Get-CMDevice cmdlet.

Parameter properties

Type:

IResultObject[]

Default value:None
Supports wildcards:False
DontShow:False
Aliases:Devices

Parameter sets

-DisableWildcardHandling

This parameter treats wildcard characters as literal character values. You can't combine it with ForceWildcardHandling.

Parameter properties

Type:SwitchParameter
Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

-ForceWildcardHandling

This parameter processes wildcard characters and may lead to unexpected behavior (not recommended). You can't combine it with DisableWildcardHandling.

Parameter properties

Type:SwitchParameter
Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

-InputObject

Specify a script object to run. To get this object, use the Get-CMScript cmdlet.

Parameter properties

Type:IResultObject
Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

-PassThru

Returns an object representing the item with which you're working. By default, this cmdlet may not generate any output.

Parameter properties

Type:SwitchParameter
Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

-ScheduleTime

Specify a UTC DateTime to schedule the script's execution for a later time.

Parameter properties

Type:DateTime
Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

-ScriptGuid

Specify the ID of the script to run. The format is a standard GUID.

Parameter properties

Type:String
Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

-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.

Parameter properties

Type:Hashtable
Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

-WhatIf

Shows what would happen if the cmdlet runs. The cmdlet isn't run.

Parameter properties

Type:SwitchParameter
Default value:None
Supports wildcards:False
DontShow:False
Aliases:wi

Parameter sets

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutBuffer, -OutVariable, -PipelineVariable, -ProgressAction, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

Inputs

Microsoft.ConfigurationManagement.ManagementProvider.IResultObject

Outputs

System.Object

Related Links


Feedback

Was this page helpful?