‘ This script simply displays three details about the computer

‘ These are displayed in individual pop-up windows
‘======================================================================
‘======================================================================
‘ Option Explicit means that variables in the script must be declared
‘ before they are used in the code

Option Explicit

‘ On Error Resume Next makes the script continue on to the next
‘ line instead of aborting if there is an error

‘ On Error Resume Next

‘ Dim decares the variables in the script

Dim objShell, objFileSystem, objFile, strLine, strCommand, ReturnCode, IP
Dim regServicePackNumber, regActiveComputerName
Dim ProductName, ServicePackNumber, ActiveComputerName
‘The Below is needed for lines 44 through 54
Dim strComputer, colOperatingSystems, objWMIService, objOperatingSystem, OperatingSystem, OSv

regServicePackNumber = “HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\CSDVersion”

regActiveComputerName = “HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Hostname”

 

‘ A file system object is created in order to access
‘ different things from the file system

Set objShell = CreateObject(“WScript.Shell”)
Set objFileSystem = CreateObject(“Scripting.FileSystemObject”)
Set objFile = objFileSystem.OpenTextFile(“defaultdhcp.txt”, 1)

strLine= objFile.Readline

‘ Project 3.1 – The below gives more detailed information on the OS using WMI Script
strComputer = “.”
Set objWMIService = GetObject(“winmgmts:” _
& “{impersonationLevel=impersonate}!\\” _
& strComputer & “\root\cimv2”)
Set colOperatingSystems = objWMIService.ExecQuery _
(“Select * from Win32_OperatingSystem”)
For Each objOperatingSystem in colOperatingSystems
OperatingSystem = objOperatingSystem.Caption
OSv = objOperatingSystem.Version

Next
‘ The loop below references an outside text file “defaultdhcp.txt” and pings the
‘ IP addresses listed in it one line at a time looking for a response and then displaying
‘ that IP address at the end. These are simply the default IP addresses of the major manufacturers.

Do Until objFile.AtEndofStream
strLine = objFile.ReadLine
strCommand = “ping -n 1 -w 300 ” & strLine
ReturnCode = objShell.Run (strCommand, 0, True)

If ReturnCode = 0 Then
IP = strLine

End if
Loop

‘If this registry value doesn’t exist it throws an error but would stop the debugging process.
On Error resume next
ServicePackNumber = objShell.RegRead(regServicePackNumber)
on error goto 0

‘ Added Intelligence below, instead of the echo returning blank, it will say “none” instead

if ServicePackNumber = false Then
ServicePackNumber = “None”
End if

‘ Added Intelligence below, if DHCP Search comes up empty, it will return a value of “Unknown”

if IP = False Then
IP = “Unknown”
End if

ActiveComputerName = objShell.RegRead(regActiveComputerName)

‘ This section uses a searchstring to search a log file for a specific
‘ pattern and then resave those lines to a new text file.

Dim objRegEx, strSearchString, errorfiletxt, colMatches
Dim strMatch
Const ForReading = 1

Set objRegEx = CreateObject(“VBScript.RegExp”)
objRegEx.Pattern = “Error”

Set errorfiletxt = objFileSystem.CreateTextFile(“errorlog.txt”, True)
Set objFile = objFileSystem.OpenTextFile(“log.txt”, ForReading)

Do Until objFile.AtEndOfStream
strSearchString = objFile.ReadLine
Set colMatches = objRegEx.Execute(strSearchString)
If colMatches.Count > 0 Then
For Each strMatch in colMatches
errorfiletxt.WriteLine(strSearchString)
Next
End If
Loop

errorfiletxt.Close
objFile.Close

‘ This is the Array Portion of the Script, it creates an array using the same
‘ information displayed at the end of the script and saves it to a txt file.

Dim arComputer, computer, i, ComputerInfotxt

Set ComputerInfotxt = objFileSystem.CreateTextFile(“ComputerInfo.txt”, True)

i = 0

arComputer = Array(“Operating System: ” & OperatingSystem, “Version: ” & OSv, “Service Pack: ” & ServicePackNumber, “Computer Name: ” & ActiveComputerName, “DHCP Server: ” & IP)
For Each computer in arComputer
ComputerInfotxt.WriteLine(arComputer(i))
i = i+1

Next

ComputerInfotxt.Close

‘This part of the script creates a file, then writes to it (interaction with file system)
Dim strSubnet, intStartIP, intEndIP, strResult, x, strIP, strCPTR
Dim colItems, objItem
Dim Timeout, filetxt, path, getname
Dim strUserProfile, strWinDir

Set filetxt = objFileSystem.CreateTextFile(“networkscan.txt”, True)
path = objFileSystem.GetAbsolutePathName(“networkscan.txt”)
getname = objFileSystem.GetFileName(path)
strSubnet = “192.168.1.”
intStartIP = 2
intEndIP = 3
strResult = “No response”

x = intStartIP

