| Author |
Topic  |
|
|
kgbrisc1
Here To Stay
 
United Kingdom
159 Posts
Status: offline |
Posted - 03/14/2006 : 05:03:15 AM
|
Here's a little script i wrote to retrieve basic computer info - i use it quite a lot when querying desktop status.
'********** Start of script ***********
'********************************************************* '** ** '** Script for retrieving basic computer / user info ** '** ** '********************************************************* '** ** '** By Kieren Briscoe - Version 1.0 ** '** ** '********************************************************* '** ** '** Revision History ** '** ** '** v1.0 (24/01/06) ** '** Base version ** '** ** '*********************************************************
'## Define Variables Dim StrDate, ScriptRestart Dim Str1,Str2,Str3,Str4,Str5,Str6
'## Loop until Cancelled by User do until ScriptRestart = 7
'## Get Computer Name / Ip Address strComputer = InputBox("Enter Computer Name or IP Address..") On Error Resume Next
'## Setup WMI access Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer _ & "\root\cimv2")
'## If Cannot Access Computer's wMI - Goto Error Messagebox If Err.Number = 0 Then On Error Goto 0
'------------------------------------------------------ ' ' Get OS Startup Time ' '------------------------------------------------------
Set colObjects = objWMIService.ExecQuery _ ("SELECT * FROM Win32_PerfRawData_PerfOS_System")
For Each objWmiObject In colObjects intPerfTimeStamp = objWmiObject.Timestamp_Object intPerfTimeFreq = objWmiObject.Frequency_Object intCounter = objWmiObject.SystemUpTime Next
iUptimeInSec = (intPerfTimeStamp - intCounter)/intPerfTimeFreq
'## convert the seconds sUptime = ConvertTime(iUptimeInSec)
'## Write string Str4 = "System Uptime:" & vbtab & vbtab & sUptime
'------------------------------------------------------ ' ' Get Computer / User Info ' '------------------------------------------------------
Set colAdapters = objWMIService.ExecQuery _ ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
For Each objAdapter in colAdapters
'## Get Computer Name from DNS info and Write string Str1 = "Computer name:" & vbtab & vbtab & objAdapter.DNSHostName
If Not IsNull(objAdapter.IPAddress) Then For i = 0 To UBound(objAdapter.IPAddress)
'## Get IP Address(es) and write string Str2 = "IP address:" & vbtab & vbtab & objAdapter.IPAddress(i) & Chr(13)
Next End If
'## Get Mac Address(es) and write string Str3 = "Physical (MAC) address:" & vbtab & objAdapter.MACAddress & Chr(13)
Next
'------------------------------------------------------ ' ' Get Computer / User Info ' '------------------------------------------------------
Set colComputer = objWMIService.ExecQuery _ ("Select * from Win32_ComputerSystem")
For Each objComputer in colComputer
'## Get Username of Current User and write string Str5 = "Logged-on user:" & vbtab & vbtab & objComputer.UserName
Next
'------------------------------------------------------ ' ' Get Computer / User Info ' '------------------------------------------------------
Set colSession = objWMIService.ExecQuery _ ("Select * from Win32_LogonSession where AuthenticationPackage = 'NTLM'")
For Each objSession in colSession
'## Get Last Logon Time and write String StrDate = WMIDateStringToDate(objSession.StartTime) Str6 = "User Logon Time:" & vbtab & vbtab & StrDate
Next
'------------------------------------------------------ ' ' Output Message Box if WMI Service Found ' Arument 4 Creates Yes and No Buttons ' No Returns an integer of 7 to Variable ScriptRestart ' '------------------------------------------------------
ScriptRestart=Msgbox(Str1 & Chr(13) & Str2 & Str3 & Str4 & Chr(13) _ & Str5 & Chr(13) & Str6 & Chr(13) & Chr(13) & "Try Another Computer?",4)
'------------------------------------------------------ ' ' Output Error Message Box if WMI Service Not Found ' Arument 4 Creates Yes and No Buttons ' No Returns an integer of 7 to Variable ScriptRestart ' '------------------------------------------------------
Else ScriptRestart=Msgbox("Could not connect to Computer " & strComputer _ & " with WMI.. " & Chr(13) & Chr(13) & "Try Another Computer?",4) End If
'## Restart Script loop
'-------------------------------------------------------- ' ' Function to convert seconds to days,hrs,mins,secs ' '--------------------------------------------------------
Function ConvertTime(seconds) ConvSec = seconds Mod 60 ConvMin = (seconds Mod 3600) \ 60 ConvHour = (seconds Mod (3600 * 24)) \ 3600 ConvDays = seconds \ (3600 * 24) ConvertTime = ConvDays & " days " & ConvHour & " hours " _ & ConvMin & " minutes " & ConvSec & " seconds " End Function
'-------------------------------------------------------- ' ' Function to convert UTC Date to Human Readable ' '--------------------------------------------------------
Function WMIDateStringToDate(utcDate) WMIDateStringToDate = CDate(Mid(utcDate, 5, 2) & "/" & _ Mid(utcDate, 7, 2) & "/" & _ Left(utcDate, 4) & " " & _ Mid(utcDate, 9, 2) & ":" & _ Mid(utcDate, 11, 2) & ":" & _ Mid(utcDate, 13, 2)) End Function
'********** end of script ***********
|
|
|
wim
Honorable But Hopeless Addict
    
