vbscript WMI Using Public Member Functions (nVidia NV2) - vbscript

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...

Related

Join value of the properties in the For Each loop to one line output

Sorry for my bad English.
At here Parse properties string in an array to For Each loop, with support from https://stackoverflow.com/users/1630171/
I was able to read the battery information. But it is just separate information, so how do we assemble it and export the results into one line.
Dim strResult, objItem, arrayItem
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Battery")
arrayItems = Array("Name", "Availability", "BatteryStatus", "Chemistry")
For Each objItem in colItems
For Each arrayItem In arrayItems
strResult = Join(objItem.Properties_(arrayItem))
Next
WScript.Echo strResult
Next
And it shows empty result of the output.
You need to collect the properties and join them into a string.
Try this:
Option Explicit
Dim objItem, arrayItems, strComputer
Dim objWMIService, colItems, arrResult, i
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
'The third parameter 48 for 'ExecQuery' is the combination of wbemFlagForwardOnly + wbemFlagReturnImmediately
'see: https://learn.microsoft.com/en-us/windows/desktop/wmisdk/swbemservices-execquery#parameters
Set colItems = objWMIService.ExecQuery("Select * from Win32_Battery",,48)
arrayItems = Array("Name", "Availability", "BatteryStatus", "Chemistry")
For Each objItem in colItems
'create/clear an array to store the various pieces of information
ReDim arrResult(UBound(arrayItems))
For i = 0 To (UBound(arrayItems))
' Sometimes the WMI property returns a Null value (Nothing)..
If Not IsNull (objItem.Properties_(arrayItems(i))) Then
arrResult(i) = objItem.Properties_(arrayItems(i))
Else
arrResult(i) = "Unknown"
End If
Next
'as example I'm using the Tab character to join the pieces
WScript.Echo Join(arrResult, Chr(9))
Next

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 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

VB Script GetObject method error...Help please WMI

at this point i get error message:
Set objWMI = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Error: Invalid procedure call or Argument: "GetObject"
Code: 800A005
Source: Microsoft VBScript Runtime error
Complete rewrite.
Here is the code generated by WMI Code Creator, with very minor editing.
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_ComputerSystem",,48)
For Each objItem in colItems
MsgBox "CurrentTimeZone: " & objItem.CurrentTimeZone
Next

Resources