Parse properties string in an array to For Each loop - vbscript

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

Related

Detect if headphones are plugged in or not via VBScript

Is any a way to detect if headphones are plugged in or not via VBScript?
This link doesnt help Switching current active sound device using VBScript?
You should be able to use the Win32_SoundDevice WMI class. Here is a sample script that might be a good starting point:
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_SoundDevice",,48)
For Each objItem in colItems
Wscript.Echo "Availability: " & objItem.Availability
Wscript.Echo "Caption: " & objItem.Caption
Wscript.Echo "Description: " & objItem.Description
Wscript.Echo "Name: " & objItem.Name
Wscript.Echo "Status: " & objItem.Status
Wscript.Echo "StatusInfo: " & objItem.StatusInfo
Next
(source)
I would do some comparison runs before and after plugging in your headphones and go from there.

how to create a VB script file without a pop up window

I googled a code that works just as I wanted,
But when I schedule it in task manager issue occurs ..after every pop up screen i need to click ok..then only the file gets updated.Please let me know what changes are to be done so that after running VBS it silently updates the file.
actual code:
source:http://www.wisesoft.co.uk/scripts/vbscript_disk_space_usage_report.aspx
OPTION EXPLICIT
CONST strComputer = "."
CONST strReport = "D:\diskspace.txt"
DIM objWMIService, objItem, colItems
DIM strDriveType, strDiskSize, txt
SET objWMIService = GETOBJECT("winmgmts:\\" & strComputer & "\root\cimv2")
SET colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk WHERE DriveType=3")
txt = "Drive" & vbtab & "Size" & vbtab & "Used" & vbtab & "Free" & vbtab & "Free(%)" & vbcrlf
FOR EACH objItem in colItems
DIM pctFreeSpace,strFreeSpace,strusedSpace
pctFreeSpace = INT((objItem.FreeSpace / objItem.Size) * 1000)/10
strDiskSize = Int(objItem.Size /1073741824) & "Gb"
strFreeSpace = Int(objItem.FreeSpace /1073741824) & "Gb"
strUsedSpace = Int((objItem.Size-objItem.FreeSpace)/1073741824) & "Gb"
txt = txt & objItem.Name & vbtab & strDiskSize & vbtab & strUsedSpace & vbTab & strFreeSpace & vbtab & pctFreeSpace & vbcrlf
NEXT
writeTextFile txt, strReport
wscript.echo "Report written to " & strReport & vbcrlf & vbcrlf & txt
' Procedure to write output to a text file
PRIVATE SUB writeTextFile(BYVAL txt,BYVAL strTextFilePath)
DIM objFSO,objTextFile
SET objFSO = CREATEOBJECT("Scripting.FileSystemObject")
SET objTextFile = objFSO.CreateTextFile(strTextFilePath)
objTextFile.Write(txt)
objTextFile.Close
SET objTextFile = NOTHING
END SUB
Call the script with cscript script_file.vbs instead of wscript script_file.vbs.
Popup massage genarated by wscript.echo if you delete that line, code will run silently
wscript.echo "Report written to " & strReport & vbcrlf & vbcrlf & txt

How to get unique id of a mouse

