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:
On my Windows 7 workstation I configured the next Trustedhosts setting (read Administrator Guide to Windows PowerShell Remoting for info on Trustedhosts)
So what do we need to do, to get an overview of all alerts in the OpsMgr Management Group using PowerShell Remoting?
Steps:
$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}
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)
$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#http://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"
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
Hi Rikard,
Thanks for the tip. I just added some of your code to my example script.
Regards,
Stefan