Pavleck.Net

Monitoring, Scripting, and other Technologies

Archive for the 'SCOM Snippets' Category


Setting agent failover servers & Switching SNMP device proxies

Posted by Jeremy D. Pavleck on 7th July 2008

By default, you can’t really specify a failover management server in OpsMgr. Why? Not really sure, though I think it’s a ploy to ensure you setup the OpsMgr Active Directory Integration, which will handle this for you.

No fret though, we can still do it - it’ll just take a little bit of actual effort.

First, we need to define our Primary and Failover management servers. This isn’t something you can just progmatically grab, so you’ll need to know the name yourself.

In my $PROFILE, I’ve set them to be defined to 2 variables with the following:

# Set Primary Management Server
$Primary_MS = Get-ManagementServer | ? {$_.Name -like "SERVERNAME*"}
# Set Failover Management Server
$Failover_MS = Get-ManagementServer | ? {$_.Name -like "BACKUPSERVER*"}

Now that we have that set, it’s simple to do the rest. First, lets grab all of the servers that don’t have a failover management server set.

$noFailoverSpecified = Get-Agent | ? {(!$_.GetFailoverManagementServers())}

What the above does is call the GetFailoverManagementServers() method on each agent. If they have a failover, it will return data and thus $True. If there aren’t any failovers, it will return nothing - which is the same as $False. So we look for all the ones that don’t return anything.

If you’re curious, you can see just how many servers are missing failovers with

$noFailoverSpecified.Count

- in my case it was 63.

Now, we just run a quick snippet that adds the failover server to the agent:

ForEach ($agent in $noFailoverSpecified) {
Set-ManagementServer -PrimaryManagementServer $Primary_MS -AgentManagedComputer $agent -FailoverServer $Failover_MS | Out-Null
}

That will crunch away as it’s doing it’s thing, we’re redirecting output to $null so we don’t have to see agents scrolling over and over. When it returns you to a prompt, you’re done. If you’d like to verify that you did indeed set all of the agents to have a failover, we can check real quick:

If ((Get-Agent |? {(!$_.GetFailoverManagementServers())}).Count -eq $null)
{
Write-Host "Every agent has a failover server, great job!" -ForeGroundColor Green
} else {
Write-Host "Looks like we missed some, try again!" -ForeGroundColor Magenta
}

And that’s that. All of your agents have a primary and failover server.

Screen shot of SCOM Command Shell showing steps to setup failover agents

But wait, you have a lot of remotely managed devices too? Monitoring SNMP on a bunch of different servers - what happens for that?

Well, we can’t setup a failover (From what I’ve seen, if I’m wrong please let me know) agent. But we can proactively write a script that will change the proxy agent on the devices, and run it as needed.

This was written in a response to this query on the newsgroups, and is only a cursory look into it. There may be other ways of doing this - and I’d love to hear it. As it stands, I’m not sure how to set them back to a management server as the monitor.

Firstly, we’ll have to pick an agent managed computer to use as the new proxy agent. You can’t use a management server for this, because they aren’t “Agent Managed” and you can’t use Set-ManagementServer because the devices aren’t “Remote Managed Computers”.  I have a seperate agent-managed server on my network I call “Timex” because it acts like a watcher node. So I’ll go ahead and use him.

$proxyAgent = Get-Agent |? {$_.Name -eq "timex.pavleck.net"}

Then gather a list of our current remotely managed devices

$remDevices = Get-RemotelyManagedDevice

Now just loop through it, setting the device to use the proxy agent we just instantiated:

ForEach($device in $remDevices) {
Set-ProxyAgent -ProxyAgent $proxyAgent -Device $device | Out-Null
}

That will loop through things changing the proxy server that it uses. When it’s done, we can verify it by running:

Get-RemotelyManagedDevice |? {$_.ProxyAgentPrincipalName -ne $proxyAgent.Name}

If it outputs nothing, then they’ve all been changed. Simple as that!

SCOM: Setting the proxy agent for a device via command shell

Posted in Command Shell, Powershell, SCOM, SCOM Snippets, SNMP | 2 Comments »

SCOM One-Liners: Get the Host name of your remotely managed device.

Posted by Jeremy D. Pavleck on 5th June 2008

One of the nice things about SCOM2007 is the ease in which you can monitor and manage non-Windows devices (Even better with the dhjdhdhdhd addon). Being able to add your switches, routers and Unix devices makes for a more complete overview of the health of your system.

There is a downfall though, and that would be that although you can use Get-RemotelyManagedDevice in the Command shell to list the, umm, remotely managed devices, all it will return is the IP address.

Here’s a SCOM command shell one-liner that will use some .Net-Fu to reverse the IP for you, on the fly:

Get-RemotelyManagedDevice | ForEach-Object {Write-Host "IP $($_.Name) resolves to hostname $([System.Net.Dns]::GetHostByAddress($_.Name).HostName)" }

If you want, you can go a step further and add it to your profile (Or more preferably, a SCOM snippet file that I’ll be writing about later.) and pull it up anytime you want.

The Display-NetAgentsByHostName function has one parameter, -short. Run the default ‘long’ way, it will reverse the IP, list the client that is managing it, the management group and the health state.
With the -short switch, it simply outputs health state, host name and IP address.

It’s not perfect, and coult use some formatting, but it works as is, so I leave it up as an exercise to the reader to polish it some more ;)
Page formatting messing it up? I’ll fix it some day - until then, download Display-NetAgentsByHostName.ps1

function Display-NetAgentsByHostName([switch]$short)
{
function LookUp([string]$ip)
{
trap {
"Unable to resolve IP"
continue;
}
([System.Net.Dns]::GetHostByAddress($ip)).HostName
}
Get-RemotelyManagedDevice | ForEach-Object {
If (!$short) {
If ($_.HealthState -eq "Success") {
Write-Host ("IP Address $($_.Name) resolves to ‘$(Lookup($_.Name))’ - Managed by server: $($_.ProxyAgentPrincipalName.Split(’.;’)[0])" +
"in Management group: $($_.ManagementGroup) - Health State: $($_.HealthState)`n") -ForeGroundColor Green
} elseif ($_.HealthState -eq "Warning") {
Write-Host ("IP Address $($_.Name) resolves to ‘$(Lookup($_.Name))’ - Managed by server: $($_.ProxyAgentPrincipalName.Split(’.;’)[0])" +
"in Management group: $($_.ManagementGroup) - Health State: $($_.HealthState)`n") -ForeGroundColor Yellow
} elseif ($_.HealthSTate -eq "Error") {
Write-Host ("IP Address $($_.Name) resolves to ‘$(Lookup($_.Name))’ - Managed by server: $($_.ProxyAgentPrincipalName.Split(’.;’)[0])" +
"in Management group: $($_.ManagementGroup) - Health State: $($_.HealthState)`n") -ForeGroundColor Red
}
} else {
If ($_.HealthState -eq "Success") {
Write-Host "HEALTHY - Host: $(Lookup($_.Name)) - IP: $($_.Name)" -ForeGroundColor Green
} elseif ($_.HealthState -eq "Warning") {
Write-Host "HEALTH WARNING! State: $($_.HealthState) - Host: $(Lookup($_.Name)) - IP: $($_.Name)" -ForeGroundColor Yellow
} elseif ($_.HealthState -eq "Error") {
Write-Host "HEALTH ERROR! State: $($_.HealthState) - Host: $(Lookup($_.Name)) - IP: $($_.Name)" -ForeGroundColor Red
}
}
}
}

Posted in Powershell, SCOM, SCOM Snippets | No Comments »