Link to home
Start Free TrialLog in
Avatar of fifko
fifko

asked on

Optimizing VBScript for adding local admin group

I was given a task today to clean and optimize a VBS thats executed as part of a logon policy. I will work on it by myself, but wanted to check (and compare :P) with some of the real pros in here, how would You deal with it.

The point is to make it work for german (LCID 1031) and english (LCID 1033) OS version.

Thanks in advance.
Dim objFSO, objFSOText, objFolder, objFile

strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colOS = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
For Each OS In colOS
OSLanguage = OS.OSLanguage
Next

If OSLanguage = "1031" Then

Set objNetwork = WScript.CreateObject("WScript.Network")
Set objGroup = GetObject("WinNT://" & objNetwork.ComputerName & "/Administratoren,group")
Set objFSO = CreateObject("Scripting.FileSystemObject")


logfile = "\\itsdonefile\log$\add_local_admins\" & objnetwork.computername & ".txt"
Set objFile = objFSO.CreateTextFile(logfile,true,false)
 
strArgument = "itsdone\secgrp_lokaler_admin"
                x = InStr(strArgument,"\")

                If X>0 Then

                                Domain_Name = Left(strArgument,x-1)
                                Admin_Name = Right(strArgument,Len(strArgument)-x)
                                DNPath = "WinNT://" & Domain_Name & "/" & Admin_Name
                                
                                On Error Resume Next
                                If Not objGroup.IsMember(DNPath) Then 
					objGroup.Add(DNPath)
					objfile.write("Admingruppe: "&dnpath& " hinzugefügt"&";"&Date&";"&time)
				Else
					objfile.write("Admingruppe: "&dnpath& " war bereits hinzugefügt"&";"&Date&";"&time)
				end If
				
                End If      

objFile.close
Set objGroup = Nothing
set objNetwork = Nothing 

Else

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


logfile = "\\itsdonefile\log$\add_local_admins\" & objnetwork.computername & ".txt"
Set objFile = objFSO.CreateTextFile(logfile,true,false)
 
strArgument = "itsdone\secgrp_lokaler_admin"
                x = InStr(strArgument,"\")

                if X>0 Then

                                Domain_Name = Left(strArgument,x-1)
                                Admin_Name = Right(strArgument,Len(strArgument)-x)
                                DNPath = "WinNT://" & Domain_Name & "/" & Admin_Name
                                
                                On Error Resume Next
                                If Not objGroup.IsMember(DNPath) Then 
					objGroup.Add(DNPath)
					objfile.write("Admingruppe: "&dnpath& " hinzugefügt"&";"&Date&";"&time)
				Else
					objfile.write("Admingruppe: "&dnpath& " war bereits hinzugefügt"&";"&Date&";"&time)
				end If
				
                End If      

objFile.close
Set objGroup = Nothing
set objNetwork = Nothing 


End If

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of fifko
fifko

ASKER

Good idea angelIII, thanks.

So far, it looks like this:
On Error Resume Next

Dim objFSO	:	Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objFile	:	Set objFile = objFSO.CreateTextFile("\\itsdonefile\log$\add_local_admins\" & objNetwork.ComputerName & ".txt", true, false)

strComputer = "."
strArgument = "itsdone\SecGrp_Lokaler_Admin"
Set objNetwork = WScript.CreateObject("WScript.Network")
Set objGroup = GetObject(strGroup)
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colOS = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
For Each OS In colOS
	If OS.OSLanguage = "1031" Then
			strGroup = "WinNT://" & objNetwork.ComputerName & "/Administratoren,group"
		Else
			strGroup = "WinNT://" & objNetwork.ComputerName & "/Administrators,group"
	End If
Next

X = InStr(strArgument,"\")
	If X > 0 Then
			Domain_Name = Left(strArgument,x-1)
			Admin_Name = Right(strArgument,Len(strArgument)-x)
			DNPath = "WinNT://" & Domain_Name & "/" & Admin_Name
		If Not objGroup.IsMember(DNPath) Then
				objGroup.Add(DNPath)
				objFile.Write("Admingruppe: " & DNPath & " hinzugefügt" & ";" & Now)
			Else
				objFile.Write("Admingruppe: " & DNPath & " war bereits hinzugefügt" & ";" & Now)
		End If
	End If

objFile.close
Set objGroup = Nothing
set objNetwork = Nothing

Open in new window

Did you see my code and comments, it much the same, but I'd say your code is pretty well optimized now. That takes out the duplication.

Regards,

Rob.