Page 1 of 1

domain disjoin script

Posted: Mon Jan 24, 2011 8:43 pm
by superfly12

I am trying to write a script which disjoin a server from a domain. I have got a piece of code from WMIcodecreater for this one. It works fine if i use the server name in the script.

strComputer = "."
Set objWMIService = GetObject("winmgmts:" & strComputer & "rootCIMV2")
' Obtain an instance of the the class
' using a key property value.
Set objShare = objWMIService.Get("Win32_ComputerSystem.Name='W*****19'")

' Obtain an InParameters object specific
' to the method.
Set objInParam = objShare.Methods_("UnjoinDomainOrWorkgroup"). _

' Add the input parameters.
objInParam.Properties_.Item("FUnjoinOptions") = 0

' Execute the method and obtain the return status.
' The OutParameters object in objOutParams
' is created by the provider.
Set objOutParams = objWMIService.ExecMethod("Win32_ComputerSystem.Name='W*****19'", "UnjoinDomainOrWorkgroup", objInParam)

' List OutParams
Wscript.Echo "Out Parameters: "
Wscript.echo "ReturnValue: " & objOutParams.ReturnValue

Instead of hardcoding the server name in the script, how do i make it to work on any other server like..
Set objShare = objWMIService.Get("Win32_ComputerSystem.Name='" & strComputer & "'"), but it gives an error SWbemServiceEx: Not found.
Please suggest.

domain disjoin script

Posted: Tue Jan 25, 2011 12:51 am
by jvierra
Just change the name of teh server before connecting to teh service.

Code: Select all

strComputer = "server2" 
Set wmi = GetObject("winmgmts:" & strComputer & "rootCIMV2") 
Set coll = wmi.ExecQuery("select * from Win32_ComputerSystem")  
For Each c In coll
     c.UnjoinDomainOrWorkgroup "mypassword", "myaadminaccout", 0
Don't know where you got the code but it looks to be a mix of mismatched parts. Just query teh computer for teh object and execute the method on the object.

The computer will not be removed from the domain until it is rebooted.

jvierra2011-01-25 07:57:24

domain disjoin script

Posted: Tue Jan 25, 2011 2:54 am
by jvierra
By th eway -

This is all of the code you need in powershell.

Code: Select all


domain disjoin script

Posted: Tue Jan 25, 2011 4:58 am
by superfly12
Thanks for you time to reply.There is data center movement scheduled and the servers should be renamed to reflect the location and i think they will be doing in bunches.You have mentioned that "YOu do not have to rejoin if you use the rename correctly.", does it mean that i don't have to disjoin and rejoin to reflect the new name ? I was thinking that i have to disjoin it first, then rename and reboot it and then join the domain. Please correct me if im wrong.

domain disjoin script

Posted: Sun Jun 17, 2012 5:19 pm
by charlinparker
I had gone through the post, We need to disjoin more than 20 computers from a domain, is there anyway we can do this by a script easily? It is a Server 2000 Domain they are on, 99% of them are Windows XP Pc's and rest are Windows 2000 PC's.
Please produce some more attachments about the topic for view detail information.domein
professionele web hosting