SCOM.Management.OMSWorkspaceAdd.WA (WriteActionModuleType)

Element properties:

TypeWriteActionModuleType
IsolationAny
AccessibilityInternal
RunAsDefault
InputTypeSystem.BaseData
OutputTypeSystem.BaseData

Member Modules:

ID Module Type TypeId RunAs 
PSWA WriteAction Microsoft.Windows.PowerShellWriteAction Default

Overrideable Parameters:

IDParameterTypeSelector
WorkspaceIDstring$Config/WorkspaceID$
WorkspaceKeystring$Config/WorkspaceKey$
ProxyURLstring$Config/ProxyURL$
AzureCloudTypeint$Config/AzureCloudType$
TimeoutSecondsint$Config/TimeoutSeconds$

Source Code:

<WriteActionModuleType ID="SCOM.Management.OMSWorkspaceAdd.WA" Accessibility="Internal" Batching="false">
<Configuration>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="WorkspaceID" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="WorkspaceKey" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="ProxyURL" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="AzureCloudType" type="xsd:integer"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="TimeoutSeconds" type="xsd:integer"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="WorkspaceID" Selector="$Config/WorkspaceID$" ParameterType="string"/>
<OverrideableParameter ID="WorkspaceKey" Selector="$Config/WorkspaceKey$" ParameterType="string"/>
<OverrideableParameter ID="ProxyURL" Selector="$Config/ProxyURL$" ParameterType="string"/>
<OverrideableParameter ID="AzureCloudType" Selector="$Config/AzureCloudType$" ParameterType="int"/>
<OverrideableParameter ID="TimeoutSeconds" Selector="$Config/TimeoutSeconds$" ParameterType="int"/>
</OverrideableParameters>
<ModuleImplementation Isolation="Any">
<Composite>
<MemberModules>
<WriteAction ID="PSWA" TypeID="Windows!Microsoft.Windows.PowerShellWriteAction">
<ScriptName>SCOM.Management.OMSWorkspaceAdd.WA.ps1</ScriptName>
<ScriptBody><Script>
#=================================================================================
# Script to ADD Log Analytics (OMS) Workspace
#=================================================================================
param($WorkspaceID,$WorkspaceKey,$ProxyURL,[int]$AzureCloudType)


# Manual Testing section - put stuff here for manually testing script - typically parameters:
#=================================================================================
# $WorkspaceID = "WorkspaceID"
# $WorkspaceKey = "WorkspaceKey"
# $ProxyURL = ""
# $AzureCloudType = "0"
#=================================================================================


# Constants section - modify stuff here:
#=================================================================================
# Assign script name variable for use in event logging
$ScriptName = "SCOM.Management.OMSWorkspaceAdd.WA.ps1"
#=================================================================================


# Starting Script section
#=================================================================================
# Gather who the script is running as
$whoami = whoami

#Load the MOMScript API and discovery propertybag
$momapi = New-Object -comObject "Mom.ScriptAPI"

#Log script event that we are starting task
$momapi.LogScriptEvent($ScriptName,1347,0, "`n Starting script. `n Running as ($whoami)")
#=================================================================================


# Begin Main Script
#=================================================================================
Write-Host "Task Starting. Running as $whoami"
#Load agent scripting object
Write-Host "Loading agent scripting objects."
$AgentCfg = New-Object -ComObject AgentConfigManager.MgmtSvcCfg
#Check to see if this agent supports OMS
$AgentSupportsOMS = $AgentCfg | Get-Member -Name 'GetCloudWorkspaces'
IF (!$AgentSupportsOMS)
{
Write-Host "FATAL ERROR: This agent version does not support Cloud Workspaces. You must upgrade this agent. Terminating."
EXIT
}

#Evaluate AzureCloudType parameter
IF ($AzureCloudType -eq 0)
{
$AzureCloudType = 0 #This is commercial Azure Cloud and default
Write-Host "Azure Cloud Type = 0 which is default Azure Commercial."
}
ELSEIF ($AzureCloudType -eq 1)
{
Write-Host "Azure Cloud Type = 1 which is Azure for US Government."
}
ELSE
{
Write-Host "An invalid Azure Cloud Type was passed. This value must be 0 or 1. `nSetting Azure Cloud Type = 0 which is default Azure Commercial."
$AzureCloudType = 0
}

#Evaluate if this MMA supports AzureCloudType
$CloudWorkspaceSupport = $AgentCfg | Get-Member -Name "AddCloudWorkspace"
[string]$CloudWorkspaceSupDef = $CloudWorkspaceSupport.Definition
IF ($CloudWorkspaceSupDef -notmatch "AzureCloudType")
{
#This MMA does not support AzureCloudType parameter
Write-Host "This MMA/Agent version does not support the AzureCloudType parameter. `nIf you require AzureCloudType then the MMA/Agent must be upgraded. `nAttempting to add Log Analytics Workspace with no AzureCloudType."
#Add OMS Workspace
Write-Host "Adding Log Analytics Workspace: ($WorkspaceID)"
$Error.Clear()
$AgentCfg.AddCloudWorkspace($WorkspaceID,$WorkspaceKey)
}
ELSE
{
#This MMA supports AzureCloudType parameter
#Add OMS Workspace
Write-Host "Adding Log Analytics Workspace: ($WorkspaceID)"
$Error.Clear()
$AgentCfg.AddCloudWorkspace($WorkspaceID,$WorkspaceKey,$AzureCloudType)
}

IF ($Error)
{
Write-Host "There was a critical error adding Log Analytics Workspace. Error is: $Error"
EXIT
}
Write-Host "Log Analytics Workspace Added."
#Add Proxy URL
IF ($ProxyURL)
{
Write-Host "A proxy URL was specified: ($ProxyURL). Adding Log Analytics Proxy configuration to agent."
$AgentCfg.SetProxyUrl($ProxyURL)
Write-Host "Proxy Added."
}
#Restart Agent
Write-Host "Restarting Agent now...."
#=================================================================================
# End Main Script

# Restart Agent
#=================================================================================
#We need a reliable way to restart the SCOM Agent out of band so that tasks can complete with success
$Command = "Start-Sleep -s 5;Restart-Service HealthService"
$Process = ([wmiclass]"root\cimv2:Win32_ProcessStartup").CreateInstance()
$Process.ShowWindow = 0
$Process.CreateFlags = 16777216
([wmiclass]"root\cimv2:Win32_Process").Create("powershell.exe $Command")|Out-Null
#=================================================================================
</Script></ScriptBody>
<Parameters>
<Parameter>
<Name>WorkspaceID</Name>
<Value>$Config/WorkspaceID$</Value>
</Parameter>
<Parameter>
<Name>WorkspaceKey</Name>
<Value>$Config/WorkspaceKey$</Value>
</Parameter>
<Parameter>
<Name>ProxyURL</Name>
<Value>$Config/ProxyURL$</Value>
</Parameter>
<Parameter>
<Name>AzureCloudType</Name>
<Value>$Config/AzureCloudType$</Value>
</Parameter>
</Parameters>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
</WriteAction>
</MemberModules>
<Composition>
<Node ID="PSWA"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.BaseData</OutputType>
<InputType>System!System.BaseData</InputType>
</WriteActionModuleType>