If a user enters the an incorrect hostname. i want the script to display a msgbox "PC wmi broken or is offline" it diplays an error but as this is being sent out to others, i'd just like a basic error msg box
Sub WindowsLoad1
strHTML = strHTML & "<br>" & "<font face='Arial' size='2' color='white''>"
strComputer = MachineName.Value
strHTML = strHTML & "<b>Sys Info</b><br> <br>"
strHTML = strHTML & "</font><font face='Arial' size='2'>"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colComputer = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
For Each objComputer in colComputer
strHTML = strHTML & "<b>Hostname</b>: " & objComputer.Name & "<br>"
strHTML = strHTML & "<b>Current User</b>: " & objComputer.UserName & "<br>"
strHTML = strHTML & "<b>Manufacturer</b>: " & objComputer.Manufacturer & "<br>"
strHTML = strHTML & "<b>Model</b>: " & objComputer.Model & "<br>"
Next
strHTML = strHTML& "<br>" & "<font face='Arial' size='2' color='white''>"
strComputer = MachineName.Value
strHTML = strHTML& "<b>Serial Number</b><br> <br>"
strHTML = strHTML&"</font><font face='Arial' size='2'>"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_BIOS")
For Each objItem in colItems
strHTML4 = strHTML4 & " " & objItem.SerialNumber & "<br><br>"
next
strHTML3 = "<font face='Arial' size='2' color='white''>"
strComputer = MachineName.Value
strHTML3 = strHTML3 & "<b>Windows Operating System Summary</b><br> <br>"
strHTML3 = strHTML3 &"</font><font face='Arial' size='2'>"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
For Each objItem in colItems
strHTML3 = strHTML3 & "<b>Caption</b>: " & objItem.Caption & "<br>"
strHTML3 = strHTML3 & "<b>Version</b>: " & objItem.Version & "<br>"
strHTML3 = strHTML3 & "<b>Service Pack</b>: " & objItem.ServicePackMajorVersion & "<br>"
Next
strHTML3 = strHTML3 & "<br>" & "<br>" & "<font face='Arial' size='2' color='white''>"
'strHTML3 = strHTML3 & "<b>Last Reboot</b><br> <br>"
strHTML3 = strHTML3 & "</font><font face='Arial' size='2'>"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
For Each objItem in colItems
dtmInstallDate = objItem.LastBootUpTime
'WMIDateStringToDate = FormatDateTime(dtmInstallDate,1) & " " & FormatDateTime(dtmInstallDate,4)
'WMIDateStringToDate = CDate(Left(dtmInstallDate, 4) & "/" & Mid(dtmInstallDate, 5, 2) & "/" & Mid(dtmInstallDate, 7, 2) _
'& " " & Mid (dtmInstallDate, 9, 2) & ":" & Mid(dtmInstallDate, 11, 2) & ":" & Mid(dtmInstallDate, 13, 2))
WMIDateStringToDate= dtmInstallDate
'strHTML3 = strHTML3 & "<b>Computer Last Booted </b>: " & WMIDateStringToDate & "<br>"
Next
strHTML2 = "<font face='Arial' size='2' color='white''>"
strComputer = MachineName.Value
strHTML2 = strHTML2 & "<b>Network Adapter Properties</b><br> <br>"
strHTML2 = strHTML2 &"</font><font face='Arial' size='2'>"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
For Each objItem in colItems
strHTML2 = strHTML2 & "<b>Description</b>: " & objItem.Description & "<br>"
strHTML2 = strHTML2 & "<b>Physical (MAC) address</b>: " & objItem.MACAddress & "<br>"
strHTML2 = strHTML2 & "<b>Host name</b>: " & objItem.DNSHostName & "<br>"
If Not IsNull(objItem.IPAddress) Then
For i = 0 To UBound(objItem.IPAddress)
strHTML2 = strHTML2 & "<b>IP address</b>: " & objItem.IPAddress(i) & "<br>"
Next
End If
If Not IsNull(objItem.IPSubnet) Then
For i = 0 To UBound(objItem.IPSubnet)
strHTML2 = strHTML2 & "<b>Subnet</b>: " & objItem.IPSubnet(i) & "<br>"
Next
End If
strHTML2 = strHTML2 & "<b>DHCP server</b>: " & objItem.DHCPServer & "<br>"
strHTML2 = strHTML2 & "<b>Primary WINS server</b>: " & objItem.WINSPrimaryServer & "<br>"
strHTML2 = strHTML2 & "<b>Secondary WINS server</b>: " & objItem.WINSSecondaryServer & "<br> <br>"
Next
DataArea.InnerHTML = strHTML & strHTML4 & strhtml3 & strhtml2
End Sub
Next code snippet with proper error handling could help:
Sub WindowsLoad1
strHTML = strHTML & "<br>" & "<font face='Arial' size='2' color='white''>"
strComputer = MachineName.Value
strHTML = strHTML & "<b>Sys Info</b><br> <br>"
strHTML = strHTML & "</font><font face='Arial' size='2'>"
On Error Resume Next
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
If Err.Number = 0 Then
On Error Goto 0
Else
msgbox "Data for current computer displayed due to the error: " _
& Err.Description _
, vbOKOnly + vbExclamation _
, "An error occured"
On Error Goto 0
strComputer = "."
MachineName.Value = strComputer
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
End If
Set colComputer = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
'''''''''''''''''''
' etc.
'''''''''''''''''''
here is a vbscript which shows all drives' volume serial numbers. But I need to customize to return only the volume serial number of the drive which the script is running from.
How to do it?
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
str = ""
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk")
For Each objItem In colItems
str = str & objItem.Name & " SerialNumber: " & objItem.VolumeSerialNumber & vbCrlf & vbCrlf
Next
MsgBox str
This should do what you need:
' Get the drive designator...
With CreateObject("Scripting.FileSystemObject")
strDrive = .GetDriveName(WScript.ScriptFullName)
End With
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk WHERE DeviceId='" & strDrive & "'")
' There should only be one item in the collection...
For Each objItem In colItems
MsgBox objItem.Name & " SerialNumber: " & objItem.VolumeSerialNumber
Next
I am trying to use some nVidia functions in there WMI API (attached, it is a txt file but should be renamed to chm for help file)
I am new to vbscript so can be doing something wrong.
My code is as follows:
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
arrComputers = Array(".")
For Each strComputer In arrComputers
WScript.Echo
WScript.Echo "=========================================="
WScript.Echo "Computer: " & strComputer
WScript.Echo "=========================================="
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2\NV")
Set colItems = objWMIService.ExecQuery("SELECT * FROM SyncTopology", "WQL", _
wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each objItem In colItems
NodeID = objItem.id
WScript.Echo "id: " & NodeID
WScript.Echo
Next
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2\NV")
Set SyncClass = objWMIService.Get("Sync")
if (SyncClass.toggleSource()) Then
wscript.echo "done!"
End if
Next
I am actually trying to use a different function but this one is the easiest and takes no arguments.
The class is 'Sync' the function is toggleSource, should be too easy!
I get an error on line:
if (SyncClass.toggleSource()) Then
stating:
C:\Users\User\Desktop\test3.vbs(28, 2) SWbemObjectEx: Invalid method Parameter(s)
I can query attributes in the class just can run methods :(
I can use these methods in Powershell so they should work, just can't get it working in vbscript!! AHHH...
I'm trying to write a script in vbscript but being a near noob and online tutorials didn't work, I had to resort to posting here asking for help.
The script that I've mixed and match from different sources displays domain, user, computer name, ip address. The script is working. However in certain environment, a user could potentially have multiple IP addresses and when displaying in MsgBox, only the last IP address result is returned and in many cases, that's wrong.
I would like to know how I add/can store the address in an array and have MsgBox display the other IP addresses if there was more than one result.
Thank you.
Script attached below:
Option Explicit
DIM WshNetwork, strComputer, IPConfigSet, objWMIService, IPConfig, i, j, strIP, title, message, colItems, objItem
DIM arrIPAddress, columnC, strIPAddress, testIP(3)
Set WshNetwork = WScript.CreateObject("WScript.Network")
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set IPConfigSet = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_NetworkAdapterConfiguration",,48)
For Each objItem in colItems
If isNull(objItem.IPAddress) Then
Else
Wscript.Echo "IPAddress: " & Join(objItem.IPAddress, ",")
strIP = objItem.IPAddress(0)
End If
Next
title = "Who Am I?"
message = "Domain: " & vbTab & vbTab & WshNetwork.UserDomain & VbCrlf & _
"User Name: " & vbTab & UCase(WshNetwork.UserName) & VbCrlf & _
"Computer Name: " & vbTab & WshNetwork.ComputerName & VbCrlf & _
"IP Address1: " & vbTab & strIP
Msgbox message, , title
In your code MsgBox will show the first address of the network adapter last enumerated. If you want to show all IP addresses, change this:
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_NetworkAdapterConfiguration",,48)
For Each objItem in colItems
If isNull(objItem.IPAddress) Then
Else
Wscript.Echo "IPAddress: " & Join(objItem.IPAddress, ",")
strIP = objItem.IPAddress(0)
End If
Next
title = "Who Am I?"
message = "Domain: " & vbTab & vbTab & WshNetwork.UserDomain & VbCrlf & _
"User Name: " & vbTab & UCase(WshNetwork.UserName) & VbCrlf & _
"Computer Name: " & vbTab & WshNetwork.ComputerName & VbCrlf & _
"IP Address1: " & vbTab & strIP
into this:
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
ReDim arrIP(-1)
For Each objItem In colItems
For Each addr In objItem.IPAddress
ReDim Preserve arrIP(UBound(arrIP)+1)
arrIP(UBound(arrIP)) = addr
Next
Next
title = "Who Am I?"
message = "Domain:" & vbTab & vbTab & WshNetwork.UserDomain & vbNewLine & _
"User Name:" & vbTab & UCase(WshNetwork.UserName) & vbNewLine & _
"Computer Name:" & vbTab & WshNetwork.ComputerName & vbNewLine & _
"IP Address1:" & vbTab & Join(arrIP, ", ")
The following was tested in Windows 8; works flawlessly!
Option Explicit
DIM objHTTP, WshNetwork, strComputer, IPConfigSet, objWMIService, IPConfig, i, j, strIP, title, message, colItems, objItem
DIM arrIPAddress, columnC, strIPAddress, testIP(3), addr
Set objHTTP = WScript.CreateObject("MSXML2.ServerXmlHttp")
objHTTP.Open "GET", "http://icanhazip.com", False
objHTTP.Send
Set WshNetwork = WScript.CreateObject("WScript.Network")
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set IPConfigSet = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
ReDim arrIP(-1)
For Each objItem In colItems
For Each addr In objItem.IPAddress
ReDim Preserve arrIP(UBound(arrIP)+1)
arrIP(UBound(arrIP)) = addr
Next
Next
title = "Who Am I?"
message = "Domain:" & vbTab & vbTab & WshNetwork.UserDomain & vbNewLine & _
"User Name:" & vbTab & UCase(WshNetwork.UserName) & vbNewLine & _
"Computer Name:" & vbTab & WshNetwork.ComputerName & vbNewLine & _
"Public IP Address: " & vbTab & objHTTP.ResponseText & vbNewLine & _
"Network IPs v4 & v6: " & vbNewLine & vbTab & vbTab & Join(arrIP, ", " & vbNewLine & vbTab & vbTab) & "."
Msgbox message, , title
Set objHTTP = Nothing</code>
Option Explicit
DIM WshNetwork, strComputer, IPConfigSet, objWMIService, IPConfig, i, j, strIP, title, message, colItems, objItem
DIM arrIPAddress, columnC, strIPAddress, testIP(3)
Set WshNetwork = WScript.CreateObject("WScript.Network")
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set IPConfigSet = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_NetworkAdapterConfiguration",,48)
For Each objItem in colItems
If isNull(objItem.IPAddress) Then
Else
' Wscript.Echo "IPAddress: " & Join(objItem.IPAddress, ",")
strIP = objItem.IPAddress(0)
End If
Next
title = "Who Am I?"
message = "Domain: " & vbTab & vbTab & WshNetwork.UserDomain & VbCrlf & _
"User Name: " & vbTab & UCase(WshNetwork.UserName) & VbCrlf & _
"Computer Name: " & vbTab & WshNetwork.ComputerName & VbCrlf & _
"IP Address1: " & vbTab & strIP
Msgbox message, , title
I'm trying to search through an array for a printer and if the printer exists display the name in the HTA. That bit works ok, but when no printer is found in the array all the installed printers on the device are displayed. is there a way to only show printers that are found
Set objFSO = CreateObject("Scripting.FileSystemObject")
arrPrinters = Split(objFSO.OpenTextFile("C:\Windows\DEW\denied-printers.txt" ,ForReading).ReadAll(), VbCrLf)
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery("Select * from Win32_Printer")
For Each objPrinter in colInstalledPrinters
localprinter = objPrinter.Name
For Each strLine in arrPrinters
If inStr(localprinter,strLine) > 0 Then
strHTML = strHTML & "<tr><td>" & localprinter & "</td></tr>"
End If
Next
Next
try this
If inStr(localprinter,strLine) > 0 OR inStr(localprinter,strLine) = NULL Then
End If
the problem is if array is empty, strline is NULL and when you used it in inStr, it returns NULL instead of '0'. That is one possibility – tunmise fasipe 3 mins ago edit
Your main problem - i guess without knowing the contents of yoyr file - is that in instr(textToSearch, searchString) you switch the two parameters .
Anyway, here a version of your code i tested.
const ForReading = 1
strComputer = "."
set objFSO = createObject("Scripting.FileSystemObject")
printers = objFSO.OpenTextFile("denied-printers.txt" ,ForReading).ReadAll()
set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
set colInstalledPrinters = objWMIService.ExecQuery("Select * from Win32_Printer")
for Each objPrinter in colInstalledPrinters
localprinter = objPrinter.Name
if instr(printers, localprinter) then
strHTML = strHTML & "<tr><td>" & localprinter & "</td></tr>"
end if
next
EDIT: here the stand alone vbscript version, save it to a .vbs file and run to test
on error resume next
const ForReading = 1
strComputer = "."
file = "denied-printers.txt"
set objFSO = createObject("Scripting.FileSystemObject")
set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
set colInstalledPrinters = objWMIService.ExecQuery("Select * from Win32_Printer")
if err.number=0 then
printers = objFSO.OpenTextFile(file ,ForReading).ReadAll()
if err.number=0 then
set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
set colInstalledPrinters = objWMIService.ExecQuery("Select * from Win32_Printer")
for Each objPrinter in colInstalledPrinters
localprinter = objPrinter.Name
if instr(printers, localprinter) then
wscript.echo localprinter & " found in " & file
end if
next
else
wscript.echo "file " & file & " not found, showing all printers"
for Each objPrinter in colInstalledPrinters
wscript.echo objPrinter.Name
next
end if
else
wscript.echo "Error" & err.description
end if