I want to get a unique id of a mouse provided that every mouse brand is the same in a laboratory.
I have tried using WMIC to get device attributes. My VBS script is this:
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_PointingDevice",,48)
Wscript.Echo "DeviceID: " & objItem.DeviceID
I have tried generating this script with different mouse brand and it outputs a unique device id. But when I use the same model/brand of mouse, the same device id is generated. Please help me find a unique data to be used to identify every mouse in a laboratory.
I think Thangadurai is right with his comment do your original question... However, you could try to find desired mouse id running next code snippets.
The simpliest solution with wmic:
wmic path Win32_PointingDevice get * /FORMAT:Textvaluelist.xsl
About the same output with vbScript: use cscript 28273913.vbs if saved as 28273913.vbs.
' VB Script Document
option explicit
' NameSpace: \root\CIMV2 Class : Win32_PointingDevice
' D:\VB_scripts_help\Scriptomatic
'
On Error GOTO 0
Dim arrComputers, strComputer, objWMIService, colItems, objItem
Dim strPowerManagementCapabilities
arrComputers = Array(".")
WScript.Echo "NameSpace: \root\CIMV2 Class : Win32_PointingDevice"
For Each strComputer In arrComputers
WScript.Echo "..."
WScript.Echo "=========================================="
WScript.Echo "Computer: " & strComputer
WScript.Echo "=========================================="
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_PointingDevice", "WQL", _
wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each objItem In colItems
WScript.Echo "Availability: " & objItem.Availability
WScript.Echo "Caption: " & objItem.Caption
WScript.Echo "ConfigManagerErrorCode: " & objItem.ConfigManagerErrorCode
WScript.Echo "ConfigManagerUserConfig: " & objItem.ConfigManagerUserConfig
WScript.Echo "CreationClassName: " & objItem.CreationClassName
WScript.Echo "Description: " & objItem.Description
WScript.Echo "DeviceID: " & objItem.DeviceID
WScript.Echo "DeviceInterface: " & objItem.DeviceInterface
WScript.Echo "DoubleSpeedThreshold: " & objItem.DoubleSpeedThreshold
WScript.Echo "ErrorCleared: " & objItem.ErrorCleared
WScript.Echo "ErrorDescription: " & objItem.ErrorDescription
WScript.Echo "Handedness: " & objItem.Handedness
WScript.Echo "HardwareType: " & objItem.HardwareType
WScript.Echo "InfFileName: " & objItem.InfFileName
WScript.Echo "InfSection: " & objItem.InfSection
WScript.Echo "InstallDate: " & WMIDateStringToDate(objItem.InstallDate)
WScript.Echo "IsLocked: " & objItem.IsLocked
WScript.Echo "LastErrorCode: " & objItem.LastErrorCode
WScript.Echo "Manufacturer: " & objItem.Manufacturer
WScript.Echo "Name: " & objItem.Name
WScript.Echo "NumberOfButtons: " & objItem.NumberOfButtons
WScript.Echo "PNPDeviceID: " & objItem.PNPDeviceID
WScript.Echo "PointingType: " & objItem.PointingType
If Isnull( objItem.PowerManagementCapabilities) Then
strPowerManagementCapabilities=""
Else
strPowerManagementCapabilities=Join(objItem.PowerManagementCapabilities, ",")
End If
WScript.Echo "PowerManagementCapabilities: " & strPowerManagementCapabilities
WScript.Echo "PowerManagementSupported: " & objItem.PowerManagementSupported
WScript.Echo "QuadSpeedThreshold: " & objItem.QuadSpeedThreshold
WScript.Echo "Resolution: " & objItem.Resolution
WScript.Echo "SampleRate: " & objItem.SampleRate
WScript.Echo "Status: " & objItem.Status
WScript.Echo "StatusInfo: " & objItem.StatusInfo
WScript.Echo "Synch: " & objItem.Synch
WScript.Echo "SystemCreationClassName: " & objItem.SystemCreationClassName
WScript.Echo "SystemName: " & objItem.SystemName
WScript.Echo "."
Next
Next
Function WMIDateStringToDate(dtmDate)
WMIDateStringToDate = ( Left(dtmDate, 4) & "/" & _
Mid(dtmDate, 5, 2) & "/" & Mid(dtmDate, 7, 2) _
& " " & Mid (dtmDate, 9, 2) & ":" & Mid(dtmDate, 11, 2) & ":" & Mid(dtmDate,13, 2))
End Function
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
More complex than previous wmic example providing possibility to run it against more computers in one step. Note the arrComputers = Array(".") line. Here "." means This computer and could be rewritten by a list of computer names or IP addresses e.g.
arrComputers = Array _
( "computer_1_name" _
, "computer_2_IP" _
, "computer_3_name" _
)

Getting Volume Serial Number Of Script Location

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

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

Resources