' ### Script: srvinfo.vbs ' ### Created: 04/04/04 ' ### Modified: ' ### Contact: tim@blackpondfarm.com ' ### Description: Replicates srvinfo.exe functionality but minor tweaks '======================================================================================================== '======================================================================================================== Option Explicit 'Declare variables Dim WshShell,Wshnetwork,WshFile Dim Local,Inst,dll,Version Dim strServer,strArgs Dim GetServices,GetDrivers,GetShares,GetVersions Dim ObjNetwork,ObjOS,ObjComputer,ObjProc,ObjMemory,ObjDrives,ObjServices,ObjZone,ObjDrivers,ObjShares,ObjBIOS,objHOTFIX Dim dateTime,strTime Set WshShell = Wscript.CreateObject("Wscript.shell") Set Wshnetwork = wscript.CreateObject("Wscript.Network") Set Wshfile = wscript.CreateObject("Scripting.FileSystemObject") 'Make sure the host is cscript, if not then set default to cscript if instr(1, wscript.fullname, "cscript.exe", 1) = 0 then VerifyCscript() 'Parse the command line GetArgs() 'Go for it FullMealDeal '======================================================================================================== '======================================================================================================== private sub fullmealdeal() 'Set WMI objects that will be used in all cases (no switches) Set ObjOS = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strServer).InstancesOf("Win32_OperatingSystem") 'Make sure we can connect IF err.number <> 0 THEN wscript.echo err.number wscript.echo "Unable to access server " & strServer & ". Check server name" wscript.echo "and ensure you are an Administrator on the target server." wscript.quit END IF Set ObjComputer = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strServer).InstancesOf("Win32_ComputerSystem") Set ObjProc = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strServer).InstancesOf("Win32_Processor") Set ObjMemory = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strServer).InstancesOf("Win32_LogicalMemoryConfiguration") Set ObjDrives = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strServer).InstancesOf("Win32_LogicalDisk") Set ObjZone = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strServer).InstancesOf("Win32_TimeZone") Set ObjBIOS = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strServer).InstancesOf("Win32_BIOS") Set ObjNetwork = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strServer).INSTancesOf("Win32_NetworkAdapterConfiguration") On error resume next 'Call subs for each query sServername sComputerStuff sOSStuff sDomain sDC sProcessors sBIOS sMemory sHotfixes sDrives sIPConfig sShares sServices sDrivers sTimeZone sUptime End sub '======================================================================================================== '======================================================================================================== Private Sub sServerName() wscript.echo "" For Each INST in ObjOS wscript.echo "Server Name: " & INST.CSName NEXT End Sub '======================================================================================================== '======================================================================================================== Private Sub sComputerstuff() On error resume next Dim ROLE For Each INST in ObjComputer IF inst.Domain <> "" THEN wscript.echo "Security: Users" ELSE wscript.echo "Security: Share" END IF ROLE=INST.DomainRole Select Case Role Case "1" wscript.echo "Role: Member Workstation" Case "2" wscript.echo "Role: LanMan Server" Case "3" wscript.echo "Role: Member Server" Case "4" wscript.echo "Role: Domain Controller" Case ELSE wscript.echo "Role: Backup Browser" End Select next End Sub '======================================================================================================== '======================================================================================================== Private Sub sOSStuff() For Each INST in ObjOS wscript.echo "Registered Owner: " & INST.RegisteredUser wscript.echo "Registered Organization: " & INST.Organization wscript.echo "ProductID: " & INST.SerialNumber wscript.echo "Original Install Date: " & FormatTime(inst.InstallDate) Version = INST.Version WScript.Echo "Version: " & Version WScript.Echo "Build: " & INST.BuildNumber & " " & INST.CSDVersion wscript.echo "Current Type: " & INST.BuildType wscript.echo "Product Name: " & INST.Caption next End Sub '======================================================================================================== '======================================================================================================== Private Sub sDomain() Dim DNSDomain For Each INST in ObjComputer wscript.echo "Domain: " & inst.Domain next End Sub '======================================================================================================== '======================================================================================================== Private Sub sProcessors() For Each INST in ObjProc wscript.echo INST.DeviceID & " " & INST.Caption & " " & INST.MaxClockSpeed & " Mhz Load: " & INST.LoadPercentage & "%" next End Sub '======================================================================================================== '======================================================================================================== Private Sub sBIOS() DIM BIOS,BIOSDATE for each INST in ObjBIOS BIOS = left(INST.ReleaseDate,8) next dim ybios,dbios,mbios yBIOS = left(BIOS,4) mBIOS = mid(BIOS,5,2) dBIOS = right(BIOS,2) WScript.Echo "BIOS: " & mBIOS & "/" & dBIOS & "/" & yBIOS End Sub '======================================================================================================== '======================================================================================================== Private Sub sMemory() for each INST in ObjMemory WScript.Echo "Memory: " & clng(INST.TotalPhysicalMemory / 1024) & " Mb" next End Sub '======================================================================================================== '======================================================================================================== Private Sub sHotfixes() wscript.echo "Hotfixes:" IF Left(Version,1) = "4" THEN Wscript.echo " Unable to enumerate hotfixes on NT 4.0 box" ELSE Set ObjHOTFIX = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strServer).InstancesOf("Win32_QuickFixEngineering") For Each INST in ObjHOTFIX IF len(INST.InstalledBy) > 0 THEN wscript.echo " [" & INST.HotfixID & "]: Installed by " & INST.InstalledBy & " Description: " & INST.Description next END IF END SUB '======================================================================================================== '======================================================================================================== Private Sub sDrives() For Each INST in ObjDrives IF INST.Description="Local Fixed Disk" THEN WScript.Echo "Disk " & INST.deviceid & " " & INST.FileSystem & " Size=" & clng(INST.size / 1048576) & " Mb " & "Freespace=" & formatPercent((INST.freespace / INST.size),0) next End Sub '======================================================================================================== '======================================================================================================== Private Sub sIPConfig() On error resume next Dim names,i,a,m,q For each INST in ObjNetwork IF INST.IPAddress <> "" THEN wscript.echo "Network Card " & INST.Caption(a) For a = LBound(INST.IPAddress) to UBound(INST.IPAddress) WScript.Echo " IP Address: " & INST.IPAddress(a) Next For i = LBound(INST.IPsubnet) to UBound(INST.IPSubnet) WScript.Echo " Subnet Mask: " & INST.IPSubnet(i) Next For m = LBound(INST.DefaultIPGateway) to UBound(INST.DefaultIPGateway) WScript.Echo " Default Gateway: " & INST.DefaultIPGateway(m) Next names = "" For q = LBound(INST.DNSServerSearchOrder) to UBound(INST.DNSServerSearchOrder) names = names & " " & INST.DNSServerSearchOrder(q) next WScript.echo " DNS Nameservers:" & names WScript.Echo " Primary WINS: " & INST.WINSPrimaryServer WScript.Echo " Secondary WINS: " & INST.WINSSecondaryServer WScript.Echo " Physical Address: " & INST.MACAddress IF INST.DHCPEnabled = CINT(-1) THEN wscript.echo " DHCP Enabled: Yes" ELSE wscript.echo " DHCP Enabled: No" END IF IF INST.IPXEnabled = "True" Then wscript.echo " IPX Enabled" wscript.echo END IF Next End Sub '======================================================================================================== '======================================================================================================== Private Sub sShares() If GetShares = "Yes" Then Set ObjShares = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strServer).InstancesOf("Win32_Share") WScript.Echo "Shares:" For each INST in ObjShares WScript.Echo " Share " & INST.Name & " " & INST.Path Next End if End Sub '======================================================================================================== '======================================================================================================== Private Sub sServices() If GetServices <> "No" Then Set ObjServices = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strServer).InstancesOf("Win32_Service") wscript.echo "Services:" For each INST in ObjServices WScript.Echo " [" & INST.State & "] " & INST.DisplayName next Else End If End Sub '======================================================================================================== '======================================================================================================== Private Sub sDrivers() If GetDrivers = "Yes" Then Set ObjDrivers = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strServer).InstancesOf("Win32_SystemDriver") wscript.echo "Service Drivers:" For each INST in ObjDrivers WScript.Echo " [" & INST.State & "] " & INST.Description next Else End If End Sub '======================================================================================================== '======================================================================================================== Private Sub sTimezone() On error resume next for each inst in ObjZone WScript.Echo "Time Zone: " & inst.StandardName next End Sub '======================================================================================================== '======================================================================================================== Private Sub sUptime() For each INST in ObjOS wscript.echo "Last reboot: " & FormatTime(inst.LastBootUpTime) next End Sub '======================================================================================================== '======================================================================================================== 'General Functions and Subs '======================================================================================================== '======================================================================================================== Private Function FormatTime(strDate) Dim str str = Mid(strDate,9,2) & ":" _ & Mid(strDate,11,2) & ":" _ & Mid(strDate,13,2) & " " _ & Mid(strDate,5,2) & "-" _ & Mid(strDate,7,2) & "-" _ & Mid(strDate,1,4) FormatTime = str End Function '======================================================================================================== '======================================================================================================== Private Sub Scripthelp() WScript.Echo WScript.Echo WScript.Echo WScript.Echo "SrvInfo.vbs for NT" WScript.Echo "Version " & Version WScript.Echo "=====================================================" WScript.Echo "Remotely gather information about a target server." WScript.Echo "Assume local machine if no computer name is provided." WScript.Echo "=====================================================" WScript.Echo "" WScript.Echo "Usage: SRVINFO [[-?|-ns|-d|-v|-s] computer_name]" WScript.Echo " -?: Show usage" WScript.Echo " -ns: Do NOT show any service information" WScript.Echo " -d: Show service drivers and service" WScript.Echo " -v: Get version info for Exchange, IIS, SQL" WScript.Echo " -s: Show shares" Wscript.Echo Wscript.Echo Wscript.Echo "Basically replicates srvinfo.exe functions in VB, except as noted:" Wscript.Echo Wscript.Echo "Additions:" Wscript.Echo "DNS Domain" Wscript.Echo "IP Config" Wscript.Echo "Memory" Wscript.Echo "Time Zone" Wscript.Echo "CPU Load" WScript.Echo Wscript.Echo "Items in srvinfo.exe but not in this script:" Wscript.Echo "Hotfixes (Win32_QuickFixEngineering)" Wscript.Echo "PDC" Wscript.Echo "-v does not report SQL & IIS Versions" Wscript.Echo Wscript.Echo "tim@blackpondfarm.com" Wscript.Echo Wscript.Quit End Sub '======================================================================================================== '======================================================================================================== Function GetArgs() On Error Resume Next Dim a For a = 0 To wscript.arguments.count - 1 argtext = argtext & wscript.arguments(a) & " " strArgs = wscript.arguments(a) next If strArgs <> "" AND strArgs <> "-ns" AND strArgs <> "-s" AND strArgs <> "-d" AND strArgs <> "-v" Then strserver=strArgs if left(strserver,2)= "\\" THEN strserver=mid(strserver,3) End if Else strServer=Wshshell.ExpandEnvironmentStrings("%COMPUTERNAME%") Local = "Yes" End If If ParseArgument("-?") Then Call SCRIPTHELP() Exit Function End If If ParseArgument("/?") Then Call SCRIPTHELP() Exit Function End If If ParseArgument("-ns") Then GetServices = "No" End If If ParseArgument("-d") Then GetDrivers = "Yes" End If If ParseArgument("-v") Then GetVersions = "Yes" End If If ParseArgument("-s") Then GetShares = "Yes" End If If Err.number <> 0 Then LogError "End of Arguments Function", Err.number Err.Clear End If End Function '======================================================================================================== '======================================================================================================== Function ParseArgument(strtext) Dim a On Error Resume Next For a = 0 To wscript.arguments.count - 1 If wscript.arguments(a) = strtext Then ParseArgument = a + 1 Next End Function '======================================================================================================== '======================================================================================================== Function GMT() On error resume next 'Get time in GMT Set dateTime = CreateObject("WbemScripting.SWbemDateTime") If Err.Number <> 0 Then 'Pre-.NET server, use a cludgy conversion and assume Pacific Daylight Time strTime = datevalue(now+#07:00:00#) & " " & timevalue(now+#07:00:00#) ELSE dateTime.SetVarDate (CDate (now)) strTime = dateTime.GetVarDate (false) END IF END FUNCTION '======================================================================================================== '======================================================================================================== Function VerifyCscript() Dim temp 'Sets up Vbscript to always run in command window temp = wshshell.Run("cmd /c ""wscript //h:cscript //nologo //s 1>nul 2>nul""", 0, true) temp = MsgBox ("The script has changed the default output of Windows Scripting Host to the command prompt." &_ vbCrLf & "This is pop up is normal, just re-run the script!", 0, "WSH default changed to cscript.") wscript.quit End Function '======================================================================================================== 'END OF SCRIPT '========================================================================================================