Need a VB Script to check if service exist - vbscript

I want to write a VBS script which will check if specific service is installed/exist or not locally.
If it is not installed/exist, script will display message (any text) and disabled the network interface i.e. NIC.
If service exist and running, NO Action. Just exit.
If service exist but not running, same action, script will display message (any text) and disabled the network interface i.e. NIC.
i have below given code which is displaying a message in case one service is stop but it is not -
Checking if service exist or not
Disabling the NIC
strComputer = "."
Set objWMIService = Getobject("winmgmts:"_
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colRunningServices = onjWMIService.ExecQuery _
("select State from Win32_Service where Name = 'dhcp'")
For Each objService in colRunningServices
If objService.State <> "Running" Then
errReturn = msgbox ("Stopped")
End If
Next
Please help. Thanks in advance.

To know if service is installed, check colRunningServices.Count . It will be 0 if no service match the wmi query.
To disable NIC, use a wmi query SELECT * FROM Win32_NetworkAdapter, iterate de returned collection of NICs, find the one you are interested and use the Disable/Enable method of it. BUT this only will work if OS is Vista or later.

It would look something like this but I cannot test this!
Option Explicit
Const TITLE = "Title"
Const SERVICE = "dhcp"
Dim wmi
Dim svcs,svc
Set wmi = GetObject("winmgmts:\\.\root\cimv2")
Set svcs = wmi.ExecQuery("Select * from Win32_Service where Name = '" & SERVICE & "'")
If svcs.Count = 0 Then
Call MsgBox(SERVICE & " service does not exist",vbCritical,TITLE)
Call disableNIC(wmi)
Else
For Each svc In svcs
If svc.State <> "Running" Then
Call MsgBox(SERVICE & " service is not running",vbCritical,TITLE)
Call disableNIC(wmi)
End If
Next
End If
Set wmi = Nothing
WScript.Quit
Sub disableNIC(ByRef wmi)
Dim nics,nic
Set nics = wmi.ExecQuery("Select * from Win32_NetworkAdapter")
For Each nic In nics
nic.Disable
Next
End Sub
Beware that this disables every NIC. If you want to specify one you'd have to add a where clause in disableNIC. Also I imagine you would have to run this as an administrator.
Seems like a weird thing to want to do though...

Related

Get running instance of application from WMI process?

I'm trying to get hold of running instances of MS Access 2010+ (on Win10) but the usual tip; GetObject(, "Access.Application") ... for works only for hidden instances started by myself with script, but not any instances started from GUI by the user.
And yes, I've read perhaps ten or more google hits on the subject, both on WMI and GetObject, but I seem to have missed something important.
However, I've tried the code below and I can get hold of any process of running Access instances in this way, and I can even .terminate() them, but, that's not what I want to do. Instead I want to grab the process and assign it to a usable (correct type) Access variable (see "OutInstance" in the code below) :
[Edit: Using WHERE clause, and skipped Exit as to retrieve the last instance]
Public Function GetRunningInstance(sAppName sComputer, ByRef OutInstance)
Dim oWMIService
Dim wProcesses
Dim oPrc
GetRunningInstance = False
Set OutInstance = Nothing
if sComputer = "" then sComputer = "."
Set oWMIService = GetObject("winmgmts:" & "{impersonationLevel=" & _
"impersonate}!\\" & sComputer & "\root\cimv2")
Set wProcesses = oWMIService.ExecQuery ("SELECT * FROM Win32_Process " & _
"WHERE Name = '" & sAppName & "'")
For Each oPrc in wProcesses
''' oPrc.Terminate() ''' Works, I can shut down Access...
Set OutInstance = oPrc
GetRunningInstance = True
''' By not exiting we get the last instance.
Next
End Function
Now, after trying to get hold of an instance, how do I "cast" the process to a usable Access application variable in this VBScript?
Sub Test_DoStuff()
Dim InstProc
Dim AccessApp
If GetRunningInstance("msaccess.exe", "127.0.0.1", InstProc) Then
Set AccessApp = ''' cast 'InstProc' to my "Access.Application" somehow?
Else
Set AccessApp = CreateObject("Access.Application")
End If
'''
''' Doing my stuff
'''
AccessApp.CloseCurrentDatabase
AccessApp.DoCmd.Quit
End Sub
Test
I (also) don't understand why GetObject(, "Access.Application") doesn't work in all cases. Permissions? (I understand that it's 'unsafe' to close a database currently being used by a user, but also that can be dealt with).
// Rolf

VBScript to stop service works in WinXP not in Win7

I have a boot script that copies some ini files, based on the OS being used, for MySQL Server. The script works fine on WinXP but in Win7 it does not stop the service. No error is reported. The script appears to run fine except for the fact that the ini files are not copied if the service is running.
I have removed the stop service portion of the code and placed it in a script by itself. However, it still works on WinXP and not on Win7.
Can anyone see any reason for the script not working?
Sub StopService(computerName, serviceName)
Dim service
Set service = GetService(computerName, serviceName)
If(service Is Nothing) Then
Call Log.Warning("The " & serviceName & " service on the " & computerName & " computer was not found")
Else
service.StopService()
End If
End Sub
Function GetService(computerName, serviceName)
Dim wmiService, objectsList, eObjectsList, ObjService, item, sysID
Set wmiService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & computerName & "\root\cimv2")
Set objectsList = wmiService.ExecQuery("Select * from Win32_Service Where Name = '" & serviceName & "'")
For Each ObjService in objectsList
If(ObjService.Name = serviceName) Then
Set GetService = ObjService
Exit Function
End If
Next
Set GetService = Nothing
End Function
StopService ".", "MySQL56"
Thanks for any and all help,
Charles

Return volume name in Do loop

I'm trying to create a script for HP UFT to automatically create/mount/unmount VHDs as needed. The problem is that diskpart takes quite a while to create, mount, format and label the VHD. I need a method to pause the script while the VHD is being created/mounted, after which it will continue on with the script.
The variable uftDrive is currently returning a drive letter, not a volume name, so the loop just runs indefinitely. Any thoughts as to how to pass the volume names as a variable? The VHD script is automatically assigning the first-available drive letter to the drive, as we have multiple machines that UFT will be run on, and they don't have identical network drive mappings, forcing us to dynamically detect the drive by volume name.
'===========================================================================
'This will check to see if the Virtual Hard Disk (VHD) exists, and if not,
'create it
'===========================================================================
Dim makevhdExists, vhdExists
Set makevhdExists = CreateObject("Scripting.FileSystemObject")
Set vhdExists = CreateObject("Scripting.FileSystemObject")
If Not makevhdExists.FileExists("c:\UFT\mountVHD.bat") Then
makevhdExists.CopyFile "\\companyADfolders\Users\UFT\VHD\*.*", "c:\UFT\"
End If
If Not makevhdExists.FileExists("c:\UFT\unmountVHD.bat") Then
wait 2
ElseIf Not vhdExists.FileExists("C:\UFT\UFT.vhd") Then
SystemUtil.Run "cmd","/c""C:\UFT\createVHD.bat"""
End If
Dim uftExists, uftDrive
uftExists = "False"
Do
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
("Select * From Win32_LogicalDisk Where VolumeName = 'UFT'")
For Each objItem in colItems
uftDrive = objItem.Name 'This is currently returning a drive letter,
'not a volume name
If uftDrive.Name = "UFT" Then
uftExists = "True"**
End If
Next
Loop Until uftExists = "True"
For the wait part
WScript.Sleep 5000
where the value indicated is milliseconds
For the second part, maybe i'm missing something, so, instead of an answer I have one silly question:
If you query wmi for win32_logicaldisk instances where their VolumeName is UFT, WHY your if command checks the Name property instead of the VolumeName property?
In any case, you don't need that if. If colItems.Count is greater than 0, there is at least one instance that matches the indicated condition
Something like this should work
Dim uftExists
uftExists = False
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Do
Set colItems = objWMIService.ExecQuery _
("Select * From Win32_LogicalDisk Where VolumeName = 'UFT'")
If colItems.Count > 0 Then
utfExists = True
Else
Sleep 500
End If
Loop Until uftExists
Changing ObjItem.Name to ObjItem.VolumeName worked!
For Each objItem in colItems
uftDrive = objItem.VolumeName 'This is currently returning a drive letter, not a volume name
If uftDrive = "UFT" Then
uftExists = "True"**
End If
Next
For the first question: To delay the script, you can use this function:
WScript.Sleep 1000
The number is in Milliseconds, so 1000 is 1 second.

Find a service with Partial name VBS

We have a service that runs with different names on different machines like
Bomgar-scadsadccd, Bomgarsdscchfn, Bomgarscnkfkdk
So, here we need to write a VB script that will find this service with partial name "Bomgar"
and check it's status like
1) Not present, then should install from a shared folder like "start \10.216.16.245\Bomgar.exe"
2) Installed but not running, then start the service.
3) Installed and running then quit.
How can we achieve this?
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colRunningServices = objWMIService.ExecQuery("Select * from Win32_Service where Name='bomgar*'")
nItems = colRunningServices.Count
If nItems > 0 Then
For Each objItem in colRunningServices
If objItem.State = "Stopped" Then
objItem.startservice
ElseIf objItem.State = "Running" Then
exit
End If
Next
Else
start \\10.18.23.245\Shared\Bomgar.exe
End If
You can use the like keyword and % wildcards in your WQL statement to find any services containing certain text. For example:
Set colRunningServices = objWMIService.ExecQuery("Select * from Win32_Service where Name like '%bomgar%'")
And instead of start, you'll need to use something like the Run command of the WshShell object to launch your EXE:
With CreateObject("WScript.Shell")
.Run "\\10.18.23.245\Shared\Bomgar.exe"
End With

How to write a VBS Script to check a website and then stop and start a windows service

I need to write a VBS script to check a website for certain text : "Service Unavailable" and "Error in /"
When it finds this I need it to restart the windows service "World wide publishing service"
Where do I start?
Any help would be much appreciated!
Cheers!
Andy
You can check on a web site by using the MSXML2.XMLHTTP object (ie the same object that internet explorer uses to fire AJAX requests) and checking the status code (200 is status OK, 404 is page not found etc)
dim http: set http = CreateObject("MSXML2.XMLHTTP")
http.open "GET", "http://site.com?param=value", false
http.send
if not http.status = 200 then
' something not right, start your service
end if
as far as starting a service is concerned, this page has quite a few examples of working with services, of which this is how to start one (copied verbatim, not tested):
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colServiceList = objWMIService.ExecQuery _
("Select * from Win32_Service where Name='NetDDE'")
For each objService in colServiceList
errReturn = objService.StartService()
Next
Wscript.Sleep 20000
Set colServiceList = objWMIService.ExecQuery("Associators of " _
& "{Win32_Service.Name='NetDDE'} Where " _
& "AssocClass=Win32_DependentService " & "Role=Dependent" )
For each objService in colServiceList
objService.StartService()
Next
if the service you want to start is IIS, then perhaps you dispense with the http request and instead directly detect whether the IIS service is running or not

Resources