domain disjoin script

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 11 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
superfly12
Posts: 133
Last visit: Sun Nov 02, 2014 4:37 pm

domain disjoin script

Post by superfly12 »

Hi,

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"). _
inParameters.SpawnInstance_()


' 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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

domain disjoin script

Post 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
Next
	
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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

domain disjoin script

Post by jvierra »

By th eway -

This is all of the code you need in powershell.

Code: Select all

$computer='somepc'
[wmi]"
$computerrootcimv2:win32_computersystem.Name=`'$computer`'").UnjoinDomainOrWorkgroup()
User avatar
superfly12
Posts: 133
Last visit: Sun Nov 02, 2014 4:37 pm

domain disjoin script

Post 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.
User avatar
charlinparker
Posts: 1
Last visit: Sun Jun 17, 2012 5:19 pm

domain disjoin script

Post 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
This topic is 11 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked