Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
''-------------------------------------------------------------------------------------
'' ====================================================================================
'' TITLE: Get Domain PcName their OS and Service Pack
''
'' AUTHOR: Christian Sawyer
'' DATE: 09/22/2004
'' COMPANY: Implanciel Inc.
'' EMAIL: csawyer@implanciel.com
''
'' PURPOSE: Lists all computers, their OS, and service pack in a domain.
'' Generate an Excel report file or a text file based on user answer.
'' ====================================================================================
Option Explicit
Dim objXL
Dim objFSO
Dim objWorkFile
Dim intRow
Dim strXL_TXT
''Ask user if he want the output as a text file or an Excel file.
strXL_TXT = InputBox("What format of output file do you want? EXCEL or TEXT", "Output Format", "Excel")
If IsNoData(strXL_TXT) Then
strXL_TXT = "Excel"
Wscript.Echo "You didn''t type a choice. I choose Excel for you."
Else
If InStr(UCase(strXL_TXT), "EXC") Then
''Create an Excel instance.
Set objXL = CreateObject("Excel.Application")
intRow = 2
'' Create the Output Excel spreadsheet.
Call BuildSpreadSheet
'' Reassign correct value in case of mistyped letter.
strXL_TXT = "Excel"
ElseIf InStr(UCase(strXL_TXT), "TEX") Then
'' Create a File System Object Instance
Set objFSO = CreateObject("Scripting.FileSystemObject")
'' Create the Output Text file.
Call CreateTextFile
'' Reassign correct value in case of mistyped letter.
strXL_TXT = "Text"
Else
Wscript.Echo "Letters you typed didn''t correspond to Excel or Text choice." & _
"Please restart this script and type Excel or Text as your choice."
Wscript.Quit
End If
End If
'' Call the main function
GetPcOsSPFromDomain
''-------------------------------------------------------------------------------------
'' ====================================================================================
'' TITLE: GetDomainPcNameOsSp
''
'' AUTHOR: Christian Sawyer
'' DATE: 09/22/2004
'' COMPANY: Implanciel Inc.
'' EMAIL: csawyer@implanciel.com
''
'' PURPOSE: Lists all computers, their OS, and service pack in a domain.
'' Save output in Excel or Text file depending of strXL_TXT value.
''
'' HOW TO USE: GetPcOsSPFromDomain
'' ====================================================================================
Function GetPcOsSPFromDomain()
On Error Resume Next
Dim objConn
Dim objCmd
Dim objRS
Dim strDomain
Dim strCmd
Dim strName
ReDim arrOsSP(5)
'' Initialize connection to AD.
Set objConn = CreateObject("ADODB.Connection")
objConn.Open "Provider = ADsDSOObject;"
Set objCmd = CreateObject("ADODB.Command")
objCmd.ActiveConnection = objConn
'' Retrieve FQDN.
strDomain = GetDNDomainName
'' Build query to execute.
strCmd = ">;(|(objectCategory=computer));cn,dnsHostName,OperatingSystem,OperatingSystemVersion,OperatingSystemServicePack;subtree"
'' Add query to command object.
objCmd.CommandText = "<LDAP://" & strDomain & strCmd
'' Execute the command.
Set objRS = objCmd.Execute
'' Verify if query return something. If not, display a message and quit.
If objRS.RecordCount = 0 Then
WScript.Echo "Not Found"
WScript.Quit
Else
'' Loop through recordset for each computer in AD and retrieve properties.
Do While Not objRS.EOF
arrOsSP(0) = objRS("cn") ''Computer Name
arrOsSP(1) = objRS("dnsHostName") ''dns Host Name
arrOsSP(2) = objRS("OperatingSystem") ''OS
arrOsSP(3) = objRS("OperatingSystemVersion") ''OS Version
arrOsSP(4) = objRS("OperatingSystemServicePack") ''OS Service Pack
If strXL_TXT = "Excel" Then
'' Add each entries in spreadsheet.
Call AddToSpreadSheet(arrOsSP)
Else
'' Add each entries in text file.
Call AddToFile(arrOsSP)
End If
objRS.MoveNext
Loop
End If
If strXL_TXT = "Excel" Then
'' Save spreadsheet.
SaveSpreadSheet
End If
objConn.Close
Set objConn = Nothing
Set objCmd = Nothing
Set objRS = Nothing
Set objXL = Nothing
End Function
''-------------------------------------------------------------------------------------
'' ====================================================================================
'' TITLE: GetDNDomainName
''
'' PURPOSE: Retrieve Distinguished domain name from AD.
'' Return only the domain name in form DC=domain,DC=local
''
'' HOW TO USE: strResult = GetDNDomainName()
''
'' ====================================================================================
Function GetDNDomainName()
Dim objRootDSE
Dim objDomain
'' Connect to AD Root.
Set objRootDSE = GetObject("LDAP://RootDSE")
'' Get domain name.
Set objDomain = GetObject("LDAP://" & objRootDSE.Get("DefaultNamingContext"))
'' Retrieve Distinguished Domain Name (DC=your domain name,DC=local)
GetDNDomainName = objDomain.DistinguishedName
Set objRootDSE = Nothing
Set objDomain = Nothing
End Function
''-------------------------------------------------------------------------------------
'' ====================================================================================
'' TITLE: CreateTextFile
''
'' PURPOSE: Create a text file named OS_SP_Inventory.txt in same path
'' from where this script is executed.
''
'' HOW TO USE: Call CreateTextFile
''
'' ====================================================================================
Sub CreateTextFile()
Dim strPath
Dim strFileName
'' Retrieve the complete path of the current running script.
strPath = GetScriptPath
'' Build path and filename for inventory output file
strFileName = strPath & "OS_SP_Inventory.txt"
'' Create the output file.
Set objWorkFile = objFSO.OpenTextFile(strFileName, 2, True)
'' Create Header in output file.
'' " 22 " " 30 " " 30 " " 15 " " 15 "
objWorkFile.WriteLine "Computer Name | DNS Host Name | Operating System | OS Version | Service Pack "
End Sub
''-------------------------------------------------------------------------------------
''=====================================================================================
'' TITLE: BuildSpreadSheet
''
''
'' PUPPOSE: Create an Excel file to be filled by AddToSpreadSheet.
'' Format 4 columns.
''
'' HOW TO USE: BuildSpreadSheet
''
'' ====================================================================================
Sub BuildSpreadSheet()
objXL.Visible = True
objXL.WorkBooks.Add()
objXL.Sheets("Sheet1").Select()
objXL.Sheets("Sheet1").Name = "OS & SP per Computer Account"
objXL.Sheets("Sheet2").Select()
objXL.ActiveWindow.SelectedSheets.Delete
objXL.Sheets("Sheet3").Select()
objXL.ActiveWindow.SelectedSheets.Delete
objXL.Columns(1).ColumnWidth = 22
objXL.Columns(2).ColumnWidth = 30 ''Change it if you need more space for dns host name.
objXL.Columns(3).ColumnWidth = 30
objXL.Columns(4).ColumnWidth = 15
objXL.Columns(5).ColumnWidth = 15
objXL.Cells(1, 1).Value = "Computer Name"
objXL.Cells(1, 2).Value = "DNS Host Name"
objXL.Cells(1, 3).Value = "Operating System"
objXL.Cells(1, 4).Value = "OS Version"
objXL.Cells(1, 5).Value = "Service Pack"
objXL.Range("A1:E1").Select
objXL.Selection.Font.Bold = True
objXL.Selection.Font.Size = 12
objXL.Selection.HorizontalAlignment = 3
End Sub
''-------------------------------------------------------------------------------------
'' ====================================================================================
'' TITLE: AddToFile
''
'' PURPOSE: Fill the created output text file with value from each Computer
'' found in AD. Use Pad function to make each entry equal in text
'' file.
''
'' HOW TO USE: Call AddToFile(Array)
''
'' ====================================================================================
Sub AddToFile(arrOSSP)
Dim strPCName
Dim strDNS
Dim strOS
Dim strOSVer
Dim strSP
strPCName = Pad(arrOSSP(0),22,False)
strDNS = Pad(arrOSSP(1),30,False) ''Change 30 for greater number if needed.
strOS = Pad(arrOSSP(2),30,False)
strOSVer = Pad(arrOSSP(3),15,False)
strSP = Pad(arrOSSP(4),15,False)
objWorkFile.WriteLine(strPCName & "| " & strDNS & "| " & strOS & "| " & strOSVer & "| " & strSP)
End Sub
''-------------------------------------------------------------------------------------
'' ====================================================================================
'' TITLE: Pad
''
'' PURPOSE: Fill the created output text file with value from each Computer
'' found in AD. Use Pad function to make each entry equal in text
'' file.
''
'' HOW TO USE: Pad("Any string", Lenght of passed string, True or False)
'' True = Padding will be infront of passed string.
'' False = Padding will be at the end of passed string.
''
'' ====================================================================================
Function Pad(strText, intLen, blnFront)
Dim intStartLen
If IsNoData(strText) Then strText = "N/A"
intStartLen = Len(strText)
If Len(strText) >= intLen Then
Pad = strText
Else
If blnFront Then
Pad = String(intLen - intStartLen, " ") & strText
Else
Pad = strText & String(intLen - intStartLen, " ")
End If
End If
End Function
''-------------------------------------------------------------------------------------
''=====================================================================================
'' TITLE: AddToSpreadSheet
''
'' PUPPOSE: Add anything in an Excel file spread in 4 columns.
'' In this case, add machine name processed in column 1,
'' Operating System in column 2, OS Version in column 3,
'' Service pack in column 4.
''
'' PARAMETERS: arrNetwork, an array containing 4 first values.
''
'' HOW TO USE: Call AddToSpreadSheet(arrResult)
''
'' ====================================================================================
Sub AddToSpreadSheet(arrOSSP)
objXL.Sheets("OS & SP per Computer Account").Select()
objXL.Cells(intRow, 1).Value = arrOSSP(0)
objXL.Cells(intRow, 2).Value = arrOSSP(1)
objXL.Cells(intRow, 3).Value = arrOSSP(2)
objXL.Cells(intRow, 4).Value = arrOSSP(3)
objXL.Cells(intRow, 5).Value = arrOSSP(4)
intRow = intRow + 1
End Sub
''-------------------------------------------------------------------------------------
''=====================================================================================
'' TITLE: SaveSpreadSheet
''
'' PUPPOSE: Save the created Excel file with prefix OS_SP_InDomain_
'' + DomainName + Current Date.
'' Save it in directory where this Script is executed from.
''
'' HOW TO USE: SaveSpreadSheet
''
''=====================================================================================
Sub SaveSpreadSheet()
Dim dtmDate
Dim strDomainName
Dim strDestFileName
Dim strFileName
'' Store current date and replace (/) by (-).
dtmDate = Date
dtmDate = Replace(dtmDate, "/", "-")
'' Parse strDomain variable to extract Domain name.
strDomainName = GetDomainName()
'' Find script path to save Excel file in it with prefix + domain name
'' + current date and .xls extension.
strDestFileName = GetScriptPath() & "OS_SP_InDomain_" & strDomainName & "_" & dtmDate & ".xls"
strFileName = objXL.GetSaveAsFilename(strDestFileName)
If strFileName <> False Then
objBook.SaveAs(strFileName)
End If
End Sub
''-------------------------------------------------------------------------------------
'' ====================================================================================
'' TITLE: GetDomainName
''
'' PURPOSE: Retrieve domain name from Wscript.Network class.
'' Return only the domain name
''
'' HOW TO USE: strResult = GetDomainName()
''
'' ====================================================================================
Function GetDomainName()
Dim objNet
Set objNet = CreateObject("Wscript.Network")
GetDomainName = objNet.UserDomain
Set objNet = Nothing
End Function
''-------------------------------------------------------------------------------------
'' ====================================================================================
'' TITLE: GetScriptPath
''
'' PURPOSE: Find the path where the current script were executed from.
'' Used to save any file in same path where the script reside.
''
'' HOW TO USE: strResult = GetScriptPath()
''
'' ====================================================================================
Function GetScriptPath()
GetScriptPath = Mid(WScript.ScriptFullName, 1, InStrRev(WScript.ScriptFullName,"\"))
End Function
''-------------------------------------------------------------------------------------
''=====================================================================================
'' TITLE: IsNoData
''
'' PURPOSE: Verify if passed parameter variable contain something.
''
'' RETURN: True if contain something, otherwise, False.
''
'' HOW TO USE: intResult = IsNoData(varSource) or
'' If IsNoData(varSource) Then do something.
''
'' AUTHOR: Christian Sawyer
'' ====================================================================================
Function IsNoData(varVal2Check)
''Verify if varVal2Check contain something.
On Error Resume Next
If IsNull(varVal2Check) Or IsEmpty(varVal2Check) Then
IsNoData = True
Else
If IsDate(varVal2Check) Then
IsNoData = False
ElseIf varVal2Check = "" Then
IsNoData = True
ElseIf Not IsObject(varVal2Check) Then
IsNoData = False
Else
IsNoData = False
End If
End If
End Function