Wednesday 30 March 2011

3 useful SCOM powershell reports

Often one wants quick access to a list of agents in SCOM or perhaps a report of open alerts.


In our SCOM setup it takes what sometimes seems an eternity to open the console so here's a quicker way.


It's not very obvious with System Center Operations Manager / SCOM how to extract a list of agents so here are three useful powershell queries to export data with the first example being a list of agent objects with just key columns of data.
These examples make use of the Select-Object and Sort-Object commandlets


########### GET AGENTS / AGENT LIST#######################
# export a list of agents sorted by name and showing only key columns of data
$title="AgentList"
$Body = get-agent | select Name,DisplayName,PrimaryManagementServerName,HealthState,Domain,IPAddress,Version,InstallTime,InstalledBy,ProxyingEnabled | sort-object Name
$Body | ConvertTo-Html | set-content c:\temp\$Title.htm




############ OPEN Alerts ########################
# export a list of alerts sorted by LastmodifiedDate desc and showing only key columns
# do as csv because typically massive
$title="OpenAlertsByUTCDateModDesc"
$Body =get-alert -criteria 'ResolutionState = "0"' | select Id,Name,Priority,Severity,TimeRaised,LastModified,MonitoringObjectDisplayName,MonitoringObjectPath,Category,Description,MonitoringObjectId,MonitoringClassId,IsMonitorAlert | sort-object LastModified -descending
$body | Export-Csv c:\temp\$Title.csv




########## PENDING ACTIONS ##################
# export a list of pending actions (agents waiting to be approved in Pending management screen)
$title="PendingActions"
$Body = get-agentpendingaction | select agentname,AgentPendingActionType,ManagementServerName,LastModified | sort-object agentname
$Body | ConvertTo-Html | set-content c:\temp\$Title.htm

Monday 28 March 2011

Attack of ^M aka carriadge return (CR) in vbscript

I spent a lot of time nailing an obscure vbscript problem. I was using a function to split a command output using vbscript regular expression object. the bizarre problem I encountered was that because the command output was many lines long it contained vbcrlf characters as the last 2 chr of the line.

before splitting the string into the ObjRE.Submatches collection the whole string appeared as you would expect
xxxx vbcrlf
yyyyy vbcrlf
...

post splitting using a pattern and the 'multiline' property I ended up with each submatch collection object having a hidden cr charater last character which in vi appears as ^M. in my other text editor (editplus) it said it was chr 0A

I tried everything I could think of to replace or even 'instr' the chr using various possiblites (chr 10, chr 13, vbcrlf) etc. but vbscript just ignores it and the cr remains annoyingly embedded in the string just itching to cause trouble later on...

eventually the only fix I could find was to trim the last char from each submatch item e.g
  For Each submatch In oMatch.Submatches
   if len(submatch)=0 or isnull(submatch) then
  else
      retstr = retstr & Left(submatch,Len(submatch)-1) 

this seems a dumb way to do this but vbscript just won't seem to handle the single cr character consistently.