We help IT Professionals succeed at work.
Get Started

Optimizing VBScript for adding local admin group

fifko
fifko asked
on
490 Views
Last Modified: 2013-12-26
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

Comment
Watch Question
Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009
Commented:
This problem has been solved!
Unlock 2 Answers and 4 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE