# VBS script - Multiple functions



## StumpedTechy (Jul 7, 2004)

Okay I went over the creation of this script and hashed it out in my head on another thread - I had it working (only when the member was not an admin on the machine) and then I modified it slightly so I would not get script errors if I ended either of the prompt boxes early (by adding in the If statements for each prompt) My final working script is this -

strComputer = InputBox("Type in the Computer Name","Computer Name Variable")
IF strComputer = "" THEN
WScript.Echo "No Computer Name was given or you clicked Cancel"
WScript.Quit
END IF
strUID = InputBox("Enter Domain User Name", "Domain User Name Variable")
IF strUID = "" THEN
WScript.Echo "No Domain User Name was given or you clicked Cancel"
WScript.Quit
END IF
strCmdLine = "mstsc.exe /v:" & strComputer & " /f"
Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators")
Set objUser = GetObject("WinNT://Domain/" & strUID & "")
objGroup.Add(objUser.ADsPath)
Set WshShell = CreateObject("Wscript.Shell")
WshShell.Run strCmdLine, , True
Set WshShell = CreateObject("Wscript.Shell")
WshShell.Run strCmdLine, , True
objGroup.Remove(objUser.ADsPath)

Now the problem with this script as it is - If the user from some ungodly reason is already an Admin the script comes back with an error that the User already exists in this group.

The line it errors out on is - objGroup.Add(objUser.ADsPath)

I have also tried -

strComputer = InputBox("Type in the Computer Name","Computer Name Variable")
IF strComputer = "" THEN
WScript.Echo "No Computer Name was given or you clicked Cancel"
WScript.Quit
END IF
strUID = InputBox("Enter Domain User Name", "Domain User Name Variable")
IF strUID = "" THEN
WScript.Echo "No Domain User Name was given or you clicked Cancel"
WScript.Quit
END IF
strCmdLine = "mstsc.exe /v:" & strComputer & " /f"
Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators")
Set objUser = GetObject("WinNT://Domain/" & strUID & "")
objGroup.Add(objUser.ADsPath)
If Err.Number = 0 Then
Set WshShell = CreateObject("Wscript.Shell")
WshShell.Run strCmdLine, , True
MsgBox "Check to see why this user is an Adminuistator on this machine. They will not be removed from the Administrators group by this script. This must be done manually"
If Err.Number <> 0 Then
Set WshShell = CreateObject("Wscript.Shell")
WshShell.Run strCmdLine, , True
objGroup.Remove(objUser.ADsPath)
End If
End If

Trying to add in an error statement but this did not work. I think because this may not return an error number persay...I have also tried an if exist but I am not sure of the context to put that in to check back against the local computers admins group.

Basically I want where

No matter what the user ID is in the admins group.
I want to then kick off the Remote Desktop with the computer name

Lastly I want

objGroup.Remove(objUser.ADsPath) ran if the user was not an Admin on the machine already. Or if the user was in the Admins group it would run MsgBox "Check to see why this user is an Adminuistator on this machine. They will not be removed from the Administrators group by this script. This must be done manually"

I know I am on the cusp of having it working I just need direction in the objGroup.Add(objUser.ADsPath) and how to direct if this gives an error to do one thing and if it works right without an error to a seperate task.


----------



## Lycaon (Sep 19, 2005)

You're missing *On Error Resume Next*

Without it, any errors generated are handled immediately by the script interpreter (which immediately stops running your script) instead of being 'passed on' to your code.

Try this:


```
On Error Resume Next ' <-- Allows a script to continue running in the event of an error (like trying to add a user to a group they're already a member of)

objGroup.Add(objUser.ADsPath)

If Err.Number <> 0 Then ' If it's not 0, the user was NOT added to the group successfully.  Use MsgBox Err.Description to see why
    Set WshShell = CreateObject("Wscript.Shell")
    WshShell.Run strCmdLine, , True
    MsgBox "Check to see why this user is an Adminuistator on this machine. They will not be removed from the Administrators group by this script. This must be done manually"

End If

On Error Goto 0 ' *always* set error handling back to default until you're ready to use it again

' Rest of your script
```
Feel free to PM me if you need detailed help with your scripting.


----------



## StumpedTechy (Jul 7, 2004)

Lycaon I sent you a msg but never heard back from you -

I did finally manage to figure out what is up though -

strComputer = InputBox("Type in the Computer Name","Computer Name Variable")
IF strComputer = "" THEN
WScript.Echo "No Computer Name was given or you clicked Cancel"
WScript.Quit
END IF
strUID = InputBox("Enter Domain User Name", "Domain User Name Variable")
IF strUID = "" THEN
WScript.Echo "No Domain User Name was given or you clicked Cancel"
WScript.Quit
END IF
strCmdLine = "mstsc.exe /v:" & strComputer & " /f"
Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators")
Set objUser = GetObject("WinNT://Domain/" & strUID & "")
On Error Resume Next
objGroup.Add(objUser.ADsPath)
If Err.Number <> 0 Then 
Set WshShell = CreateObject("Wscript.Shell")
WshShell.Run strCmdLine, , True
MsgBox "Check to see why this user is an Adminuistator on this machine. They will not be removed from the Administrators group by this script. This must be done manually"
Else
Set WshShell = CreateObject("Wscript.Shell")
WshShell.Run strCmdLine, , True
objGroup.Remove(objUser.ADsPath)
End If

Your on error resume next took me to the right direction...

Ultimately it was the on error resume next and then it was also the else clause... I had forgot about that and this makes it work exactly like I had wanted.. it does the first part if it see the error and if no error then it does the second part.


----------



## Lycaon (Sep 19, 2005)

Sorry about that, computer took an impromptu vacation from sanity, so to speak :\


----------

