Run Powershell Scripts without configuring RMS Name every time

I frequently run Powershell Scripts both as run once and as SCOM Command Based rules. For the Scheduled rules I store in a Un-Sealed MP and to sync the MP across multiple Management Groups, depending on size of your estate this can be many. So the Powershell Script usually starts something like this
#——-Variables———-
#   RMS Name
$RMS=”RMS Cluster/Server Name”

# — SET UP OpsMgr COMMAND SHELL —
if ((Get-PSSnapin | Where-Object {$_.Name -eq ‘Microsoft.EnterpriseManagement.OperationsManager.Client’}) -eq $null) {
Add-PSSnapin Microsoft.EnterpriseManagement.OperationsManager.Client -ErrorAction SilentlyContinue -ErrorVariable Err
if ($Err) { $(throw write-host $Err) }
}
if ((Get-ManagementGroupConnection | Where-Object {$_.ManagementServerName -eq $RMS}) -eq $null) {
Debug-Output “Connecting to Management Server: $RMS”
New-ManagementGroupConnection $RMS -ErrorAction SilentlyContinue -ErrorVariable Err |out-null
if ($Err) { $(throw write-host $Err) }
}
if ((Get-PSDrive | Where-Object {$_.Name -eq ‘Monitoring’}) -eq $null) {
New-PSDrive -Name: Monitoring -PSProvider: OperationsManagerMonitoring -Root: \ -ErrorAction SilentlyContinue -ErrorVariable Err|out-null
if ($Err) { $(throw write-host $Err) }
Debug-Output “Operations manager shell initialized.”
}

Set-Location Monitoring:\$RMS |out-null

The problem is haviong to update the RMS Server name everytime. So create yourself a directory on your RMS servers and drop a config.xml file that the script can query. You may ask yourself you still have to change buit this config file can be referenced by all your scripts that run from the RMS and the file can be placed there as part iof the buid process. This allows you to sync scripts having now environemnt dependant detailes within them.

So in my case I have aded the config file named ‘scomconfig.xml and placed in E:\Demo folder. The drive is on a RMS cluster resource so that it fails over when the RMS does and better for resilience and one place to maintain files and scripts.

In the comconfig.xml edit and add following text and save.

<?xml version=”1.0″ ?>

<Configuration>

<RMSName>RMSServerorClsuterName</RMSName>

</Configuration>

Your new powershell script should now include

#——-Variables———-

# —  Get Configuration from Config file.
[xml]$ConfigFile = get-content e:\demo\scomconfig.xml
# — Set RMS name from config file
$RMS = $configfile.Configuration.RMSName

That’s it!

Advertisements
This entry was posted in Command Shell - Powershell Reference. Bookmark the permalink.

One Response to Run Powershell Scripts without configuring RMS Name every time

  1. Big Andy says:

    Seems like a useful tip

    🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s