Operations Management Suite deployment automation with Powershell

Today I am going to show you how to deploy OMS workspaces with Powershell. It is an easy way to deploy multiple environments with predefined values.

First of all we want to login to our Azure and select subscription.

Login-AzureRmAccount
Get-AzureRmSubscription -SubscriptionId $SubID | Select-AzureRmSubscription

Next, we want to define the values for variables like location, plan, solutions to deploy to our OMS workspaces, event logs to monitor.

$Plan = "Premium"
$Location = "West Europe"
$Solutions = "Security", "Updates", "SQLAssessment", "AntiMalware", "AgentHealthAssessment", "ChangeTracking", "LogManagement", `
"SiteRecovery", "Backup", "NetworkMonitoring"
$ResourceRG = "OMS-Automation"
$WorkspaceName = "OMSLabs"
$WindowsEventLogs = "Application", "Operations Manager", "System"

If you are not sure which solutions are available on your OMS workspace, after you create one, you can run the following line to check what available solutions you have.

Get-AzureRmOperationalInsightsIntelligencePacks -ResourceGroupName $ResourceRG  -WorkspaceName $WorkspaceName

After defining values to variables we are going to create resource group, OMS workspace and enable solution packs.

try {
    Get-AzureRmResourceGroup -Name $ResourceRG -ErrorAction Stop
} catch {
    New-AzureRmResourceGroup -Name $ResourceRG -Location $Location
}
# Create the workspace
New-AzureRmOperationalInsightsWorkspace -Location $Location -Name $WorkspaceName -Sku $Plan -ResourceGroupName $ResourceRG

foreach ($solution in $Solutions) {
    Set-AzureRmOperationalInsightsIntelligencePack -ResourceGroupName $ResourceRG  -WorkspaceName $WorkspaceName -IntelligencePackName $solution -Enabled $true
}

Next step – we are going to define what performance counters we want start monitoring and create the function to add those performance counters in play

#Instance description. For Example: LogicalDisk(*)\Disk Read Bytes/sec, Instance is: *
#########################################################
$InstanceNameAll = “*”
$InstanceNameTotal = '_Total'

#########################################################
#Objects of the Performance Counters
#########################################################
$ObjectCache = "Cache"
$ObjectLogicalDisk = "LogicalDisk"
$ObjectMemory = "Memory"
$ObjectNetworkAdapter = "Network Adapter"
$ObjectNetworkInterface = "Network Interface"
$ObjectPagingFile = "Paging File"
$ObjectProcess = "Process"
$ObjectProcessorInformation = "Processor Information"
$ObjectProcessor = "Processor"

$ObjectSQLAgentAlerts = "SQLAgent:Alerts"
$ObjectSQLAgentJobs = "SQLAgent:Jobs"
$ObjectSQLAgentStatistics = "SQLAgent:Statistics"

$ObjectSQLServerAccessMethods = "SQLServer:Access Methods"
$ObjectSQLServerExecStatistics = "SQLServer:Exec Statistics"
$ObjectSQLServerLocks = "SQLServer:Locks"
$ObjectSQLServerSQLErrors = "SQLServer:SQL Errors"

$ObjectSystem = "System"

#########################################################
#Counters of the Performance Objects
#########################################################
$CounterCache = “Copy Read Hits %”

$CounterLogicalDisk = 
     "% Free Space" `
	,"Avg. Disk sec/Read" `
	,"Avg. Disk sec/Transfer" `
	,"Avg. Disk sec/Write" `
	,"Current Disk Queue Length" `
	,"Disk Read Bytes/sec" `
	,"Disk Reads/sec" `
	,"Disk Transfers/sec" `
	,"Disk Writes/sec"

$CounterMemory = 
     "% Committed Bytes In Use" `
    ,"Available MBytes" `
    ,"Page Faults/sec" `
    ,"Pages Input/sec" `
    ,"Pages Output/sec" `
    ,"Pool Nonpaged Bytes"

$CounterNetworkAdapter = 
     "Bytes Received/sec" `
    ,"Bytes Sent/sec"

$CounterNetworkInterface = "Bytes Total/sec"

$CounterPagingFile = 
     "% Usage" `
    ,"% Usage Peak"

$CounterProcess = "% Processor Time"

$CounterProcessorInformation = 
     "% Interrupt Time" `
    ,"Interrupts/sec"

$CounterProcessor = "% Processor Time"
$CounterProcessorTotal = "% Processor Time"

$CounterSQLAgentAlerts = "Activated alerts"
$CounterSQLAgentJobs = "Failed jobs"
$CounterSQLAgentStatistics = "SQL Server restarted"
$CounterSQLServerAccessMethods = "Table Lock Escalations/sec"
$CounterSQLServerExecStatistics = "Distributed Query"
$CounterSQLServerLocks = "Number of Deadlocks/sec"
$CounterSQLServerSQLErrors = "Errors/sec"

$CounterSystem = "Processor Queue Length"

ForEach ( $WEvent in $WindowsEventLogs)
{
    New-AzureRmOperationalInsightsWindowsEventDataSource -ResourceGroupName $ResourceRG  `
        -WorkspaceName $WorkspaceName -EventLogName $WEvent -CollectErrors -CollectWarnings -Name "$Wevent Event Log"
}

# Windows Perf

