Getting Volume Serial Number Of Script Location - vbscript

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

Related

Parse properties string in an array to For Each loop

I am trying to get information about the battery in my laptop by using VBScript.
It is more properties. I want to parse arrayItems("Name","Availability","BatteryStatus","Chemistry") for each objItem.
For example:
For iii = 0 To UBound(arrayItems)
WScript.Echo "Result of iii:" & objItem.arrayItems (iii)
Next
I do not want to enter manually as "Availability: " & objItem.Availability, "BatteryStatus: " & objItem.BatteryStatus, ...
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Battery",,48)
For Each objItem In colItems
WScript.Echo "Name: " & objItem.Name
WScript.Echo "Availability: " & objItem.Availability
WScript.Echo "BatteryStatus: " & objItem.BatteryStatus
WScript.Echo "Chemistry: " & objItem.Chemistry
WScript.Echo "DesignVoltage: " & objItem.DesignVoltage
WScript.Echo "EstimatedChargeRemaining: " & objItem.EstimatedChargeRemaining
WScript.Echo "Status: " & objItem.Status
Next
What you want can be done via the object's properties_ property set:
arrayItems = Array("Name", "Availability", "BatteryStatus", "Chemistry")
For Each objItem In colItems
For Each name In arrayItems
WScript.Echo name & ": " & objItem.properties_(name)
Next
Next

Using WMI objects in VB script to decide which programs to run

I have a vbscript that will find the OS, Service pack and OS Architecture but when i try to use the object in a If then statement its not defined, I tried to declare it but that didn't work. What i am trying to complete is create a script that will define the OS, Service pack and whether the system is x86 or x64 and the execute a program that will install the updates specific for that system but I'm stuck.
Set dtmConvertedDate = CreateObject("WbemScripting.SWbemDateTime")
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
Wscript.Echo "Version: " & objOperatingSystem.Version
Wscript.Echo "Service Pack: " & objOperatingSystem.CSDVersion
Wscript.Echo "CPU Bit: " & objOperatingSystem.OSArchitecture
Next
I am assuming you are not storing those variables you are echoing with "wscript.echo"..
You could use this example (or make a case statement):
Set dtmConvertedDate = CreateObject("WbemScripting.SWbemDateTime")
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
osVersion = objOperatingSystem.Version
osCSDVersion = objOperatingSystem.CSDVersion
osArchitecture = objOperatingSystem.OSArchitecture
Next
Wscript.Echo "Version: " & osVersion
Wscript.Echo "Service Pack: " & osCSDVersion
Wscript.Echo "CPU Bit: " & osArchitecture
If InStr(LCase(osArchitecture), "64-bit") Then
wscript.echo "64-bit"
' .... Do stuff here ....
Else
wscript.echo "not 64-bit"
' .... Do stuff here ....
End If

VBscript for use with multiple IP addresses

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

VBscript to get info from pc

Hello I´m writing this script where i need Serial number, IP address, login user, produkt key from office, autoroute to a text file. I have begun to write a script but having some issues. Not a done script but im kinda stuck to here.
Here´s the script:
On Error Resume Next
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
arrComputers = Array("localhost")
For Each strComputer In arrComputers
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService1.ExecQuery("SELECT * FROM Win32_ComputerSystemProduct", "WQL", _ wbemFlagReturnImmediately + wbemFlagForwardOnly)
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService2.ExecQuery("SELECT * FROM Win32_ComputerSystem", "WQL", _ wbemFlagReturnImmediately + wbemFlagForwardOnly)
Set objWMIService = GetObject( "winmgmts:\\" & strCoputer & ".\root\CIMV2" )
Set colItems = objWMIService.ExecQuery( "SELECT * FROM Win32_NetworkAdapterConfiguration", "WQL", _ wbemFlagReturnImmediately + wbemFlagForwardOnly)
next
For Each objItem In colItems2
Dim fso, tf
Set fso = CreateObject("Scripting.FileSystemObject")
Set tf = fso.CreateTextFile ("c:\" & objItem.UserName & ".txt", True)
tf.Write "Username: " & objItem.UserName
tf.WriteBlankLines(1)
tf.Write "Hostname: " & objItem.Name
tf.WriteBlankLines(1)
tf.Write "Domain: " & objItem.Domain
tf.WriteBlankLines(2)
next
For Each objItem In colItems1
tf.Write "Serial: " & objItem.IdentifyingNumber
tf.WriteBlankLines(1)
tf.Write "Model: " & objItem.Name
tf.WriteBlankLines(2)
tf.Write "Vendor: " & objItem.Vendor
tf.WriteBlankLines(1)
tf.Write "Version: " & objItem.Version
tf.WriteBlankLines(2)
Next
For Each objItem In colItems3
tf.Write "IP Address: " & objItem.IPAddress
tf.WriteBlankLines(1)
tf.Close
next
Microsoft has created a free tool called Scriptomatic which automatically creates vbscript code for such kind of task. You can download scriptomatic from the below location.
http://www.microsoft.com/en-in/download/details.aspx?id=12028

searching arrays with vbs

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

Resources