# State type constants
$STATE_SUCCESS = "Success"
$STATE_WARNING = "Warning"
$STATE_ERROR = "Error"
$momAPI = new-object -comObject MOM.ScriptAPI
# Event ID or Messages
$LOCAL_SYSTEM_SRC = "CheckRunningLocalSystem"
$LOCAL_SYSTEM_ID = 104
# Checks if the script is running under local system account
function CheckRunningLocalSystem
{
$UserWindowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$UserSecurityIdentifier = $UserWindowsIdentity.User
$UserSID = $UserSecurityIdentifier.Value
$UserName = $UserWindowsIdentity.Name
Write-Verbose "The powershell module is running as $UserName"
if ($UserSID -ne "S-1-5-18")
{
$momAPI.LogScriptEvent($LOCAL_SYSTEM_SRC, $LOCAL_SYSTEM_ID, $EVENT_TYPE_ERROR, $SCRIPT_NAME)
Write-Warning "The powershell module is not running as Local System Account"
Exit
}
}
# Enter condition is to have set all four variables: ObjectName CounterName InstanceName and Value
function CreatePerformancePropertyBag
{
param ($ObjectName = $ObjectName, $CounterName = $CounterName, $InstanceName = $InstanceName, $Value = $Value)
if ($ObjectName -eq $null -or $CounterName -eq $null -or $InstanceName -eq $null -or $Value -eq $null)
{
Write-Warning "$SCRIPT_NAME - Performance property is not properly defined"
return
}
if ($PerformancePropertyBag -eq $null)
{
Write-Warning "$SCRIPT_NAME - Unable to create performance property bag"
return
}
$PerformancePropertyBag.AddValue('ObjectName', $ObjectName)
Write-Verbose "$SCRIPT_NAME - added performance property bag value for object name $ObjectName"
$PerformancePropertyBag.AddValue('CounterName', $CounterName)
Write-Verbose "$SCRIPT_NAME - added performance property bag value for counter name $CounterName"
$PerformancePropertyBag.AddValue('InstanceName', $InstanceName)
Write-Verbose "$SCRIPT_NAME - added performance property bag value for instance name $InstanceName"
$PerformancePropertyBag.AddValue('Value', $Value)
Write-Verbose "$SCRIPT_NAME - added performance property bag value for value $Value"
# submitting performance property bag
$PerformancePropertyBag
Write-Host "$SCRIPT_NAME - performance property bag returned"
}
# Enter condition is to have set StateProperty variable and Message variable is optional when STATE_SUCCESS
function CreateStatePropertyBag
{
param ($StateProperty = $StateProperty, $Message = $Message)
if ($StateProperty -ne $STATE_SUCCESS -and $StateProperty -ne $STATE_WARNING -and $StateProperty -ne $STATE_ERROR)
{
Write-Warning "$SCRIPT_NAME - State property is not properly defined: $StateProperty"
return
}
if ($StatePropertyBag -eq $null)
{
Write-Warning "$SCRIPT_NAME - Unable to create state property bag"
return
}
$StatePropertyBag.AddValue('State', $StateProperty)
Write-Verbose "$SCRIPT_NAME - added state property bag value for state $StateProperty"
if ($Message -ne $null)
{
$StatePropertyBag.AddValue('Message', $Message)
Write-Verbose "$SCRIPT_NAME - added state property bag value for message $Message"
}
# Submitting state property bag
$StatePropertyBag
Write-Host "$SCRIPT_NAME - state property bag returned"
}
CheckRunningLocalSystem
Write-Host "$SCRIPT_NAME - Executing Test-ImapConnectivity to $MailboxServer"