Part 2: Add Locations to OpsMgr Agents with PowerShell

March 25, 2014 at 1:50 pm in Powershell, SytemCenter, Uncategorized by alkin

In my previous post I explained how to do a bulk import of locations.

Now that we have all the locations we want into OpsMgr we can associate agent-managed computers, management servers, or resource pools with a location using teh set-scomlocation CMDLET.

The Set-SCOMLocation cmdlet associates one or more agent-managed computers, management servers, or resource pools with a location. This cmdlet removes a current association, if one exists. The Web Application Availability Monitoring Summary Map Dashboard displays state information for agents, management servers, and resource pools associated with a location. You can remove an association by using the Remove-SCOMLocation cmdlet.

The following script will add location coordinates to a list of servers retrieved from a CSV file. You can download the CSV file here.

# //***************************************************************************
# // ***** Script Header *****
# //
# // Solution:
# // File:      AddlocationstoAgents.ps1
# // Author:    Alexandre Verkinderen
# // Purpose:   Add locations to Agents from Excel
# //
# //
# // ***** End Header *****
# //***************************************************************************
# //----------------------------------------------------------------------------
#//
#//  Global constant and variable declarations
#/
#//----------------------------------------------------------------------------

$list = Import-Csv -Path "C:\temp\Servers.csv"

$MS = "MOM4"

Import-Module OperationsManager
#Connect to OpsMgr Management GroupStart-OperationsManagerClientShell -ManagementServerName: $MS -PersistConnection: $true -Interactive: $true

#//----------------------------------------------------------------------------
#//  Main routines
#//----------------------------------------------------------------------------

#Get all agents
$computers = get-scomagent

#Get all Locations
$Locations = Get-scomlocation

foreach ($entry in $list)
{

$name = $entry.Server
$Loc = $entry.Location

#Filter agents by name
$agent = $computers | where { $_.displayname -like "*$name*" }

#Filter agents by Location
$location = $Locations | where { $_.displayname -like "*$Loc*" }

#Set location
Set-SCOMLocation –Location $Location –Agent $agent

write-host server $name is in $loc

}

Hope this helps.

Alexandre Verkinderen