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.
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
Regards,
Rob.
ASKER
So far, it looks like this:
Open in new window