VB Script GetObject method error...Help please WMI - vbscript

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

Related

How to terminate a VBS File using Vb script

How to terminate a VBS File using Vb script.. I tried this code and it is not working,
Call StopProcessVBS(strComputer,strProcess)
Function StopProcessVBS (strComputerArg,strProcessArg)
Set WshShell = CreateObject("WScript.Shell")
Dim objWMIService, colProcessList
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputerArg & "\root\cimv2")
Set colItems = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = 'cscript.exe' OR Name = 'wscript.exe'")
For Each objItem in colItems
If objItem.CommandLine = strProcessArg Then
**objItem.CommandLine.Terminate()**
End If
Next
Set WshShell = Nothing
Set objWMIService = Nothing
Set colItems = Nothing
End Function
Finally Worked, Tried the below code
strComputer = "."
Call StopProcessVBS(strComputer,strProcess)
Function StopProcessVBS (strComputerArg,strProcessArg)
Set WshShell = CreateObject("WScript.Shell")
Dim objWMIService, colProcessList
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputerArg & "\root\cimv2")
Set colItems = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = 'cscript.exe' OR Name = 'wscript.exe'")
For Each objItem in colItems
If Instr (1,Replace(objItem.CommandLine,"""",""),strProcessArg) Then
objItem.Terminate()
End If
Next
Set WshShell = Nothing
Set objWMIService = Nothing
Set colItems = Nothing
End Function

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 WMI Using Public Member Functions (nVidia NV2)

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

Using VBScript how can I check if the Spooler service is started and if not start it?

I'd like to use VBScript to check if the Spooler service is started and if not start it, the code below checks the service status but I need some help modifying this so I can check if it is started.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colRunningServices = objWMIService.ExecQuery _
("Select * from Win32_Service")
For Each objService in colRunningServices
Wscript.Echo objService.DisplayName & VbTab & objService.State
Next
Many thanks
Steven
How about something like this. This command will start it if it isn't already running. No need to check in advance.
Dim shell
Set shell = CreateObject("WScript.Shell")
shell.Run "NET START spooler", 1, false
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colRunningServices = objWMIService.ExecQuery _
("select State from Win32_Service where Name = 'Spooler'")
For Each objService in colRunningServices
If objService.State <> "Running" Then
errReturn = objService.StartService()
End If
Next
Note you can also use objService.started to check if its started.
Just for the completeless, here's an alternative variant using the Shell.Application object:
Const strServiceName = "Spooler"
Set oShell = CreateObject("Shell.Application")
If Not oShell.IsServiceRunning(strServiceName) Then
oShell.ServiceStart strServiceName, False
End If
Or simply:
Set oShell = CreateObject("Shell.Application")
oShell.ServiceStart "Spooler", False ''# This returns False if the service is already running

How to terminate process using VBScript

I have this VBScript code to terminate one process
Const strComputer = "."
Dim objWMIService, colProcessList
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'Process.exe'")
For Each objProcess in colProcessList
objProcess.Terminate()
Next
It works fine with some processes, but when it comes to any process runs under SYSTEM, it can't stop it.
Is there is anything I need to add to kill the process under SYSTEM?
The way I have gotten this to work in the past is by using PsKill from Microsoft's SysInternals. PsKill can terminate system processes and any processes that are locked.
You need to download the executable and place it in the same directory as the script or add it's path in the WshShell.Exec call. Here's your sample code changed to use PsKill.
Const strComputer = "."
Set WshShell = CreateObject("WScript.Shell")
Dim objWMIService, colProcessList
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'Process.exe'")
For Each objProcess in colProcessList
WshShell.Exec "PSKill " & objProcess.ProcessId
Next
Try explicit assert debug privilege {impersonationLevel=impersonate,(debug)}:
Set wmi = GetObject("winmgmts:{impersonationLevel=impersonate,(debug)}!\\.\root\CIMV2")
Set procs = wmi.ExecQuery("SELECT * FROM Win32_Process WHERE Name='SearchIndexer.exe'", , 48)
For Each proc In procs
proc.Terminate
Next

Resources