Netherlands
1552 Posts
Status: offline |
Posted - 03/14/2006 : 05:35:19 AM
|
Not to criticise in any way but it might be interesting to know that a tool like this exists: http://sydi.sourceforge.net/
It might give you some pointers about what else you can add to your script |
I hear and I forget, I see and I know, I do and I understand. |
 |
|
|
Mark Minasi
Chief cook and bottle washer
    
USA
10658 Posts
Status: offline |
Posted - 03/14/2006 : 07:31:20 AM
|
| Or take a look at systeminfo.exe; it's on every XP desktop and runs remotely. |
Mark tweetin' at mminasi |
 |
|
|
wim
Honorable But Hopeless Addict
    
Netherlands
1552 Posts
Status: offline |
Posted - 03/14/2006 : 08:11:21 AM
|
| sure, but that's no fun! |
I hear and I forget, I see and I know, I do and I understand. |
 |
|
|
Mark Minasi
Chief cook and bottle washer
    
USA
10658 Posts
Status: offline |
Posted - 03/15/2006 : 06:11:07 AM
|
| Well, you have me there.<g> |
Mark tweetin' at mminasi |
 |
|
|
kgbrisc1
Here To Stay
 
United Kingdom
159 Posts
Status: offline |
Posted - 03/15/2006 : 07:27:05 AM
|
| Thanks guys - but neither will tell you quickly who is logged onto a machine and when they last rebooted which is often what i need to know when talking to a user on the phone. Well i may delve a bit deeper into detail in the near future to test my scripting skills.... |
 |
|
|
wim
Honorable But Hopeless Addict
    
Netherlands
1552 Posts
Status: offline |
Posted - 03/15/2006 : 08:12:46 AM
|
| I've written something similar in the past which is explorer based. It uses queries in AD to create lists of printers, users etc and you can than drill down to more detailed info. |
I hear and I forget, I see and I know, I do and I understand. |
 |
|
|
netmarcos
Honorable But Hopeless Addict
    