Do Until x > intEndIP
strIP = CStr(x)
strCPTR = strSubnet & strIP

Set objWMIService = GetObject(“winmgmts:\\.\root\cimv2”)

Set colItems = objWMIService.ExecQuery(“Select * from Win32_PingStatus ” & “Where Address = ‘” & strCPTR & “‘ AND Timeout = 500 “)

For Each objItem in colItems
If objItem.StatusCode = 0 Then
filetxt.WriteLine(strCPTR & ” – Reply received.”)
End If
Next
x = x + 1
Loop

filetxt.Close

‘ Project 3.2 – I’m in the middle of a move and don’t have a good way of simulating an LDAP server, however
‘ the following portion of script would create an LDAP user using the computer name.

‘ Dim provider, OU, domain, oClass, oCN, oUname, objDomain, objUser, oUname,

‘ provider = “LDAP://”
‘ OU = “ou=cubefarm, ou=office,”
‘ domain = dc=a, dc=com”
‘ oCN = “CN=”
‘ oUname = ActiveComputerName

‘ Set objDomain = GetObject(provider & OU & domain)
‘ Set objUser = objDomain.create(oClass, oCN & oUname)

‘ objUser.Put “sAMAccountName”, oUname
‘ objUser.SetInfo

‘ (the confirmation of the user being created would appear with the rest
‘ of the information at the end)

‘ This portion of the script creates popup windows with the information pulled
‘ out of the registry
‘ Project 4.1 – Active Directory Search, the following script would connect to a specified LDAP server
‘ and search for the user that was just created to confirm its existence. the Wscript at the bottom has
‘ been changed to show that the user was found in ActiveDirectory.

‘ Dim qQuery, objConnection, objCommand, objRecordSet

‘ qQuery = “LDAP://; ;oUname, sAMAccountName;subtree”
‘ Set objConnection = CreateObject(“ADODB.Connection”)
‘ Set objCommand = CreateObject(ADODB.Command”)
‘ objConnection.Open “Provider=ADsDSOObject:”
‘ objCommand.ActiveConnection = objConnection
‘ objCommand.CommandText = oQuery
‘ Set objRecordSet = objCommand.Execute

‘ While Not objRecordSet.EOF
‘ If objRecordSet.Fields = False
‘ oUname = “Not Found”
‘ objRecordSet.MoveNext
‘ Wend
‘ objConnection.Close

‘ Project 4.2 – LogonScripts, subroutines, and Network Components. The following script uses all three to
‘ map a folder that I always have on servers as a network drive. It is setup as a subroutine so it could
‘ be called at a later time.

Sub MapNetworkDrive

Dim objNetwork
Dim strDriveLetter, strRemotePath, strUser, strPassword, strProfile
Dim Input, serverprompt, usernameprompt, passwordprompt, promptforinfo, driveletter, objPassword

driveletter = InputBox(“Enter Desired Drive Letter”, “Requested Drive Letter”)
serverprompt = InputBox(“Enter Server”, “Server Location”)
usernameprompt=InputBox(“Enter Username”, “Username”)
passwordprompt=InputBox(“Enter Password”, “Server Password”)

strDriveLetter = driveletter
strRemotePath = “\\” & serverprompt
strUser = usernameprompt
strPassword = passwordprompt
strProfile = “false”

Set objNetwork = WScript.CreateObject(“WScript.Network”)
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath, _
strProfile, strUser, strPassword

End Sub

MapNetworkDrive

‘ Project 5.1
‘ This short couple of lines of script makes a backup of the registry, however, this particular script
‘ in its entirety doesn’t have a need to write a change to the registry. This backup would be
‘ for “just in case”. This script does not work on Vista and above, however.

Set objShell = CreateObject(“WScript.shell”)
objShell.Exec “%comspec% /k reg.exe EXPORT HKLM hklm.reg”

‘ This next part of the script finds the default printer on the system and
‘ adds that to the final Wscript.echo:

Dim colInstalledPrinters, objPrinter, defaultprinter

strComputer = “.”
Set objWMIService = GetObject(“winmgmts:” & “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”)
Set colInstalledPrinters = objWMIService.ExecQuery(“Select * from Win32_Printer where Default = ‘True'”)
For Each objPrinter in colInstalledPrinters
defaultprinter = objPrinter.Name
Next

Wscript.echo “Operating System: ” & OperatingSystem & _
vbCrLf & _
vbCrLf & “Version: ” & OSv & _
vbCrLf & _
vbCrLf & “Service Pack: ” & ServicePackNumber & _
vbCrLf & _
vbCrLf & “Computer Name: ” & ActiveComputerName & _
vbCrLf & _
vbCrLf & “DHCP Server: ” & IP & _
vbCrLf & _
vbCrLf & “Default Printer: ” & defaultprinter
‘vbCrlf & _
‘vbCrlf & “Active Directory User = ” & oUname