Wednesday 26 September 2007

OpsMgr R2 allows you to embed powershell scripts in a MP - See this MP for an example - http://derekhar.blogspot.com/2009/11/new-agent-maintenance-mode.html

----------

If you want to run powershell, perl or any other scripts not run by cscript.exe from a management pack then you invariably had to create the script and distribute it across all the servers where it needed to run. This could easily become quite complex maintaining all these scripts in multiple different locations. If you are in a situation where you have multiple management groups and none of them are quite the same then you could have multiple different versions of the script and the rule that would call the script.

To avoid this problem it is now possible to embed other scripts in a management pack, not just vbscript and jscript.

To explain how this is done we need to break in to the XML. Open the image below to view the full command. This would be executed using on a recurring basis using a System.Scheduler datasource.

The Write Action aboves enables proxying for all management servers. This check is quite easy to do using the command shell alias.

This line launches the Powershell exe. Remember that Powershell must be installed on any server you are targeting the rule at. The same goes for any other script language that you wish to use.

<WorkingDirectory>%MOMROOT%</WorkingDirectory>

The %MOMROOT% variable point to the Installation path of OpsMgr. This is useful when targeting Powershell scripts at the RMS or Management servers as it allows you to easily load the Command Shell Snap-in. This then gives access to all the Command Shell aliases.

<CommandLine>-Command "&amp; '$File/EnableProxy.ps1$'"</CommandLine>

The arguments being passed specifies to run the script $File/EnableProxy.ps1$. After this the file must be entered.

<Files><File><Name>EnableProxy.ps1</Name><Contents>

The name of the script must match the command line name. While I have not tested this there appears to be the ability to embed multiple files here.

Now we drop into the script itself. This rule is targeted to run only on the RMS so $rootMS is set as localhost. The next 3 lines load the Command Shell snapin, and opens the connection to the local management group.

I have not tried it but I assume that returning the data back to OpsMgr would be performed using the MOM.ScriptAPI com object so if you are able to utilize the the object from the scripting language you are using then I don't think you would have a problem.

For example in Powershell you would use $momapi = new-object -comobject MOM.ScriptAPI.

I tried this out but could not get it to work.What I did was create a write action module with overrides that contained the script.I then created a rule taht used that module but I could not get the override passed in correctly. I think it has something to do with the $ character being used for variables in powershell and for override parameters. There may be an escape sequence but I do not have th etime right now to look at this right now.

I have posted the currrent MP at http://cid-397bb61b75cc76c5.skydrive.live.com/self.aspx/Public/PowershellScriptOverrides.xml if you want to give it a shot.

I was able to use your MP. However, now I have rule that collects information based on what values my powershell script outputs to the MOMScript.API.

Now I imagine I need a monitor to assess this output that is being collected. Any thoughts on how this would be done? I just need an example of a monitor and how it would relate to the rule running a powershell script.

Just wanted to add more info to my previous questions.1. I made sure that system that generates alerts has all required snapins.2. Instead of localhost , I changed to my managementnode serverin the script.3.I also run the script lines manually from(powershell v1.0) the system that is generating alert.

the permissions I was referring to was the account being used time run the script. I think to make this change the user running the script must be a member of the operations manager administrators group.

By default in opsmgr the default action account for an agent is the local system account. You could workarpund this using a run as account in the mp

Now, I am trying to run some SCVMM commands remotedly from recovery section. I have installed Adminconsole of SCVMM.I can launch the powershell which comes with SCVMM and execute scripts remotedly successfuly.

The problem I have is, I launch the powershell (installed from windows2008 server )and try adding VMM snapin. But I receive the message VMM snap in not found.

Not, sure what I am missing to execute SCVMM commands from recovery section.

Now, I am trying to run some SCVMM commands remotedly from recovery section. I have installed Adminconsole of SCVMM.I can launch the powershell which comes with SCVMM and execute scripts remotedly successfuly.

The problem I have is, I launch the powershell (installed from windows2008 server )and try adding VMM snapin. But I receive the message VMM snap in not found.

Not, sure what I am missing to execute SCVMM commands from recovery section.

1. I have a SCVMM , SCOM on a single management node server.2.Have a cluster with 5 hosts , server1,server2,server3,server4 etc in the cluster(Hyper v enabled)

3. On selecting implement button on PRO tip window in SCVMM, The powershell started executing on server2 server but it failed to connect to SCVMM server remotely and hence failed.

I could successfully connect remotely to SCVMM server from server2 by manually executing powershell commands.

Note: I have SCVMM admin console on server 2 and snap in is also present.

Questions1. Why does powershell executing on server2?2. Why not it is running on Management server(SCVMM)?3. What should be done to make power shell scripts to execute on management server regardless of which host server is expereincing problems.4. how do I fix this problem.?