DiscoPosse – Using the chicken to measure IT
Technology, Cycling, Music and Madness


General

March 10, 2012

Remove old Active Directory computer accounts from SCCM

More articles by »
Written by: Eric
Tags: , , , ,

A common issue in IT organizations is that the removal of computer objects is not done regularly. While I’ve presented scripts for removing old computer accounts from Active Directory, I’ve recently had to extend the removal of legacy computers into other systems such as SCCM.

Because SCCM dynamically discovers computers, but does not remove them I’ve created this short script which scans the Active Directory for computer accounts which have been off the network for more than a certain number of days (45 is my preferred number).

The next thing that you will need to do is to make sure that you move those computer accounts to an OU which is not discoverable by SCCM, or delete them from Active Directory altogether so that they are not re-discovered. This script simply removes them from SCCM.

The code at the end of the article is also stored at my TechNet Gallery page here or you can go directly to the article from this link:

http://gallery.technet.microsoft.com/Remove-old-Active-7fc40c61

In order to really keep a clean Active Directory, you should also make use of other processes such as my Organize Computers by Subnet IP which you will find here:

http://gallery.technet.microsoft.com/PowerShell-Organize-d37c2a29

The full article on that process is here.

Here’s the code:

# Environment setup 
# Import the ActiveDirectory module to enable the Get-ADComputer CmdLet 
Import-Module ActiveDirectory 

$SCCMServer = ”YOUR SCCM SERVER” 
$sitename = ”YOUR SCCM SITE” 
$old = (Get-Date).AddDays(-45) # The threshold for what we consider to be old (current set as 45 days) 

# Find the computers in Active Directory which are ”old” 
$oldComputers = Get-ADComputer -Filter {PasswordLastSet -le $old} -Properties * 

ForEach ($oldComputer in $oldComputers) { 
    # Select the computer(s) 
    $computername = $oldComputer.name  

    # Get the resourceID from SCCM 
    $resID = Get-WmiObject -computername $SCCMServer -query ”select resourceID from sms_r_system     where name like `’$computername`’” -Namespace ”root\sms\site_$sitename” 
    $computerID = $resID.ResourceID 

    if ($resID.ResourceId -eq $null) { 
        $msgboxValue = ”No SCCM record for that computer” 
        } 
    else 
        { 
            $comp = [wmi]“\\$SCCMServer\root\sms\site_$($sitename):sms_r_system.resourceID=$($resID.ResourceId)”  

            # Output to screen 
        Write-Host ”$computername with resourceID $computerID will be deleted” 

        # Delete the computer account 
            $comp.psbase.delete() 
    } 
}

 



About the Author

Eric





 
 

 
featured_powershell

Updating (same as parent folder) records with DNSCMD and PowerShell

In an earlier post on the site (Microsoft DNS record updates using PowerShell and DNSCMD) I noted how PowerShell cannot natively update records in MS DNS, however we could leverage the DNSCMD command and pass parameters using a...
by Eric
1

 
 
featured_powershell

CSV, yeah you know me! – PowerShell and the Import-Csv CmdLet – Part 3

This was a long overdue post, so thanks for sticking with me while I finally got back on track with our CSV, yeah you know me series (Here are Part 1 and Part 2). As I’d mentioned in the closing of Part 2, we want to be a...
by Eric
0

 
 
featured_powershell

Finding RDP sessions on servers using PowerShell

Have you ever needed to use RDP to get to a server console for some local admin work and then been bounced out because there are already active sessions? Or have you had your Active Directory account locked out because of an op...
by Eric
8

 




Join Zipcar and get $50 in free driving Join Zipcar and get $50 in free driving Join Zipcar and get $50 in free driving

0 Comments


Be the first to comment!


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>