Add domain user to administrators group on remote machine (VBScript)

We have a support team that they need to our assistance about adding their accounts to local administrators group of some computers. My colleagues have to open “Computer Management”, connect to remote computer and add the user to the group. I don’t like this process.

The below script helps my colleagues, maybe useful for you as well:

'Version 1.0
'Written by Davoud Teimouri
Option Explicit
On Error Resume Next
Dim strComputer
Dim strUser
Dim objGroup
Dim ObjUser
strComputer = Inputbox ("Please enter the computer name (Without domain name):")
strUser= Inputbox ("Please enter the user name:")
strComputer=strComputer & ".Your Domain"
Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators,group")
Set objUser = GetObject("WinNT://Your Domain/" & strUser & ",user")
If Ping(strComputer) Then
    Err.Description=""
Else
    Err.Description=" The target is not reachable."
End If

If Err=0 Then
    objGroup.Add(objUser.ADsPath)
    Msgbox (strUser & " has been added to administrators group of " & strComputer)
Else
    Err.Description=" Please check the username."
    DisplayErrorInfo
End If

Sub DisplayErrorInfo

    WScript.Echo "An error is occurred." & Err.Description
    Err.Clear

End Sub

Function Ping(strHost)
    Dim oPing, oRetStatus, bReturn
    Set oPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("select * from Win32_PingStatus where address='" & strHost & "'")
 
    For Each oRetStatus In oPing
        If IsNull(oRetStatus.StatusCode) Or oRetStatus.StatusCode <> 0 Then
            bReturn = False
        Else
            bReturn = True

        End If
        Set oRetStatus = Nothing
    Next
    Set oPing = Nothing
 
    Ping = bReturn
End Function

Further Reading

Run ESXi Commands Via PowerShell And SSH

Windows 7 USB Sound Problem On PCoIP

How to change the listening port for Remote Desktop

Davoud Teimouri

Professional blogger, vExpert 2015/2016/2017/2018/2019/2020/2021/2022/2023, vExpert NSX, vExpert PRO, vExpert Security, vExpert EUC, VCA, MCITP. This blog is started with simple posts and now, it has large following readers.

2 Responses

  1. sunny says:

    it gives error, please check your username

Leave a Reply

Your email address will not be published. Required fields are marked *