In this blog, I am going to demonstrate you how to create and run PowerShell scripts through SCCM. Benefit of deploying scripts through SCCM console is that, you can deploy it instantly and the results will be pretty much live.

Benefits of using PowerShell scripts feature

You can create the script on the fly and deploy it instantly to a system. Hence, no need of creating any package / application and avoid deploying it. As we are not creating package, no need of taking care of distributing on distribution points.

Enable Feature “Create and run scripts”

Before running this feature, make sure you have enabled it through Update and Servicing features. Navigate to \Administration\Overview\Updates and Servicing\Features and on right pane look for “Create and run scripts”. Make sure Status to show as Yes or else right click and select Turn on.

RunScripts 01

Setting up of Script Approver

It is always a best practice that you need to have another person set as approver. If the person creating the script approves the script himself/herself, that won’t looks good as there are chances of human error. If there is another layer of check, wherein another person is checking the script before approving it makes complete sense of verifying it before anyone else can use the script.

Navigate to \Administration\Overview\Site Configuration\Sites, select site on right Pane. From the Ribbon select Hierarchy Settings. Under General tab, check the box “Script authors require additional script approver”.

RunScripts 02

Ready to create the script

We are ready to deploy the script. Navigate to \Software Library\Overview\Scripts. From the Ribbon, select Create Script.

RunScripts 03

As a sample, lets create a script to check disk space on a system. Name it as Check Free Disk Space, under script window, copy / paste following script:

$free=(get-wmiobject -class win32_logicaldisk | Where-Object {$_.DeviceID -eq 'C:'} | select -expandproperty freespace)/1GB
Write-host "Free Space available:" $freespace " GB"
RunScripts 04

Confirm the details, and click Next.

You will see the page The Create Script completed successfully

RunScripts 05

Once script is created, you will see the script under the Pane with Approval State showing as Waiting for approval.

RunScripts 06

Under Ribbon, Approve/Deny will be greyed out as I can’t approve my own script with same user ID.

Login to the server with different user ID, navigate to \Software Library\Overview\Scripts. We can see the script named Check Free Disk Space. From the Ribbon, where Approve/Deny option has now become available to run. Click on Approve/Deny.

Under Script Details page, verify the script and click Next.

Under Script Approval page, you have option to approve or deny. As the script looks ok, select Approve and click Next.

Confirm the details and click Next.

We can now see the script Approval State as Yes, and Approver name showing who has approved the script. Author and Approver both are different, that what we expected.

RunScripts 07

Run Script on a collection / or system

We can run the script on a collection or on individual system. Navigate to \Assets and Compliance\Overview\Device Collections. Select any collection, lets say VDI’s. Right click the collection and select Run Script.

RunScripts 08

Under Select script to run, select the existing script ie. Check Free Disk Space and click Next.

RunScripts 09

Confirm the script details before running the script and click Next.

Allow some time to complete the job, you can close also at this moment to check the status at later stage.

RunScripts 10

Once completed, you can results in different format ie. Bar Chart.

RunScripts 11

Click on Run Details tab on the bottom to see system name along with free disk space available.

RunScripts 12

You can check the Script status at later stage also by navigating to \Monitoring\Overview\Script Status. Script name will be displayed along with Last Update Time, collection name and other info. Double click it to get the same details I showed you previously.