Link to home
Start Free TrialLog in
Avatar of Ron Shorts
Ron ShortsFlag for United States of America

asked on

Powershell script help with error handling, sccm queries

Trying to accomplish the following, oBdA provided me with the process script, but I'd like to build on it.  If anyone can help, it would be greatly appreciated!

Have a powershell script that can be called from a vbscript and pass the parameter defined in the vbscript to the powershell script. In vbscript, I can call with this code, but not sure how to pass the parameter (computer) for example defined in vbscript...
'Set objShell = CreateObject("Wscript.Shell")
'Objshell.Run("powershell.exe -noexit .\test.ps1")

Open in new window

Powershell Script I'd like to do a few things:
• Check if the computer is online or offline.
• if computer is is offline, exit and send an email
• if the computer is online, run the below script that will check for running processes.
• if the below has anything other than return code 0 (meaning process is still running), have send email
• if the below has a return code of 0 (meaning no processing running), run an executable and invoke some SCCM commands.
 $computerName = 'RemoteMachine' ## Set to $null to run locally
$processList = @'
    "Name",             "Expected",     "Running"
    "cmd",              "1",            "0"
    "powershell",       "1",            "0"
    "OfficeClickToRun", "2",            "0"
'@ | ConvertFrom-Csv | ForEach-Object {$_.Expected = [int]$_.Expected; $_}

$splat = @{}
If ($computerName) {
    $splat['ComputerName'] = $computerName
    Write-Host "Testing processes on $($computerName)" -ForegroundColor Yellow
}
$cursorTop = [console]::cursorTop + 1
Do  {
    $processList | ForEach-Object {
        $_.Running = @(Get-Process $_.Name @splat -ErrorAction SilentlyContinue).Count
    }
    [console]::SetCursorPosition(0, $cursorTop)
    ($processList | Format-Table -AutoSize | Out-String).Split("`r`n", [StringSplitOptions]::RemoveEmptyEntries) | Write-Host
    If ($running = @($processList | Where-Object {$_.Running -ge $_.Expected}).Count) {
        Start-Sleep -Seconds 5
    }
} Until (-not $running)

return 0 

Open in new window

 
• SCCM commands I haven't worked with before, but looking for any failed updates or pending updates. Looking to have some sort of reporting or audit for these (send email) and invoke commands to trigger install. Below are some PS commands I found for these:

If you are interested in seeing which updates are missing:
get-wmiobject -query "SELECT * FROM CCM_SoftwareUpdate WHERE ComplianceState = 0" -namespace "ROOT\ccm\ClientSDK"

Open in new window

If you would like to see if there are updates applying, if true they are running:
$CCMUpdate = get-wmiobject -query "SELECT * FROM CCM_SoftwareUpdate" -namespace "ROOT\ccm\ClientSDK"
if(@($CCMUpdate | where { $_.EvaluationState -eq 2 -or $_.EvaluationState -eq 3 -or $_.EvaluationState -eq 4 -or $_.EvaluationState -eq 5 -or $_.EvaluationState -eq 6 -or $_.EvaluationState -eq 7 -or $_.EvaluationState -eq 11 }).length -ne 0) { $SCCMUpdate = $true } else {$SCCMUpdate = $false }

Open in new window

Here is a nice addition if you only want to install specific update(s) you just have to modify the select statement:
([wmiclass]'ROOT\ccm\ClientSDK:CCM_SoftwareUpdatesManager').InstallUpdates([System.Management.ManagementObject[]] (get-wmiobject -query 'SELECT * FROM CCM_SoftwareUpdate' -namespace 'ROOT\ccm\ClientSDK'))

Open in new window

Trigger install of all updates:
([wmiclass]'ROOT\ccm\ClientSDK:CCM_SoftwareUpdatesManager').InstallUpdates()

Open in new window

Triggering an update scan on a client:
([wmiclass]'ROOT\ccm:SMS_Client').TriggerSchedule('{00000000-0000-0000-0000-000000000113}')

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial