SCCM Run Script Examples

One of my favorite, if not my favorite, features of SCCM current branch is the ability to run PowerShell scripts against a system or collection from within the console. You just have to create a new script in the console and, when you run it against a system or collection, the script runs on the system(s) locally. This is a great way to automate some of those routine tasks you may have.
I am not going to go into how to create or use the Run Scripts feature. You can find the Microsoft documentation on the feature at https://docs.microsoft.com/en-us/sccm/apps/deploy-use/create-deploy-scripts. What I am going to do is to share a few of the scripts I have put together. These are simple scripts but ones that could be helpful.

Check Service Status (prompts for service name, not service display name)

Param(
[Parameter(Mandatory=$True)]
[string]$name
)
$srvstat = get-service $name | Select-Object -ExpandProperty status
if ($srvstat -ne $null )
{$srvstat}
else {"File Not Found"}

Check for installed KB (prompts for KB number minus the KB prefix))

Param(
[Parameter(Mandatory=$True)]
[string]$KBfind
)
Get-HotFix -id $KBfind -ErrorAction SilentlyContinue | select -ExpandProperty InstalledOn

Restart a service (prompts for service name, not service display name)

Param(
[Parameter(Mandatory=$True)]
[string]$name
)

$srvstat =  get-service $name | Select-Object -ExpandProperty status

if ($srvstat -ne $null )
{
Restart-Service $name -force
"$name restarting"
}
else {"$name Not Found"}

Send pop-up message to system (prompts for message)

Param(
[Parameter(Mandatory=$True)]
[string]$msg
)
msg * "$msg"

Check pending reboot reason

$msg = "No reboot required"
if (Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending" -EA Ignore) { $msg = "Component Based Servicing" }
if (Get-Item "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired" -EA Ignore) { $msg = "Windows Update" }
if (Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager" -Name PendingFileRenameOperations -EA Ignore) { $msg = "File Rename" }
try {
$util = [wmiclass]"\\.\root\ccm\clientsdk:CCM_ClientUtilities"
$status = $util.DetermineIfRebootPending()
if(($status -ne $null) -and $status.RebootPending){
$msg = "SCCM Related"
}
}catch{}
$msg

2 thoughts on “SCCM Run Script Examples”

  1. “Restart a service” and “Check for installed KB” are the same.

    Param(
    [Parameter(Mandatory=$True)]
    [string]$service
    )
    restart-service $service -PassThru

    maybe like this?

    1. Thanks for that. I copied the wrong snippet of code. I changed it to what I am using which is just an expansion of my check service status code. Similar to what you suggested.

Comments are closed.