Skip to content

Instantly share code, notes, and snippets.

@aaronpmiller
Created March 3, 2015 06:06
Show Gist options
  • Save aaronpmiller/c8fcee26c07def948725 to your computer and use it in GitHub Desktop.
Save aaronpmiller/c8fcee26c07def948725 to your computer and use it in GitHub Desktop.
Method to raise status messages with SCCM
Function New-SMSConnection {
PARAM(
[Parameter(Mandatory=$true)][string]$siteServer,
[Parameter(Mandatory=$true)][string]$siteCode
)
$smsNamespace = "root\sms\site_$siteCode"
$Locator = New-Object -com "WbemScripting.SWbemLocator"
$Locator.Security_.AuthenticationLevel = 6
$Connection = $Locator.ConnectServer($siteServer, $smsNamespace)
Return $Connection
}
Function New-SMSStatusMsg {
[CmdletBinding()]
PARAM(
[Parameter(Mandatory=$true)][string]$MessageText,
[Parameter(Mandatory=$false)][UInt32][ValidateSet(256,512,768,1024)]$MessageType=768,
[Parameter(Mandatory=$false)][UInt32]$Win32Error,
[Parameter(Mandatory=$false)][UInt32]$ProcessID,
[Parameter(Mandatory=$false)][UInt32]$ThreadID,
[Parameter(Mandatory=$false)][DateTime]$Time,
[Parameter(Mandatory=$false)][UInt32[]]$AttrIDs,
[Parameter(Mandatory=$false)][string[]]$AttrValues,
[Parameter(Mandatory=$false)][string]$TopLevelSiteCode,
[Parameter(Mandatory=$false)][string][ValidateSet('Informational','Error','Warning')]$Type='Informational',
[Parameter(Mandatory=$false)][UInt32]$LocaleID=1033,
[Parameter(Mandatory=$true)][string]$MachineName,
[Parameter(Mandatory=$true)][string]$ApplicationName,
[Parameter(Mandatory=$true)][__ComObject]$Connection
)
$Context = New-Object -com WbemScripting.SWbemNamedValueSet
$Context.Add("LocaleID", "MS\$LocaleID") | Out-Null
$Context.Add("MachineName", "$MachineName") | Out-Null
$Context.Add("ApplicationName", "$ApplicationName") | Out-Null
#Obtain an instance of the class using a key property value.
$Class = $Connection.Get("SMS_StatusMessage")
#Obtain an InParameters object specific to the method.
$Inparams = $Class.Methods_.item("Raise$($Type)StatusMsg").Inparameters.SpawnInstance_()
#Add the mandatory input parameters.
$Inparams.Properties_.item("MessageText").value = $MessageText
$Inparams.Properties_.item("MessageType").value = $MessageType
#Add the optional input parameters.
if ($Win32Error) {$Inparams.Properties_.item("Win32Error").value = $Win32Error}
if ($ProcessID) {$Inparams.Properties_.item("ProcessID").value = $ProcessID}
if ($ThreadID) {$Inparams.Properties_.item("ThreadID").value = $ThreadID}
if ($Time) {$Inparams.Properties_.item("Time").value = $Time}
if ($AttrIDs) {$Inparams.Properties_.item("AttrIDs").value = $AttrIDs}
if ($AttrValues) {$Inparams.Properties_.item("AttrValues").value = $AttrValues}
if ($TopLevelSiteCode) {$Inparams.Properties_.item("TopLevelSiteCode").value = $TopLevelSiteCode}
#Execute the method. Out Parameter is Created.
$Outparams = $Connection.ExecMethod("SMS_StatusMessage","Raise$($Type)StatusMsg",$Inparams,$null,$Context)
}
$Connection = New-SMSConnection -siteServer 'siteServer.abc.example.com' -siteCode 'ABC'
New-SMSStatusMsg -MessageText 'Custom message text' -Type Informational -MachineName 'abc012345' -ApplicationName 'ABC-CustomApplication' -Connection $Connection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment