1

I am putting together a script to run post-sysprep.

It should check if the machine is on the network, and if it is then it should query AD to see if a computer account exists with it's service tag (we're using these as the hostnames of the machines.)

If it does exist, it should delete the account and rejoin the machine to the domain.

I have got the majority of the script running, but need to run the following:

Remove-ADComputer -Identity $distinguishedName

How can I run this from the "standard" powershell environment? I don't want to use the AD module.

(By the way - I'm on a mixed mode 2000/03 domain as we are in the process of upgrading to 2008)

I'm new to PowerShell so be gentle if I'm completely missing the point!

Thanks,

Ben

3 Answers 3

3

DSRM will work fine, too.

However, if you want to stay 100% PowerShell, you can use the ADSI provider to accomplish this natively within PowerShell without modules.

 $computer = "computername"
 $dom = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()            
 $root = $dom.GetDirectoryEntry()            
 $search = [System.DirectoryServices.DirectorySearcher]$root            
 $search.filter = "(&(objectclass=computer)(name=$computer))"            
 $search.findall() | foreach-object{$_.GetDirectoryEntry() } | foreach-object{$_.DeleteObject(0)} 
1
  • I'd vote for dsrm its less complex!
    – tony roth
    May 30, 2010 at 6:07
1

If you want to run a command from the AD module (such as Remove-ADComputer) you are going to have to import that module.

You could try using the dsrm.exe utility to remove the object. See http://technet.microsoft.com/en-us/library/cc755841(WS.10).aspx for usage details on dsrm.exe.

Thanks, Mark

0

yea why over complicate things just use dsrm, remeber ps is a a full cli in that you don't have to do anything to get dsrm to work.

so within powershell, it would look like this

ps>dsrm $distinguishedName

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .