A PowerShell script for removing all Unix/Linux computers from a Management Server

We recently released a set of PowerShell scripts for cross platform computers in Operations Manager, which you could use for migrating a computer to a new management group, script-based discovery, or to upgrade the agent (see “Cross Platform PowerShell Scripts Released”). These scripts are great because they allow you to do things with your Unix/Linux computers that might not be as easy to do as with Windows computers. The problem is that when you use the OpsMgr PowerShell environment and use the command “Get-Agent”, all it returns is the Windows managed computers. None of the Unix/Linux computers show up. This makes dealing with individual Unix/Linux computers more challenging, and it’s why these PowerShell scripts are so handy – because we did all the work for you.

In my own testing I had a need for being able to quickly reset my system and remove all the Linux computers from the management group. For my own purposes, I could just hit delete a few times and be done with it – not much effort. Then, a few days ago, a customer wrote in and said they have 100+ Linux machines in their console and they’d like to delete them, and wondered if we had any PowerShell script to do that. Well, we didn’t, but using the existing PowerShell scripts we already released, I was able to quickly write up a script that used existing functions in the released scripts and then added the functionality of finding all the computers under Microsoft.Unix.Computer (the master class for all Unix and Linux computers) and then removing all of them from the management group.

IMPORTANT:

Running any script against a production Operations Manager server is dangerous! You should always test every script in a non-production environment to ensure it will work correctly in your specific situation. I have only done minimal testing in a limited environment that probably doesn’t come close to your own environment’s configuration.

Neither I nor Microsoft, nor any other person, animal, vegetable or mineral assumes responsibility for the script demonstrated here.

USE AT YOUR OWN RISK!

 

Ok, so here’s the setup. The code below is PowerShell. Copy & paste it to a file called RemoveAllUnixComputers.ps1 (you can name it anything you want but I think this name clarifies its purpose).

########################################################################################## # <copyright file="RemoveAllUnixComputers.ps1" company="Microsoft"> # Copyright (c) Microsoft Corporation. All rights reserved. # </copyright> # <summary>Looks for all Unix computers and removes them from OpsMgr</summary> ##########################################################################################

param([switch]$help)

begin { Set-PSDebug -Strict . C:\Files\PowerShell\scx.ps1 function ProcessComputers() { $localhost = GetComputerName if ($localhost -eq $null) { return LogStatus "Unable to get name of local computer" } Write-Host ("`n Local computer name: ${localhost}") $ms = Get-ManagementServer if ($ms -eq $null) { return LogStatus "Unable to get management server" } Write-Host ("`n Management server: $ms.HostComputer}") $mg = GetManagementGroup($ms.HostComputer) if ($mg -eq $null) { return LogStatus "Unable to get management group" } Write-Host ("`n Management group: ${mg}") $mc = Get-MonitoringClass -Name "Microsoft.Unix.Computer" if ($mc -eq $null) { return LogStatus "Unable to get monitoring class Microsoft.Unix.Computer" } Write-Host ("`n Monitoring class: ${mc}") $mo = Get-MonitoringObject -MonitoringClass $mc if ($mo -eq $null) { return LogStatus "Unable to get monitoring object(s) for class Microsoft.Unix.Computer" } Write-Host ("`n Monitoring object(s): ${mo}") foreach ($computer in $mo) { Write-Host "`n Now processing ${computer}" $discoveryData.Remove($computer) Write-Host ("`n`n ## Removed ${computer}") # Get the connector $connector = $mg.GetConnectorFrameworkAdministration().GetMonitoringConnectors() | where {$_.Name -eq 'Unix Computer Connector'} # Push it in via specified connector object $discoveryData.Commit($connector) Write-Host "`n`n ## Committed." } Write-Host ("`n Done removing computers ") } # Print help text. if ($Help) { Write-Host @"

RemoveAllUnixComputers.ps1

Removes all Unix/Linux computers from a management server.

Input: None.

Output: None.

Parameters: help - Prints this help

"@ exit }

    Load-Assembly("Microsoft.EnterpriseManagement.OperationsManager")

}

process { ProcessComputers }

end {

}

Note that this script calls the scx.ps1 script. This is to provide some basic functions, load the appropriate OpsMgr assemblies, and other things. This script should be modified to point to the correct location of the scx.ps1 scipt..

This is what was in my OpsMgr console before I ran the script:

image

To run the script, you’ll need to start a PowerShell shell, so go to Start > All Programs > System Center Operations Manager 2007 R2 > Operations Manager Shell. When the window opens, just type in the path and filename of the script and hit Enter. Don’t CD to the directory because then you’ll lose the connection to the provider for the management interface. Here’s what you see when you run it.

image

And when I refresh my OpsMgr console, the Linux computers are gone!

image

 

Happy scripting!