Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting a workstation 1

Status
Not open for further replies.
May 26, 2004
6
US
I'm trying to use the following script to delete a computer from a domain. It runs fine, but the computer is still listed in server manager. I cannot access the workstation, but I would like to know why the reference remains. Thanks.
'==========================================================

Sub RemoveComputer(strDomain, strComputer)
Dim objDC
Set objDC = GetObject("WinNT://" & strDomain)
objDC.Delete "Computer", strComputer
End Sub
'
'==========================================================
'Main
'
On Error Resume Next
Dim strDomain, strComputer
strDomain=InputBox("Please Enter A Domain.", "Input")
dtrComputer=Inputbox("Please Enter The Name Of The Computer", "Input")
RemoveComputer strDomain, strComputer
WScript.Echo "Done.
 
Hello TryRebooting,

Typo:
[red]s[/red]trComputer=Inputbox("Please Enter The Name Of The Computer", "Input")

regards - tsuji
 
Thanks for the correction tsuji. Any idea why the workstation is still shown?
 
TryRebooting,

Are you saying it's still there after you've corrected the typo?

- tsuji
 
TryRebooting,

I'm unconvinced, but I cannot ask for evidence obviously.

First, can you tell me why you put "on error resume next" there? I know... Do not answer.

Second, increase the overhead so as to check more thoroughly in the RemoveComputer like this for the moment. (Later you can save the .filter and use the script you are using.) Make it a function and at least control the return value (true/false).
Code:
Function RemoveComputer(strDomain, strComputer)
    Dim objDC
    RemoveComputer=false
    on error resume next
    Set objDC = GetObject("WinNT://" & strDomain)
    if err.number<>0 then
         exit function    'return false
    end of
    on error goto 0
    objDC.Filter = array("Computer")
    for each oItem in objDC
        if strcomp(oItem.name,strComputer,1)=0 then
            objDC.Delete "Computer", oItem.name
            RemoveComputer=true
            exit function
        end if
    next
End Sub
'On Error Resume Next
Dim strDomain, strComputer, bDone
strDomain=InputBox("Please Enter A Domain.", "Input")
strComputer=Inputbox("Please Enter The Name Of The Computer", "Input")
bDone=RemoveComputer(strDomain, strComputer)
msgbox "Operation completely successful : " & bDone,,"Windows Script Host"
You depend on two manual inputs and I do not see any control and/or validation of them. I would add more error controls up there if necessary.

- tsuji
 
Correction:

"End Sub" should be read "End Function" obviously.

- tsuji
 
How could you reverse this script and set a computer name and jion the domain. Then create a user profile loacal or domain.
 
Hello redlair,

It can reasonably be reversed for ntdomain.
Code:
Function CreateComputer(strDomain, strComputer)
    Dim objDC
    CreateComputer = false
    on error resume next
    Set objDC = GetObject("WinNT://" & strDomain)
    if err.number<>0 then
        exit function    'return false - cannot bind to domain
    else
        objDC.Filter = array("Computer")
        for each oItem in objDC
            if strcomp(oItem.name,strComputer,1)=0 then
                exit function    'return false - already existing
            end if
        next
        set oComputer = objDC.create("Computer",strComputer)
        if err.number<>0 then CreateComputer = true
        set oComputer = nothing
    end if
    set objDC = nothing
End Function
I have kept the maximum similarity with the remove operation otherwise you sure can have alternative ways to verify this or that within the function.

If you are going to do it in win2kdomain, some other standard methods are available.
[1] Using netdom, see
[2] For xp/2003, using wmi:
[3] Using LDAP and enable the account with security settings:

So, there are more involved than deletion.

regards - tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top