Share via


Using PowerShell Remoting to connect to OpsMgr Root Management Server and use the OpsMgr Cmdlets

This week I’m busy preparing a PowerShell v2 Essentials workshop I’ll be delivering next week. One of the Modules of this PowerShell  v2 Essentials Workshop is PowerShell Remoting. If you don’t go to my workshop or don’t know what PowerShell remoting is, I suggest reading the Administrator's Guide to Windows PowerShell Remoting .

In this blog article I’ll show you how you can use PowerShell Remoting on your workgroup machine to connect to the OpsMgr Root Management Server to execute OpsMgr Cmdlets remotely. I want to thank Theo Kostelijk (a colleague of mine) for providing me with some examples he used in Opalis.

Overview of my OpsMgr environment:

  • Workstation with Windows 7 on Microsoft Corp domain
  • OpsMgr Root Management Server in demo domain

On my Windows 7 workstation I configured the next Trustedhosts setting (read Administrator Guide to Windows PowerShell Remoting for info on Trustedhosts)

image

So what do we need to do, to get an overview of all alerts in the OpsMgr Management Group using PowerShell Remoting?

Steps:

  1. First we need to create a new session with the Remote Server (RMS)
    $RMSSession = New-PSSession –ComputerName OpsMgrRMS.stranger.local -Credential (Get-Credential)

Because the Root Management Server is in a different domain than my workstation I need to enter credentials.

Result:
image

  • Next we need to add the OpsMgr Snappin in the session we created in the first step.

    $RMSSession = get-pssession

    #Load OpsMgr Snapin Invoke-Command -Session $RMSSession -ArgumentList $rmsName -ScriptBlock {param ($rmsName) $rms=$rmsName} Invoke-Command -Session $RMSsession -ScriptBlock {add-pssnapin "Microsoft.EnterpriseManagement.OperationsManager.Client"} Invoke-Command -Session $RMSsession -ScriptBlock {Set-Location "OperationsManagerMonitoring::"} Invoke-Command -Session $RMSSession -ScriptBlock {New-ManagementGroupConnection -connectionString:$rms} Invoke-Command -Session $RMSsession -ScriptBlock {Set-Location $rmsName}

    Result:
    image

    We now have a connection with the RMS.

  • Now we can try to retrieve the Alerts using the Get-Alert OpsMgr Cmdlet

    $alerts = Invoke-Command -Session $RMSSession -ScriptBlock{get-alert}

    Result:
    image

  • When we are finished we can close the Remote Session.

    Remove-PSSession -Session $RMSSession

    Result:
    image

Here is an overview of the complete PowerShell script I used to retrieve the Alerts Remotely.

#Using OpsMgr Cmdlets using PowerShell Remoting

#Variables $rmsName = "opsmgrrms.contoso.com"

#Create a connection with the Remote Server (RMS) $RMSSession = New-PSSession -ComputerName $rmsName -Credential (Get-Credential)

#Load OpsMgr Snapin Invoke-Command -Session $RMSSession -ArgumentList $rmsName -ScriptBlock {param ($rmsName) $rms=$rmsName} Invoke-Command -Session $RMSsession -ScriptBlock {add-pssnapin "Microsoft.EnterpriseManagement.OperationsManager.Client"} Invoke-Command -Session $RMSsession -ScriptBlock {Set-Location "OperationsManagerMonitoring::"} Invoke-Command -Session $RMSSession -ScriptBlock {New-ManagementGroupConnection -connectionString:$rms} Invoke-Command -Session $RMSsession -ScriptBlock {Set-Location $rmsName}

$alerts = Invoke-Command -Session $RMSSession -ScriptBlock{get-alert} Write-Host "Return Alerts" $alerts.count

#Close Remote PowerShell Session with RMS Remove-PSSession -Session $RMSSession

Have fun with PowerShell Remoting and OpsMgr!

Update! Thanks to Rikard Ronnkvist I found a new way for connecting to the Root Management Server using PowerShell Remoting. Thanks Rikard!

#Using OpsMgr Cmdlets using PowerShell Remoting#Info from Rikard Ronnkvist#https://www.snowland.se/2010/08/23/maintenance-mode-via-powerhsell-remoting/

function OpsMgrRemoteConnection { param ([string] $rmsHostname)

 Invoke-Command -ComputerName $rmsHostname -credential (Get-Credential) -scriptblock {

  Add-PSSnapin "Microsoft.EnterpriseManagement.OperationsManager.Client"  Set-Location "OperationsManagerMonitoring::"  New-ManagementGroupConnection -ConnectionString:localhost    #Enter OpsMgr Cmdlet you want to use  $OpsMgrcommands = Get-OperationsManagerCommand  $OpsMgrCommands | select Name, CommandType   } #End scriptblock} #End Function

OpsMgrRemoteConnection -rmsHostname "opsmgrrms.contoso.com"

Tweet

Comments

  • Anonymous
    January 01, 2003
    Hi Rikard, Thanks for the tip. I just added some of your code to my example script. Regards, Stefan

  • Anonymous
    November 02, 2010
    Love remoting... did a script a couple of months ago that enables maintenance mode on the current server. www.snowland.se/.../maintenance-mode-via-powerhsell-remoting