I am trying to modify the following script to allow me to:-
- Read from a list of computers in a csv
- Delete each computer from Activedirectory & SCCM 2012
I have very limited powershell experience, so any help would be greatly appreciated :)
Thanks Adi
# Environment setup
# Import the ActiveDirectory module to enable the Get-ADComputer CmdLet
Import-Module ActiveDirectory
$sccmServer = SCCMServer
$sccmSite = Site
$computerName = Import-CSV -Path Assets.csv
# find and delete the computer from AD
ForEach ($pc in $computers) {
$dom = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
$root = $dom.GetDirectoryEntry()
$search = [System.DirectoryServices.DirectorySearcher]$root
$search.filter = "(&(objectclass=computer)(name=$computerName))"
$search.findall() | %{$_.GetDirectoryEntry() } | %{$_.DeleteObject(0)}
# find and delete from SCCM
$comp = get-wmiobject -query "select * from SMS_R_SYSTEM WHERE Name='$computerName'" -computername $sccmServer -namespace "ROOT\SMS\site_$sccmSite"
$comp.psbase.delete()
}
# spit out results
Write-Host "Deleted $computerName from AD. Removed $computerName from SCCM server $sccmServer, site $sccmSite"