function AddPerfCounters ($PerfObject, $PerfCounters, $Instance, $PerfNo)
{
    ForEach ($Counter in $PerfCounters)
    {
    $PerfNo ++
      New-AzureRmOperationalInsightsWindowsPerformanceCounterDataSource -ResourceGroupName $ResourceRG  -WorkspaceName $WorkspaceName `
         -ObjectName $PerfObject  -CounterName $Counter -IntervalSeconds 10 -Name "ElastaBytes Performance Counter $PerfNo" -InstanceName $Instance
  #Name parameter needs to be unique. Adding random unique number. Name is not being showed anywhere in the Front End
    }
}
AddPerfCounters -PerfObject $ObjectLogicalDisk -PerfCounter $CounterLogicalDisk -Instance $InstanceNameAll -PerfNo 0
AddPerfCounters -PerfObject $ObjectNetworkAdapter -PerfCounter $CounterNetworkAdapter -Instance $InstanceNameAll -PerfNo 20
AddPerfCounters -PerfObject $ObjectNetworkInterface -PerfCounter $CounterNetworkInterface -Instance $InstanceNameAll -PerfNo 30
AddPerfCounters -PerfObject $ObjectPagingFile -PerfCounter $CounterPagingFile -Instance $InstanceNameAll -PerfNo 40
AddPerfCounters -PerfObject $ObjectProcess -PerfCounter $CounterProcess -Instance $InstanceNameAll -PerfNo 50
AddPerfCounters -PerfObject $ObjectProcessorInformation -PerfCounter $CounterProcessorInformation -Instance $InstanceNameAll -PerfNo 60
AddPerfCounters -PerfObject $ObjectProcessor -PerfCounter $CounterProcessor -Instance $InstanceNameAll -PerfNo 70
AddPerfCounters -PerfObject $ObjectProcessor -PerfCounter $CounterProcessorTotal -Instance $InstanceNameTotal -PerfNo 80
AddPerfCounters -PerfObject $ObjectSQLAgentAlerts -PerfCounter $CounterSQLAgentAlerts -Instance $InstanceNameAll -PerfNo 90
AddPerfCounters -PerfObject $ObjectSQLAgentJobs -PerfCounter $CounterSQLAgentJobs -Instance $InstanceNameAll -PerfNo 100
AddPerfCounters -PerfObject $ObjectSQLAgentStatistics -PerfCounter $CounterSQLAgentStatistics -Instance $InstanceNameAll -PerfNo 110
AddPerfCounters -PerfObject $ObjectSQLServerAccessMethods -PerfCounter $CounterSQLServerAccessMethods -Instance $InstanceNameAll -PerfNo 120
AddPerfCounters -PerfObject $ObjectSQLServerExecStatistics -PerfCounter $CounterSQLServerExecStatistics -Instance $InstanceNameAll -PerfNo 130
AddPerfCounters -PerfObject $ObjectSQLServerLocks -PerfCounter $CounterSQLServerLocks -Instance $InstanceNameAll -PerfNo 140
AddPerfCounters -PerfObject $ObjectSQLServerSQLErrors -PerfCounter $CounterSQLServerSQLErrors -Instance $InstanceNameAll -PerfNo 150
AddPerfCounters -PerfObject $ObjectSystem -PerfCounter $CounterSystem -Instance $InstanceNameAll -PerfNo 160
AddPerfCounters -PerfObject $ObjectMemory -PerfCounter $CounterMemory -Instance $InstanceNameAll -PerfNo 170
AddPerfCounters -PerfObject $ObjectCache -PerfCounter $CounterCache -Instance $InstanceNameAll -PerfNo 180

And this is pretty much it. It will take you seconds to deploy new OMS workspace.

If you do have OMS workspace created with saved Log Analytics queries the following powershell code will help you to export those queries and import to new OMS workspace.

$LogAnalyticsQueries = (Get-AzureRmOperationalInsightsSavedSearch -ResourceGroupName $ResourceRG -WorkspaceName $WorkspaceName).Value.Properties | ConvertTo-Json 
$LogAnalyticsQueries | Out-File -FilePath C:\OMS\Original.json

$ExportedSearches = Get-Content -Raw -Path C:\OMS\Original.json | ConvertFrom-Json


# Import Saved Searches
foreach ($search in $ExportedSearches) 
{
    $random = Get-Random 10000
    $id = $search.Category + '-' + $random
    New-AzureRmOperationalInsightsSavedSearch -ResourceGroupName $ResourceRG -WorkspaceName $WorkspaceName -SavedSearchId $id `
    -DisplayName $search.DisplayName -Category $search.Category -Query $search.Query -Version $search.Version
}

And how to deploy Monitoring extension to your Azure VMs you can find in my previous blog post.
Enjoy using it and thank you for reading!

One comment

  1. Hi,

    At line 101 where you run command:

    AddPerfCounters -PerfObject $ObjectLogicalDisk -PerfCounter $CounterLogicalDisk -Instance $InstanceNameAll -PerfNo 0

    I get this error in PowerShell, even though I have run Login-AzureRMAccount before starting and have selected my subscription:

    New-AzureRmOperationalInsightsWindowsPerformanceCounterDataSource : Run Login-AzureRmAccount to login.
    At line:6 char:7
    + New-AzureRmOperationalInsightsWindowsPerformanceCounterDataSour …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [New-AzureRmOper…unterDataSource], PSInvalidOperationException
    + FullyQualifiedErrorId : InvalidOperation,Microsoft.Azure.Commands.OperationalInsights.NewAzureOperationalInsightsWindowsPerformanceCounterDataSourceCommand

    Is it somehow changed that when running New-AzureRmOperationalInsightsWindowsPerformanceCounterDataSource that you need to specify Login-AzureRmAccount before running the command?

    Thanks.

Leave a Reply

Your email address will not be published.