Wednesday, December 30, 2009

List all installed software

This code is useful in documenting what is installed on a computer including Microsoft patches.  The original version I wrote formatted the output for a Wiki. The output is created in the same directory as the script, example: [mycomputer]-inventory.txt

Code – *.vbs

Option Explicit
Const HKLM = &H80000002 ' HKEY_LOCAL_MACHINE
Const APPEND = 8 ' Append to file
dim objShell
dim objFso
dim objReg
Set objShell = CreateObject("WScript.Shell")
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objReg = GetObject("WinMgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")

dim strRegIdentityCodes
dim strRegComputerName ' registry path to machine name
dim strComputerName ' computer name
strRegIdentityCodes = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
strRegComputerName = "HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\ComputerName"
strComputerName = objShell.RegRead(strRegComputerName)

' Create file delete if it already exists
dim strFileOut
strFileOut = strComputerName & "-Inventory.txt"
If objFso.FileExists(strFileOut) Then
objFso.DeleteFile strFileOut, true
End If
dim objFileOut
Set objFileOut = objFSO.CreateTextFile(strFileOut)

dim arrIdentityCode
objReg.EnumKey HKLM, strRegIdentityCodes, arrIdentityCode
objFileOut.WriteLine("Computer Name: " & strComputerName)
objFileOut.WriteLine("Inventory of installed products taken on: " & Now)

On Error Resume Next
dim strIdentityCode
dim strRegIdentityInfo
dim strDisplayName
dim strDisplayVersion
For Each strIdentityCode in arrIdentityCode
strRegIdentityInfo = "HKLM\" & strRegIdentityCodes & "\" & strIdentityCode & "\"
strDisplayName = objShell.RegRead(strRegIdentityInfo & "DisplayName")
strDisplayVersion = objShell.RegRead(strRegIdentityInfo & "DisplayVersion")
if(strDisplayName <> "")then
objFileOut.WriteLine(chr(9) & strIdentityCode & ", " & strDisplayVersion )
end if
strDisplayName = ""
strDisplayVersion =""

objShell.Run "cmd /c C:\WINDOWS\pchealth\helpctr\binaries\helpctr.exe -mode hcp://system/sysinfo/msinfo.xml", 1, true

Create Local Win NT User, Windows User

Two quick and easy methods of creating local NT users.  The samples (DOS and VBS) below create a user on the local system, the VBS method check to see if the user exists before adding.

NET USER DosUser UserPassword /add


Set WshNetwork = CreateObject("WScript.Network")
strComputerName = WshNetwork.ComputerName 'getting the machine name
checkuser = 1 
Set objComputer = GetObject("WinNT://" & strComputerName)
objComputer.Filter = Array("User")
For Each objUser In objComputer
    If (objUser.Name = strUserName) And (checkuser = 1) Then 
       checkuser = 0
   End If

If checkuser = 1 Then
   Set colaccounts = GetObject("WinNT://" & strComputerName & ",computer")
   Set objUser = colaccounts.Create("user", strUserName)
   objUser.SetPassword strPassword
End If