Skip to content

Instantly share code, notes, and snippets.

@beancurd1
Last active August 29, 2017 03:04
Show Gist options
  • Save beancurd1/d6107ba69a1b2ce5dd12c2395f0a5ffd to your computer and use it in GitHub Desktop.
Save beancurd1/d6107ba69a1b2ce5dd12c2395f0a5ffd to your computer and use it in GitHub Desktop.
It collects Local Admin Users and AV on a machine and output the result to a csv file on a shared location. It can be assigned to logon or shutdown script.
' Created this for monthly compliance purpose, it collects Local Admin Users and AV on a machine
' Get software list from Registry Key. It can only list software installed by installer (*.exe & *.msi)
' Won't work with Preinstalled, Portable apps
' NOTE: I collected codes from few websites and didn't take a note of them. Please let me know
' if you found some portion of the code belongs to you. I will add a credit asap.
' REF: https://gallery.technet.microsoft.com/scriptcenter/8035d5a9-dc92-436d-a60c-67d381da15a3
If WScript.Arguments.Count <> 0 then
' assign parameter to strComputerName
strComputerName = WScript.Arguments(0)
Else
' assign local computer name to strComputerName if no parameters are passed
strComputerName = getComputerName()
End If
strFilter = "Trend Micro" ' Filter the output, we only interested in AV products
Set objShell = CreateObject("WScript.Shell")
' Join the arrays returned from functions with , delimiter
strLocalAdmins = join(getLocalAdmins(strComputerName), ", ")
strApps = join(getSoftware(strFilter, strComputerName), "; ")
' Write out the result to a file'
''ts.WriteLine """ComputerName"",""Local Admins"",""AV"",""Date"""
strResult = """" & strComputerName & """" & "," & _
"""" & strLocalAdmins & """" & "," & _
"""" & strApps & """" & "," & _
"""" & Now & """"
'' Wscript.Echo strResult 'uncomment it for debugging'
objShell.LogEvent 4, strResult
strOutput = "\\SHARELOCATION\Asset\" & strComputerName & "_LAVS.csv" ' Result will save to a CSV file
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.CreateTextFile(strOutput)
ts.WriteLine strResult
ts.Close
Set ts = Nothing
Set fso = Nothing
'********************************************************************'
'*************************[ Functions ]*************************
'********************************************************************'
' Return an array of Software installed on a Computer
Function getSoftware(strFilter, strComputerName)
Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE
strKey = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"
strDisplayName = "DisplayName"
strVerMajor = "VersionMajor"
strVerMinor = "VersionMinor"
Dim arrSoftwares() ' Dynamic Array
count = 0
'Assign the reg key where software installation is kept to a variable
Set objReg = GetObject("winmgmts://" & strComputerName & "/root/default:StdRegProv")
objReg.EnumKey HKLM, strKey, arrSubkeys
'Loop through each Subkey and list collect the software name defined in the Filter parameter
For Each strSubkey In arrSubkeys
intRet1 = objReg.GetStringValue(HKLM, strKey & strSubkey, strDisplayName, strAppName)
If intRet1 <> 0 Then
objReg.GetStringValue HKLM, strKey & strSubkey, strEntry1b, strAppName
End If
If strAppName <> "" and InStr(1, strAppName, strFilter, vbTextCompare) Then
objReg.GetDWORDValue HKLM, strKey & strSubkey, strVerMajor, intVerMajor
objReg.GetDWORDValue HKLM, strKey & strSubkey, strVerMinor, intVerMinor
If intVerMajor <> "" Then
ReDim Preserve arrSoftwares(count)
arrSoftwares(count) = strAppName & ", v" & intVerMajor & "." & intVerMinor
count = count + 1
End If
End If
Next
getSoftware = arrSoftwares
End Function
' Return an array of Users in a User Group
Function getLocalAdmins(strComputerName)
Set objGroup = GetObject("WinNT://" & strComputerName & "/Administrators,group")
Dim arrUsers() ' Dynamic Array
count = 0
For Each objUser In objGroup.Members
Set objItem = GetObject("WinNT:// " & strComputerName & objUser.Name)
ReDim Preserve arrUsers(count)
arrUsers(count) = objUser.Name
count = count + 1
Next
getLocalAdmins = arrUsers
End Function
' Return a local computer/host name
Function getComputerName()
Set wshNetwork = CreateObject( "WScript.Network" )
getComputerName = wshNetwork.ComputerName
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment