Sorry for the delay, I really spaced out and forgot about this.

If you still need it, here you go.

These are the scripts I use in my decomm process. Seach AD and remove from
AD if required and then remove from SCOM and SCCM.


Function Get-ADComputer
 {Param($ComputerName)

#Finding Host is DNS to get DNS Domain Name.
Try{$DNSName = ([System.Net.Dns]::GetHostByName(($ComputerName)))
}
Catch #Catch error if DNS does not know about this host.
{return $_.Exception.Message}
Write-host $($DNSName.HostIPList)
$FQDN = $($DNSName.HostName) -split "\."
$Domain = "$($FQDN[1]).$($FQDN[2])"
#Write-Host $Domain -ForegroundColor Green
  $forest =
[System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()
$domain = $forest.Domains | ? {$_.Name -eq $domain} #$domain | % {$_}

$domainDN = $domain.GetDirectoryEntry().DistinguishedName
#Write-Output "Found the remote domain, the full LDAP distinguished name is
$DomainDN"

#Create an LDAP searcher object and pass in the DN of the domain we wish to
query
$Searcher=New-Object
System.DirectoryServices.DirectorySearcher([ADSI]"LDAP://$domainDN")

#Pass in the ceriteria we are searching for.
$Searcher.filter="(&(objectClass=computer)(Name= $($ComputerName)))"
$results = $Searcher.FindOne()
  If ($results.Path -ne $null )
  {
$LastPwdTime =
[datetime]::fromfiletime($results.properties.item("pwdLastSet")[0])
$Computer = $results.GetDirectoryEntry()
[string]$ComputerDN = "$($Computer.DistinguishedName)"
$ADSearch = "Found Computer Account in AD: $ComputerDN" #-ForegroundColor
Green
$ADSearch
}
 }

Function Remove-SCOMAgent
 {Param($ComputerName)
$ScomServer = "SCOMServer"
Import-Module OperationsManager
New-SCOMManagementGroupConnection -ComputerName $ScomServer
$ComputerName = ""
#Loading SCOM Assamblies and API
[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.EnterpriseManagement.OperationsManager.Common")
[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.EnterpriseManagement.OperationsManager")

#Connecting to SCOM management Group
$MGConnSetting = New-Object
Microsoft.EnterpriseManagement.ManagementGroupConnectionSettings("$ScomServer")
$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

#Getting Computer From SCOM
$agent = Get-SCOMAgent -DNSHostName "$ComputerName*" -ComputerName
$ScomServer

$agentList.Add($agent);

#Checking if we found Computer in SCOM
If ($agent -ne $null)
{
Write-Host "Found Computer: $ComputerName in SCOM, proceeding with Removal"
-ForegroundColor Green
$genericReadOnlyCollectionType =
[System.Collections.ObjectModel.ReadOnlyCollection``1]
$genericReadOnlyCollection =
$genericReadOnlyCollectionType.MakeGenericType($agentManagedComputerType)
$agentReadOnlyCollection = new-object $genericReadOnlyCollection.FullName
@(,$agentList);
$admin.DeleteAgentManagedComputers($agentList)
$SCOMRemoval = "Removed"
} Else {
Write-Host "Computer: $ComputerName NOT In SCOM" -ForegroundColor Red
$SCOMRemoval = "NotFound"}
 }

 Function Remove-SCCMAgent
 {Param($ComputerName)
 $SiteServer = "SiteServer"
$SiteCode = "LAB"
$ComputerName = ""
$compObject = get-wmiobject -query "select * from SMS_R_SYSTEM WHERE
Name='$ComputerName'" -computername $SiteServer -namespace
root\SMS\site_$SiteCode

If ($compObject -ne $null)
{ $compObject.Delete()
$result = "Computer $ComputerName DELETED Successfully"
}
Else
{
      $result = "Computer $ComputerName NOT found"
}
 }

On Thu, Oct 8, 2015 at 6:20 AM, CESAR.ABREG0 . <[email protected]> wrote:

> I'm in the process of getting a script in orchestrator at my new gig. I
> will send it later when I get to the office.
>
> On Wed, Oct 7, 2015, 4:00 PM Tommy Carron <[email protected]> wrote:
>
>> Hi Steven
>>
>> Have you looked at the Powershell cmdlet:
>>
>> Uninstall-SCOMAgent -ActionAccount<PSCredential>
>> [-Agent<AgentManagedComputer[]>] -PassThru [<CommonParameters>]
>>
>> Tommy
>>
>>
>> On Thursday, 8 October 2015, Steven Peck <[email protected]> wrote:
>>
>>> Is there a PowerShell way to remove agents from the SCOM console (like
>>> right click remove) (OpsManager 2012r2).  Most searches seem to indicate
>>> remove from database which makes me nervous.
>>>
>>> Currently by the time I am told of a server being decommed, it’s
>>> generally shut down and possibly already deleted out of our virtual
>>> environment so I would like to introduce some automation either in
>>> PowerShell or Orchestrator.
>>>
>>> Thanks,
>>> Steven
>>>
>>>
>>>
>>>
>>>
>



Reply via email to