USA
2219 Posts
Status: offline |
Posted - 03/15/2006 : 09:54:18 AM
|
quote: Originally posted by kgbrisc1
Thanks guys - but neither will tell you quickly who is logged onto a machine and when they last rebooted which is often what i need to know when talking to a user on the phone. Well i may delve a bit deeper into detail in the near future to test my scripting skills....
For that, you can try something simple like thisOption Explicit
Dim strComputerName
Dim objWMIServices
Dim objUserSet
Dim oWshShell
Dim User
Dim colOperatingSystems
Dim objOperatingSystem
On Error Resume Next
Set oWshShell = CreateObject("Wscript.Shell")
strComputerName = InputBox ("Enter the name of the computer you wish to query","Target system", ".")
objWMIServices = "winmgmts:{impersonationLevel=impersonate}!//"& strComputerName &""
Set objUserSet = GetObject( objWMIServices ).InstancesOf ("Win32_ComputerSystem")
If Err.number <> 0 Then
If Err.Number = "-2147217405" Then
btnCode = oWshShell.Popup ("Access Denied" & Chr(13) & "Try again with alternate credentials?" , 30, strComputerName, 4+32)
Select Case BtnCode
case 6 altCreds()
case 7 oWshShell.Popup "Process has been cancelled by user",5,"Notice...",16
wScript.Quit
case -1 oWshShell.Popup "No user input. Process has been aborted.",10,"Notice...",64
wScript.Quit
End Select
ElseIf Err.Number = "462" Then
oWshShell.Popup "Host Unreachable", 10, strComputerName, 48
Else
oWshShell.Popup "Attempt to query current user on: " & strComputerName & " has failed." & Chr(13) & Err.Number & " : " & Err.Description, 10, strComputerName, 48
End If
Err.Clear
wScript.Quit
Else
Set colOperatingSystems = GetObject( objWMIServices ).InstancesOf ("Win32_OperatingSystem")
If Err.Number <> 0 Then WScript.Echo Err.Number & " : " & Err.Description
Err. Clear
End If
for each User in objUserSet
If User.UserName <> "" Then
oWshShell.Popup "The current user on " & strComputerName & " is: " & User.UserName & Chr(13) & upTime(strComputerName), 10, strComputerName, 64
Else
oWshShell.Popup "There are no users currently logged in at " & strComputerName & Chr(13) & upTime(strComputerName), 10, strComputerName, 64
End If
Next
Function upTime(strComputer)
Dim objOS
Dim dtmBootup
Dim dtmLastBootupTime
Dim dtmSystemUptime
On error Resume Next
upTime = 0
For Each objOS in colOperatingSystems
dtmBootup = objOS.LastBootUpTime
dtmLastBootupTime = WMIDateStringToDate(dtmBootup)
dtmSystemUptime = "Last system reboot occurred " & DateDiff("h", dtmLastBootUpTime, Now) & " hours, " & Int(DateDiff("n", dtmLastBootUpTime, Now)/60) & " minutes, " & DateDiff("n", dtmLastBootUpTime, Now) Mod 60 & " seconds ago."
If Err.Number =0 Then
upTime = dtmSystemUptime
Else
upTime = "Last reboot time cannot be retrieved from " & strComputer
End If
Err.Clear
Next
End Function
Function WMIDateStringToDate(dtmBootup)
WMIDateStringToDate = CDate(Mid(dtmBootup, 5, 2) & "/" & _
Mid(dtmBootup, 7, 2) & "/" & Left(dtmBootup, 4) _
& " " & Mid (dtmBootup, 9, 2) & ":" & _
Mid(dtmBootup, 11, 2) & ":" & Mid(dtmBootup, _
13, 2))
End Function
Function altCreds(sHost)
Dim sUser
Dim sPass
Dim oSWbemLocator
Dim oSWbemServices
sUser = InputBox("Please enter the Administrator Name: ")
sPass = InputBox("Please enter the administrator password: ")
On Error Resume Next
Set oSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
Set oSWbemServices = oSWbemLocator.ConnectServer _
(sHost, "root\cimv2", sUser, sPass)
If Err.Number <> 0 Then
oWshShell.Popup "WMI Connection was not successful. " & Chr(13) & Err.Description, 10, Err.Source & " on " & sHost, 48
Else
Set objUserSet = oSWbemServices.ExecQuery ("Select * from Win32_ComputerSystem")
Set colOperatingSystems = oSWbemServices.ExecQuery ("Select * from Win32_OperatingSystem")
End If
End Function
|
Mark M. Webster
Genius may have its limitations, but stupidity is not thus handicapped. - Elbert Hubbard
 |
Edited by - netmarcos on 03/15/2006 09:55:44 AM |
 |
|
|
soreback
Welcome Newcomer
9 Posts
Status: offline |
Posted - 04/09/2008 : 12:45:24 PM
|
| great script, but what i cant get to work, and i pretty scripting illiterate as well, is how to properly define an array of computers to have this script go out over the network and get the results for all of the computers defined. |
 |
|
|
JSCLMEDAVE
Administrator
    
USA
6113 Posts
Status: online |
Posted - 04/09/2008 : 1:34:48 PM
|
Is the , ".") necesary at the end of the text box entry?
I deleted that and it worked okay for me... |
Tim-
“This too shall pass" |
 |
