Remove from domain
Any suggestions as to why the below doesn't work?
Dim objDC, strDomain, strComputer
strDomain = "domain"
strComputer = "machine"
Sub RemoveComputer
On error resume next
Set objDC = getobject("WinNT://" & strDomain )
objDC.Delete "Computer", strComputer
if err = 0 Then
wscript.echo "RemoveComputer = true"
else
wscript.echo "RemoveComputer = false"
end if
On error goto 0
End Sub
Dim objDC, strDomain, strComputer
strDomain = "domain"
strComputer = "machine"
Sub RemoveComputer
On error resume next
Set objDC = getobject("WinNT://" & strDomain )
objDC.Delete "Computer", strComputer
if err = 0 Then
wscript.echo "RemoveComputer = true"
else
wscript.echo "RemoveComputer = false"
end if
On error goto 0
End Sub
0 Comments
[ + ] Show comments
Answers (11)
Please log in to answer
Posted by:
brenthunter2005
19 years ago
Posted by:
brenthunter2005
19 years ago
Posted by:
sean_c_roberts
19 years ago
Gack!
You WMI guys :)
I would have looked up the API call to unjoin my machine from the domain and used Call Dll Function to accomplish it... I would have JOINED the machine to a workgroup...
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/netmgmt/netmgmt/netjoindomain.asp
The NetJoinDomain function joins a computer to a workgroup or domain.
NET_API_STATUS NetJoinDomain(
LPCWSTR lpServer,
LPCWSTR lpDomain,
LPCWSTR lpAccountOU,
LPCWSTR lpAccount,
LPCWSTR lpPassword,
DWORD fJoinOptions
);
You WMI guys :)
I would have looked up the API call to unjoin my machine from the domain and used Call Dll Function to accomplish it... I would have JOINED the machine to a workgroup...
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/netmgmt/netmgmt/netjoindomain.asp
The NetJoinDomain function joins a computer to a workgroup or domain.
NET_API_STATUS NetJoinDomain(
LPCWSTR lpServer,
LPCWSTR lpDomain,
LPCWSTR lpAccountOU,
LPCWSTR lpAccount,
LPCWSTR lpPassword,
DWORD fJoinOptions
);
Posted by:
jwarner777
19 years ago
I have just started to get into scripting and everyone seems to assume that you know how to get a script to execute.
I can put code into a txt file till the cows come home but how do I actually get it to execute?!?!?!
Anyone know any good books that dont assume these things? I am a do'er once I have done something I can just build off of that but I cannot read a book and just figure out that this is what you do to get everything going.
NET_API_STATUS NetJoinDomain(
LPCWSTR lpServer,
LPCWSTR lpDomain,
LPCWSTR lpAccountOU,
LPCWSTR lpAccount,
LPCWSTR lpPassword,
DWORD fJoinOptions
);
Jason
I can put code into a txt file till the cows come home but how do I actually get it to execute?!?!?!
Anyone know any good books that dont assume these things? I am a do'er once I have done something I can just build off of that but I cannot read a book and just figure out that this is what you do to get everything going.
NET_API_STATUS NetJoinDomain(
LPCWSTR lpServer,
LPCWSTR lpDomain,
LPCWSTR lpAccountOU,
LPCWSTR lpAccount,
LPCWSTR lpPassword,
DWORD fJoinOptions
);
Jason
Posted by:
glwday
19 years ago
Posted by:
brenthunter2005
19 years ago
Posted by:
glwday
19 years ago
Posted by:
brenthunter2005
19 years ago
OK, first of all your script needs to read the contents of this text file/spreadsheet into an array and then delete the computer account from the domain. Something like this would suffice if the text file were simple:
Text file called 'computers.txt':
computer01
computer03
computer05
server04
computer09
computer10
Script:
strTextFile = "C:\temp\computers.txt"
strDomain = "DOMAINNAME"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objDC = GetObject("WinNT://" & strDomain )
Set objFile = objFSO.OpenTextFile(strTextFile,1)
Do Until objFile.AtEndOfTextStream <> TRUE
strLine = objFile.ReadLine
objDC.Delete "Computer", strLine
Loop
objFile.Close
Set objFSO = Nothing
Set objDC = Nothing
wscript.echo "Completed"
Text file called 'computers.txt':
computer01
computer03
computer05
server04
computer09
computer10
Script:
strTextFile = "C:\temp\computers.txt"
strDomain = "DOMAINNAME"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objDC = GetObject("WinNT://" & strDomain )
Set objFile = objFSO.OpenTextFile(strTextFile,1)
Do Until objFile.AtEndOfTextStream <> TRUE
strLine = objFile.ReadLine
objDC.Delete "Computer", strLine
Loop
objFile.Close
Set objFSO = Nothing
Set objDC = Nothing
wscript.echo "Completed"
Posted by:
UcMerrill
19 years ago
On Error Resume Next
WScript.Echo "Start"
LOcal_Admin_Acct = "administrator"
LOcal_Admin_PW = "password1"
Workgroup_To_Join = "workgroup"
Path = "C:\"
Set E_Log = CreateObject("Scripting.FileSystemObject").OpenTextFile(Path & "Log.txt", 8, True)
DeviceList = CreateObject("Scripting.FileSystemObject").OpenTextFile(Path & "WS.txt").ReadAll
Set wshshell = CreateObject("wscript.shell")
Set WshNetwork = WScript.CreateObject("WScript.Network")
For Each DeviceName in split(DeviceList,vbcrlf)
Wshshell.run "cmd /c C:\netdom.exe /u:" & DeviceName & "\" & LOcal_Admin_Acct & " /p:" & LOcal_Admin_PW & " MEMBER \\" & DeviceName & " /JOINWORKGROUP " & Workgroup_To_Join & " >> C:\netdom.txt", 0, True
Get_Results Devicename
Next
wscript.echo "Done"
Set WshNetwork = Nothing
Set Wshshell = Nothing
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sub Get_Results(DeviceName)
Result = CreateObject("Scripting.FileSystemObject").OpenTextFile("c:\netdom.txt").ReadAll
For Each Line_Result in split(Result,vbcrlf)
If instr(Line_Result,"successfully") > 0 then
E_Log.writeline Devicename & vbtab & Line_Result & vbtab & Now() & VbCrLf
End If
Next
End Sub
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Function Get_USerID
NT_ID = InputBox("Enter Domain Admin Acct. required to join Domain: ", "Get Valid UserID")
If NT_ID = "" Then
Msgbox "You did not enter anything for USerID...... Terminating Script!", 16, "No Account Entered"
wscript.quit
Else
Get_USerID = NT_ID
End If
End Function
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Function Get_Password
Password = InputBox("Enter Password:", "Enter Password")
If Password = "" then
Msgbox "You did not enter anything for Password...... Terminating Script!", 16 , "No Password Entered"
wscript.quit
Else
Get_Password = Password
End If
End Function
LOcal_Admin_Acct = this is the local administrator account for the pc (most likely Administrator )
LOcal_Admin_PW = This is the local admin passowrd ( ????)
Workgroup_To_Join = this is the workgroup name you would like your PC's to join
netdom should be placed under the c drive also you should place it in system32
Path = Here is where you will have to place you list of workstaion in a text file under the c drive ( C:\ws.txt)
In the ws.txt the pcs shoud have no spaces in their name. eg
computer1
computer2
computer3
computer4
computer5
computer6
etc
* Not all versions of NETDOM works the same.
Enjoy
WScript.Echo "Start"
LOcal_Admin_Acct = "administrator"
LOcal_Admin_PW = "password1"
Workgroup_To_Join = "workgroup"
Path = "C:\"
Set E_Log = CreateObject("Scripting.FileSystemObject").OpenTextFile(Path & "Log.txt", 8, True)
DeviceList = CreateObject("Scripting.FileSystemObject").OpenTextFile(Path & "WS.txt").ReadAll
Set wshshell = CreateObject("wscript.shell")
Set WshNetwork = WScript.CreateObject("WScript.Network")
For Each DeviceName in split(DeviceList,vbcrlf)
Wshshell.run "cmd /c C:\netdom.exe /u:" & DeviceName & "\" & LOcal_Admin_Acct & " /p:" & LOcal_Admin_PW & " MEMBER \\" & DeviceName & " /JOINWORKGROUP " & Workgroup_To_Join & " >> C:\netdom.txt", 0, True
Get_Results Devicename
Next
wscript.echo "Done"
Set WshNetwork = Nothing
Set Wshshell = Nothing
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sub Get_Results(DeviceName)
Result = CreateObject("Scripting.FileSystemObject").OpenTextFile("c:\netdom.txt").ReadAll
For Each Line_Result in split(Result,vbcrlf)
If instr(Line_Result,"successfully") > 0 then
E_Log.writeline Devicename & vbtab & Line_Result & vbtab & Now() & VbCrLf
End If
Next
End Sub
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Function Get_USerID
NT_ID = InputBox("Enter Domain Admin Acct. required to join Domain: ", "Get Valid UserID")
If NT_ID = "" Then
Msgbox "You did not enter anything for USerID...... Terminating Script!", 16, "No Account Entered"
wscript.quit
Else
Get_USerID = NT_ID
End If
End Function
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Function Get_Password
Password = InputBox("Enter Password:", "Enter Password")
If Password = "" then
Msgbox "You did not enter anything for Password...... Terminating Script!", 16 , "No Password Entered"
wscript.quit
Else
Get_Password = Password
End If
End Function
LOcal_Admin_Acct = this is the local administrator account for the pc (most likely Administrator )
LOcal_Admin_PW = This is the local admin passowrd ( ????)
Workgroup_To_Join = this is the workgroup name you would like your PC's to join
netdom should be placed under the c drive also you should place it in system32
Path = Here is where you will have to place you list of workstaion in a text file under the c drive ( C:\ws.txt)
In the ws.txt the pcs shoud have no spaces in their name. eg
computer1
computer2
computer3
computer4
computer5
computer6
etc
* Not all versions of NETDOM works the same.
Enjoy
Posted by:
glwday
19 years ago
I found this straight forward line to remove a computer works
NETDOM /Domain:Domain01 MEMBER BS0681618 /DELETE
From there it was easy to use the concatenate function in excel. First create three columns
Column A Column B Column C
NETDOM /Domain:Domain01 MEMBER - accountto remove - /DELETE
Concatenate lets you join the values in different cells. In Col D enter
=CONCATENATE(A1,B1,C1) and you get
NETDOM /Domain:Domain01 MEMBER accounttoremove /DELETE
Then copy the formula down for the number of assets you have in column B. then I copy and paste the joined up entries into a batch file, and job done ready to run.
This is on a Windows 2000 without AD.
NETDOM /Domain:Domain01 MEMBER BS0681618 /DELETE
From there it was easy to use the concatenate function in excel. First create three columns
Column A Column B Column C
NETDOM /Domain:Domain01 MEMBER - accountto remove - /DELETE
Concatenate lets you join the values in different cells. In Col D enter
=CONCATENATE(A1,B1,C1) and you get
NETDOM /Domain:Domain01 MEMBER accounttoremove /DELETE
Then copy the formula down for the number of assets you have in column B. then I copy and paste the joined up entries into a batch file, and job done ready to run.
This is on a Windows 2000 without AD.
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
so that the conversation will remain readable.