SCOM Command Shell Automation Query Issue
Home  » SCOM Command Shell Automation Query Issue

SCOM Command Shell Automation Query Issue
Posted: Fri, Jun 19, 2009 8:59 AM :: Rank: 12
Author
Points: 42718
Level: System Center Expert

I want to do some specific reporting using the Command Shell that would reveal information about monitoring targeted to various classes, but this code sample provided by Johathan Almquist does not seem to work for me as advertised. I keep getting errors where he is casting the data type. Can someone else insert a task name and run this to see if it works for you? 

foreach ($targetId in get-task | where {$_.displayname -match "task name"} | foreach-object {$_.get_target()}) 
    {get-monitoringclass | where {$_.id -eq $class.id.tostring()} | select displayname}

 

Here's the original URL where I found the sample 
 
The specific error message I get is "you cannot call a method on a null-valued expression"
Anyone who can help me validate this is a code issue and not my two SCOM environments would be appreciated.

 

   Report Abuse
Re: SCOM Command Shell Automation Query Issue
Posted: Sat, Jun 20, 2009 7:49 AM :: Rank: 53
Author
Points: 6007
Level: System Center Specialist

Looks like $class needs to be set to a value first.

   Report Abuse
Re: SCOM Command Shell Automation Query Issue
Posted: Sat, Jun 20, 2009 10:33 AM :: Rank: 62
Author
Points: 42718
Level: System Center Expert

 Thanks Brian, you may be right. The article where I retreived this snippet implies it should work as is, but frankly the Powershell he's doing casting from string is a bit over my head. I may have to go to ground and build from scratch.

   Report Abuse
Re: SCOM Command Shell Automation Query Issue
Posted: Sun, Jun 21, 2009 5:35 AM :: Rank: 59
Author
Points: 1183
Level: System Center Specialist
I posted a comment on Jonathan's blog linking here to let him know that snippet is buggy...
   Report Abuse
Re: SCOM Command Shell Automation Query Issue
Posted: Sun, Jun 21, 2009 8:10 AM :: Rank: 54
Author
Points: 70
Level: System Center Enthusiast

I've lost access to all of my VMs.  The key here may also be this part:

foeach($targetId...

A variable is declared, but not used later.

In addition, as already mentioned, $class isn't defined either.

   Report Abuse
Re: SCOM Command Shell Automation Query Issue
Posted: Sun, Jun 21, 2009 11:59 AM :: Rank: 58
Author
Points: 30
Level: System Center Enthusiast
I'm not sure what I was thinking when I posted that. Must have been a late night? I posted the working scipt a few minutes ago. Thanks for catching this! -Jonathan foreach ($targetId in get-task | where {$_.displayname -match "task_name"} | foreach-object {$_.get_target().id.tostring()}) {get-monitoringclass | where {$_.id -eq $targetId} | select displayname}
   Report Abuse
Re: SCOM Command Shell Automation Query Issue
Posted: Mon, Jun 22, 2009 6:36 AM :: Rank: 81
Author
Points: 42718
Level: System Center Expert

 This Powershell snippet returns the target of the task perfectly! This is truly not even what I wanted the snippet for, but your example is valuable in exposing how to cast the data type for a script I need to put together for some batch processing. I'll post my results to my blog here when finished.

Thank you all for your assistance!

   Report Abuse

Home  » SCOM Command Shell Automation Query Issue
Top Contributors
Featured Members
Pete Zerger
Points: 65502
Level: System Center Expert
Tommy Gunn
Points: 42718
Level: System Center Expert
Simon Skinner
Points: 40744
Level: System Center Expert
Stefan Koell
Points: 28999
Level: System Center Expert
Andreas Zuckerhut
Points: 27584
Level: System Center Expert