|
|
itr
Welcome Newcomer
USA
5 Posts
Status: offline |
Posted - 04/20/2008 : 5:49:12 PM
|
The following script scans a class c subnet and dumps the hardware specs to .csv files in the root of c:\ of the pc it is run on.
On Error Resume Next strSubnet = InputBox _ ("Please enter first three octets of subnet {eg. 192.168.1.}") intStartingAddress = 1
intEndingAddress = 255
Const WbemAuthenticationLevelPktPrivacy = 6
strCredentials = InputBox _ ("Please enter an administrative user name, a blank space, and then the password:", _ "Enter User Credentials")
If strCredentials = "" Then Wscript.Quit End If
arrCredentials = Split(strCredentials," ") strUser = arrCredentials(0) strPassword = arrCredentials(1) strNamespace = "root\cimv2"
For i = intStartingAddress to intEndingAddress strComputer = strSubnet & i
Set objShell = CreateObject("WScript.Shell") strCommand = "%comspec% /c ping -n 3 -w 1000 " & strComputer & "" Set objExecObject = objShell.Exec(strCommand)
Do While Not objExecObject.StdOut.AtEndOfStream strText = objExecObject.StdOut.ReadAll() If Instr(strText, "Reply") > 0 Then
Set objWbemLocator = CreateObject("WbemScripting.SWbemLocator") Set objWMIService = objwbemLocator.ConnectServer _ (strComputer, strNamespace, strUser, strPassword) objWMIService.Security_.authenticationLevel = WbemAuthenticationLevelPktPrivacy
' ===================================================================== ' Insert your code here ' =====================================================================
'***Because errors happen on error resume next '***Declarations Set objFSO = CreateObject("Scripting.FileSystemObject") '**script saves to root of c:\ change to whatever inventory path desired Set objTextFile = objFSO.CreateTextFile("C:\" & strComputer & "_Hardware.csv", True) Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") '****Manufacturer Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem") objTextFile.WriteLine objTextFile.WriteLine "MFG ID:" For Each objItem In colItems objTextFile.WriteLine "Name: " & objItem.Name objTextFile.WriteLine "Manufacturer: " & objItem.Manufacturer objTextFile.WriteLine "Model: " & objItem.Model Next '****CPU Set colCSes = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem") objTextFile.WriteLine objTextFile.WriteLine "CPU Stats:" For Each objCS In colCSes objTextFile.WriteLine "Computer Name: " & objCS.Name objTextFile.WriteLine "System Type: " & objCS.SystemType objTextFile.WriteLine "Number Of Processors: " & objCS.NumberOfProcessors Next Set colProcessors = objWMIService.ExecQuery("Select * from Win32_Processor") For Each objProcessor in colProcessors objTextFile.WriteLine "Manufacturer: " & objProcessor.Manufacturer objTextFile.WriteLine "Name: " & objProcessor.Name objTextFile.WriteLine "Description: " & objProcessor.Description objTextFile.WriteLine "Processor ID: " & objProcessor.ProcessorID objTextFile.WriteLine "Address Width: " & objProcessor.AddressWidth objTextFile.WriteLine "Data Width: " & objProcessor.DataWidth objTextFile.WriteLine "Family: " & objProcessor.Family objTextFile.WriteLine "Maximum Clock Speed: " & objProcessor.MaxClockSpeed Next '*****Memory Set colCSItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem") objTextFile.WriteLine objTextFile.WriteLine "Memory:" For Each objCSItem In colCSItems objTextFile.WriteLine "Total Physical Memory: " & Round(objCSItem.TotalPhysicalMemory/1024/1024) & " Megabytes" Next Set colOSItems = objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem") For Each objOSItem In colOSItems objTextFile.WriteLine "Free Physical Memory: " & Round(objOSItem.FreePhysicalMemory/1024/1024) & " Megabytes" objTextFile.WriteLine "Total Virtual Memory: " & Round(objOSItem.TotalVirtualMemorySize/1024) & " Megabytes" objTextFile.WriteLine "Free Virtual Memory: " & Round (objOSItem.FreeVirtualMemory/1024) & " Megabytes" objTextFile.WriteLine "Total Visible Memory Size: " & objOSItem.TotalVisibleMemorySize Next '**** Operating System Set colOSes = objWMIService.ExecQuery("Select * from Win32_OperatingSystem") objTextFile.WriteLine objTextFile.WriteLine "OS Stats:" For Each objOS in colOSes objTextFile.WriteLine "Computer Name: " & objOS.CSName objTextFile.WriteLine "Caption: " & objOS.Caption 'Name objTextFile.WriteLine "Version: " & objOS.Version 'Version & build objTextFile.WriteLine "Build Number: " & objOS.BuildNumber 'Build objTextFile.WriteLine "Build Type: " & objOS.BuildType objTextFile.WriteLine "OS Type: " & objOS.OSType objTextFile.WriteLine "Other Type Description: " & objOS.OtherTypeDescription objTextFile.WriteLine "Service Pack: " & objOS.ServicePackMajorVersion & "." & _ objOS.ServicePackMinorVersion Next '*****Domain Membership Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem") objTextFile.WriteLine objTextFile.WriteLine "Domain Membership:" For Each objItem In colItems objTextFile.WriteLine "Computer Name: " & objItem.Name objTextFile.WriteLine "Name Format: " & objItem.NameFormat objTextFile.WriteLine "Domain: " & objItem.Domain objTextFile.WriteLine "Part Of Domain: " & objItem.PartOfDomain 'post-Windows 2000 only objTextFile.WriteLine "Workgroup: " & objItem.Workgroup 'post-Windows 2000 only Select Case objItem.DomainRole Case 0 strDomainRole = "Standalone Workstation" Case 1 strDomainRole = "Member Workstation" Case 2 strDomainRole = "Standalone Server" Case 3 strDomainRole = "Member Server" Case 4 strDomainRole = "Backup Domain Controller" Case 5 strDomainRole = "Primary Domain Controller" End Select objTextFile.WriteLine "Domain Role: " & strDomainRole strRoles = Join(objItem.Roles, ",") objTextFile.WriteLine "Roles: " & strRoles objTextFile.WriteLine "Network Server Mode Enabled: " & _ objItem.NetworkServerModeEnabled Next '****Boot Directories Set colOSItems = objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem") objTextFile.WriteLine objTextFile.WriteLine "OS Directories:" For Each objOSItem In colOSItems objTextFile.WriteLine "Boot Device: " & objOSItem.BootDevice objTextFile.WriteLine "System Device: " & objOSItem.SystemDevice objTextFile.WriteLine "System Drive: " & objOSItem.SystemDrive objTextFile.WriteLine "Windows Directory: " & objOSItem.WindowsDirectory objTextFile.WriteLine "System Directory: " & objOSItem.SystemDirectory Next '*****OS Properties Set colOperatingSystems = objWMIService.ExecQuery _ ("Select * from Win32_OperatingSystem") objTextFile.WriteLine objTextFile.WriteLine "OS Properties:" For Each objOperatingSystem in colOperatingSystems objTextFile.WriteLine "Boot Device: " & objOperatingSystem.BootDevice objTextFile.WriteLine "Build Number: " & objOperatingSystem.BuildNumber objTextFile.WriteLine "Build Type: " & objOperatingSystem.BuildType objTextFile.WriteLine "Caption: " & objOperatingSystem.Caption objTextFile.WriteLine "Code Set: " & objOperatingSystem.CodeSet objTextFile.WriteLine "Country Code: " & objOperatingSystem.CountryCode objTextFile.WriteLine "Debug: " & objOperatingSystem.Debug objTextFile.WriteLine "Encryption Level: " & objOperatingSystem.EncryptionLevel dtmConvertedDate.Value = objOperatingSystem.InstallDate dtmInstallDate = dtmConvertedDate.GetVarDate objTextFile.WriteLine "Install Date: " & dtmInstallDate objTextFile.WriteLine "Licensed Users: " & _ objOperatingSystem.NumberOfLicensedUsers objTextFile.WriteLine "Organization: " & objOperatingSystem.Organization objTextFile.WriteLine "OS Language: " & objOperatingSystem.OSLanguage objTextFile.WriteLine "OS Product Suite: " & objOperatingSystem.OSProductSuite objTextFile.WriteLine "OS Type: " & objOperatingSystem.OSType objTextFile.WriteLine "Primary: " & objOperatingSystem.Primary objTextFile.WriteLine "Registered User: " & objOperatingSystem.RegisteredUser objTextFile.WriteLine "Serial Number: " & objOperatingSystem.SerialNumber objTextFile.WriteLine "Version: " & objOperatingSystem.Version Next
' ===================================================================== ' End ' =====================================================================
Else ' Wscript.Echo strComputer & " could be not reached." End If Loop Next
|
 |
|
| |
Topic  |
|
|
|