Using a SCOM Agent Task from the Command Line

One could start by asking: why in Zeus’s name would I want to do that. Well, here’s the scenario: imagine you have a big infrastructure of servers where you need to run a task against. However, you don’t have credentials for SCORCH to reach them. Your SCCM infrastructure is there, but there is no package and what you need can be done with a few script lines. Who could help us? SCOM to the rescue!

Luckily, your servers are monitored by SCOM, for which you have appropriate credentials to run tasks in all or most of them. You could just go ahead and create the task in the SCOM console. Now, you want to leverage those credentials from another platform. Again, how? HOW!?!

Powershell and SCOM!

Here’s a very simple example.

I have created a simple task which runs a small piece of vbscript:

set oArgs=wscript.Arguments

if oArgs.count >0 then

Mydata=oArgs(0)

else

Mydata=”Empty arguments!”

end if

Set objFileToWrite = CreateObject(“Scripting.FileSystemObject”).OpenTextFile(“C:\listfile.txt”,2,true)

objFileToWrite.WriteLine(Mydata)

objFileToWrite.Close

Set objFileToWrite = Nothing

Wscript.echo Mydata

 

I have targeted it to Windows Computer, to save some time.

Now, if I run it from the console, I should see a file in the root of whichever computer I picked from the list:

You can then override and the Arguments Override will be passed on to the script:

 

In my case, I had to use another account, since I can’t write to the c:\ drive with my regular account:

Now, to run this from powershell, you will need a few things:

  • A computer instance
  • An Override
  • A credential
  • A task

You can get a computer like this:

$computer=get-scomclass -DisplayName “Windows Computer” | Get-SCOMClassInstance | ? {$_.DisplayName -eq “SCOM.fehse.corp”}

For the override:

$Override=@{Arguments='””My Overriden Parameter””‘}

A credential: (from the console):

$cred = get-credential

And running the whole thing:

get-scomtask | where {$_.DisplayName -Match “Test”} | Start-SCOMTask $computer -TaskCredentials $cred -Override $Override

Your file should be like this:

So, as a last step, I may want to get some information from the script I’m running, so, the task mechanism will return any std output as a result. To capture the info, you can use the get-scomtaskresult and leverage the batchId number returned previously:

Now:

Data is a bit buried in the Output format, but it is there!

Hope this helps!