/build/static/layout/Breadcrumb_cap_w.png

Set Admin User Per System - VBScript

In setting up new systems a step I often forget in my environment is to set the user as Administrator.
With a couple of VBScripts and Kace2000 I managed to streamline this issue.

First I ran a simple pre-installation VBScript, "setUser.vbs" as a "Application" task for a "Windows Environment" as seen below.
This script prompts for the Employee's new system username and writes it to the T: drive using the systems MAC address to ensure the file is unique.

  strUser = InputBox("Please Enter A Username")

' --------------Get MAC Address------------
	MACStr = ""
	strComputer = "."
	Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
	Set colItems = objWMIService.ExecQuery( _
		"SELECT * FROM Win32_NetworkAdapterConfiguration",,48) 
		
		For Each objItem in colItems 
			MACStr = objItem.MACAddress
			if LEN(MACStr) then Exit For
		Next
	Set colItems = nothing
	Set objWMIService = nothing
' --------------Get MAC Address------------

Set objFileToWrite = CreateObject("Scripting.FileSystemObject").OpenTextFile("T:\" & Replace(MACStr,":","") & ".txt",2,true)
	objFileToWrite.WriteLine(lcase(strUser))
	objFileToWrite.Close
Set objFileToWrite = Nothing

' ------------------Get USERNAME------------
	strLine=""
	Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile("T:\" & Replace(MACStr,":","") & ".txt",1)
		do while not objFileToRead.AtEndOfStream
			 strLine = objFileToRead.ReadLine()
			 'Do something with the line
			 Exit Do
		loop
	objFileToRead.Close
	Set objFileToRead = Nothing
' ------------------Get USERNAME------------

Next as a Mid-Level Task after the image has been written to the new system another VBScript is run, "pullUsername.vbs".
This script using the MAC address finds the Text file written previously and writes it to the root of C: on the newly imaged system.
 ' --------------Get MAC Address------------
	MACStr = ""
	strComputer = "."
	Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
	Set colItems = objWMIService.ExecQuery( _
		"SELECT * FROM Win32_NetworkAdapterConfiguration",,48) 
		
		For Each objItem in colItems 
			MACStr = objItem.MACAddress
			if LEN(MACStr) then Exit For
		Next
	Set colItems = nothing
	Set objWMIService = nothing
' --------------Get MAC Address------------

' ------------------Get USERNAME------------
	strLine=""
	Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile("T:\" & Replace(MACStr,":","") & ".txt",1)
		do while not objFileToRead.AtEndOfStream
			 strLine = objFileToRead.ReadLine()
			 'Do something with the line
			 Exit Do
		loop
	objFileToRead.Close
	Set objFileToRead = Nothing
' ------------------Get USERNAME------------
	' msgbox("Username: " & strLine)
' ------------------Set USERNAME------------
	strUser = ""
	if LEN(strLine) then
		strUser = trim(lcase(strLine))
	end if
' ------------------Set USERNAME------------


Set objFileToWrite = CreateObject("Scripting.FileSystemObject").OpenTextFile("D:\user.txt",2,true)
	objFileToWrite.WriteLine(strUser)
	objFileToWrite.Close
Set objFileToWrite = Nothing


After Post Installation tasks have joined the system to the domain and rebooted, a VBScript task named "setAdmin.vbs" is launched.
This script reads the "user.txt" file and adds that username to the Administrators group of the new system.

Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\user.txt",1)
do while not objFileToRead.AtEndOfStream
     strLine = objFileToRead.ReadLine()
	 Exit Do
loop
objFileToRead.Close
Set objFileToRead = Nothing

strUser = trim(lcase(strLine))

' ---------------------- Set Admin ----------------------------
' -----------Update the variables to work with your domain-----
stradmin = "NTDOMAIN\Adminstrator"
strPassword = "secretpassword"

strDomain = "NT_AD_DOMAIN"
'--------------------------End SetAdmin------------------------

Const ADS_SECURE_AUTHENTICATION = &H1
Const ADS_USE_ENCRYPTION = &H2

Set objNetwork = CreateObject("WScript.Network")
strComputer = objNetwork.ComputerName
Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators,group")

Set objNS = GetObject("WinNT:")
Set objUser = objNS.OpenDSObject("WinNT://" & strDomain & "/" & strUser & ",user", _
    stradmin, strPassword, ADS_SECURE_AUTHENTICATION Or ADS_USE_ENCRYPTION)

If Not objGroup.IsMember(objUser.ADsPath) Then
    objGroup.Add(objUser.ADsPath)
    Wscript.Echo strUser & " added to local administrators."
End If

Lastly another custom Post Installation task batch cleans up the "user.txt" from the root of C:.


Comments

This post is locked

Don't be a Stranger!

Sign up today to participate, stay informed, earn points and establish a reputation for yourself!

Sign up! or login

Share

 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