####################################################################################
# Title: Custom fileds alert update script SCORCH
# Description: Collect additional informations from extended class and write back
# to the defined custom fields from original alert
# Version: 1.3/roe
# Date/Time: 05.09.2017 / 09:42
# Developer: Beat
####################################################################################
#Alert ID SCORCH value
$alertID = "Alert ID or runbook pattern link"
#SCOM MGM server for connection
$hostname = "scommgmsrv.domain.com"
#Declared variables for script
$arguments = "$alertID"
$password = ConvertTo-SecureString "*********" -AsPlainText -Force
$username = "DOMAIN\username"
$credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, $password
$session = New-PSSession -credential $credentials -ComputerName $hostname
$result = Invoke-Command -Session $session -ScriptBlock {$alertIDInv=$args[0]
#Begin script block "$alertIDInv"
#Write EventLog on SCOM management server "$hostname"
Write-EventLog -EntryType Success -LogName Application -Source SCOMScript -EventId 1000 -Message "Powershell script - SCORCH update custom fields - executed, Alert ID = $alertIDInv"
#Check if the operations manager module is loaded
$checksnap = Get-Module | Where-Object {$_.name -eq "OperationsManager"};
if ($checksnap.name -ne "OperationsManager")
{
Import-Module OperationsManager;
}
#Convert the alert ID and get the alert
$newalert = Get-SCOMAlert -Criteria "Id = '$alertIDInv'";
#Get the alert source server
$server= $newalert.principalname;
#Declare object to pass variable ot of script block "$alertIDInv"
new-object pscustomobject –property @{
server = $server
alertIDInv = $alertIDInv
}
#Argument list to pass in to the script block "$serverInv"
} -ArgumentList $arguments
#Close PS session from script block "$alertIDInv"
Remove-PSSession $session
#Declare Variables for script block "$serverInv"
$serverInv = $result.server
$alertIDInv = $result.alertIDInv
$argumentsInv = "$serverInv"
$session = New-PSSession -credential $credentials -ComputerName $hostname
$resultReg = Invoke-Command -Session $session -ScriptBlock {$serverInv=$args[0]
#Begin Script Block "$serverInv"
#***************Begin variables***************
$className = "Windows Computer Extended Attribute"
$classAttr1 = "AttributeDiscoveryGeneratedByUI843f4fa102e24b2791be3ccce9441e92"
$classAttr2 = "AttributeDiscoveryGeneratedByUIc16fe093d54846159a5f620545143277"
$classAttr3 = "ActiveDirectorySite"
#****************End variables****************
$class = Get-SCOMClass | WHERE {$_.Displayname -eq $className}
$SCOMExAttr = Get-SCOMClassInstance -Class $class | WHERE {$_.Displayname -eq "$serverInv"}
$serverFunctionSCOM = $SCOMExAttr.psobject.properties | % {if($_.Name -like "*$classAttr1*"){$_.value.value}}
$serverTypeSCOM = $SCOMExAttr.psobject.properties | % {if($_.Name -like "*$classAttr2*"){$_.value.value}}
$serverOUSiteSCOM = $SCOMExAttr.psobject.properties | % {if($_.Name -like "*$classAttr3"){$_.value.value}}
#Test content of variables
Write-Host $serverFunctionSCOM
Write-Host $serverTypeSCOM
Write-Host $serverOUSiteSCOM
#Declare object to pass variable ot of script block "$serverInv"
new-object pscustomobject –property @{
ServerFunctionSCOM = $serverFunctionSCOM
ServerOUSiteSCOM = $serverOUSiteSCOM
ServerTypeSCOM = $serverTypeSCOM
}
#Argument list to pass in to the script
} -ArgumentList $argumentsInv
#Close PS Session from script block "$serverInv"
Remove-PSSession $session
#Transform informations for SCOM custom fields variables
$serverFunctionSCOM = $resultReg.ServerFunctionSCOM
$serverOUSiteSCOM = $resultReg.ServerOUSiteSCOM
$serverTypeSCOM = $resultReg.ServerTypeSCOM
$serverDomainSCOM = $resultReg.ServerDomainSCOM