Created
March 15, 2016 17:53
-
-
Save dfrankel33/8deba67892e99e33e89e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#INPUTS | |
#$ENV:SCOM_MGMT_SERVER = IP Address or FQDN of SCOM Management Server | |
#$ENV:SCOM_MGMT_GROUP = SCOM Management Group, if NULL, attempt to determine via registry | |
#$ENV:SCOM_USERNAME = User that has access to connect remotely to SCOM Management Server and has access to delete managed computers | |
#$ENV:SCOM_PASSWORD = Password for above user | |
#Set Error Action Preference | |
$errorActionPreference = "Stop" | |
#RightLink/RSC Working Directory | |
$rightlink_dir = 'C:\Program Files\RightScale\RightLink' | |
#Decommission Reason | |
$decom_reason = & "${rightlink_dir}\rsc.exe" rl10 show /rll/proc/shutdown_kind | |
If ($decom_reason -eq "terminate") { | |
Write-Output "Instance is terminating. Delete managed computer from SCOM..." | |
#SCOM Management Server | |
$mgmtServer = $ENV:SCOM_MGMT_SERVER | |
#SCOM Management Group | |
if($ENV:SCOM_MGMT_GROUP) { | |
$mgmtGroup = $ENV:SCOM_MGMT_GROUP | |
} | |
else { | |
$mgmtGroup = Get-ChildItem -Path “HKLM:\SYSTEM\CurrentControlSet\Services\HealthService\Parameters\Management Groups\” -ErrorAction SilentlyContinue | ForEach-Object {Get-ItemProperty $_.pspath | select -expandproperty PSChildName} | |
if (!($mgmtGroup)) { | |
Write-Output "Error! Unable to automatically determine Management Group!" | |
EXIT 1 | |
} | |
elseif ($mgmtGroup.Count -gt 1) { | |
Write-Output "Error! Unable to automatically determine Management Group as more then one were discovered!" | |
Write-Output "Discovered Management Groups: $mgmtGroup" | |
EXIT 1 | |
} | |
else { | |
Write-Output "Management Group: $mgmtGroup" | |
} | |
} | |
#Current instance/server FQDN | |
$dnsHostName = (Get-WmiObject win32_computersystem).DNSHostName+"."+(Get-WmiObject win32_computersystem).Domain | |
#SCOM Management Server and Management Group credentials | |
$scomPassword = $ENV:SCOM_PASSWORD | ConvertTo-SecureString -AsPlainText -Force | |
$scomCreds = New-Object System.Management.Automation.PSCredential -ArgumentList $ENV:SCOM_USERNAME, $scomPassword | |
#Connect to SCOM Management Server and delete managed computer | |
Invoke-Command -ComputerName $mgmtServer -Credential $scomCreds -ScriptBlock { | |
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.EnterpriseManagement.OperationsManager.Common") | |
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.EnterpriseManagement.OperationsManager") | |
$mgConnSetting = New-Object Microsoft.EnterpriseManagement.ManagementGroupConnectionSettings("$Using:mgmtGroup") | |
$mg = New-Object Microsoft.EnterpriseManagement.ManagementGroup($mgConnSetting) | |
$admin = $mg.Administration | |
$agentManagedComputerType = [Microsoft.EnterpriseManagement.Administration.AgentManagedComputer]; | |
$genericListType = [System.Collections.Generic.List``1] | |
$genericList = $genericListType.MakeGenericType($agentManagedComputerType) | |
$agentList = New-Object $genericList.FullName | |
#Replace DNSHostName with FQDN of agent to be deleted. | |
#ComputerName is a SCOM management server to query | |
$agent = Get-SCOMAgent -DNSHostName $Using:dnsHostName -ComputerName $Using:mgmtServer | |
$agentList.Add($agent); | |
$genericReadOnlyCollectionType = [System.Collections.ObjectModel.ReadOnlyCollection``1] | |
$genericReadOnlyCollection = $genericReadOnlyCollectionType.MakeGenericType($agentManagedComputerType) | |
$agentReadOnlyCollection = New-Object $genericReadOnlyCollection.FullName @(,$agentList); | |
$admin.DeleteAgentManagedComputers($agentList) | |
} | |
} | |
else { | |
Write-Output "Instance is rebooting. Skipping SCOM deletion." | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment