Quickly Determine if Servers are in OMS

It has been a long time since I’ve posted anything, having transitioned to a role focusing on Azure. Like a number of you, I’m taking point on getting our on prem servers to send data to OMS directly. This includes Windows and Linux. We’ve come up with some creative ways to do this–especially if you consider we’re a large enterprise, and still a SCOM 2012 R2 shop.

In the SCOM world, it’s fairly commonplace to be given a list containing hundreds of servers, and be asked to confirm/deny their existence in SCOM. I’m finding this extends to OMS as well. This example applies to a single workspace, but you can make it loop through any number of workspaces if you wish.

Feed a list of servers to the code below, and go get a cup of coffee. Be sure to let people know it took hours to get the results!

Connect-AzureRmAccount
$queryResults = Invoke-AzureRmOperationalInsightsQuery -WorkspaceId “<workspace_id>” -Query “Heartbeat | distinct Computer | order by Computer asc”
$queryResults.Results # Returns result set as a list
$serverlist = Get-Content -Path <path to serverlist file>
write-host “There are” $serverlist.count “servers in list”

# $queryResults is a custom PS object with a NoteProperty of Computer. We can convert it to an array, which then allows us to use -contains
$omsagentarry = New-Object System.Collections.ArrayList
foreach ($srv in $queryResults.results)
{
$omsagentarry.Add($srv.Computer) | Out-Null
}

# Loop through each server in the list, and see if it is contained in the array of computers in OMS
foreach ($server in $serverlist)
{
if ($omsagentarry -contains $server)
{
write-host “$server in OMS”
$message = “Yes”
}
else
{
write-host “$server not in OMS”
$message = “No”
}
[PSCustomObject]@{
Server = $server
InOMS = $message
} | Export-Csv <Output file path> -notype -Append
}

Leave a Reply