Accessing cmd prompt in remote machine using vbscript - vbscript

How to access Command Prompt in remote machine using VBScript?
We are in process of creating a tool to fetch component services and its status on remote machines.
Currently we are able to achieve this in our local machine by using the below code:
Dim objShell : Set objShell = CreateObject("WScript.Shell")
Set getvalue=objShell.Exec("cmd.exe /C sc queryex type= service state= all")
Do
output = getvalue.StdOut.ReadLine
MsgBox output
Loop While getvalue.StdOut.AtEndOfStream = False
But we need to achieve the same in remote machine.

Set objWMIService = GetObject("winmgmts:\\127.0.0.1\root\cimv2")
Set config = objWMIService.ExecQuery("Select * From Win32_Service")
For Each thing in Config
Msgbox thing.Caption
Next
Is how we query services in vbscript. These are the properties available. https://msdn.microsoft.com/en-us/library/aa394418(v=vs.85).aspx

Related

Task killer script problems

I found code to kill automatically tasks in windows 10, but the task I want to kill are: Host Service: Local System that keeps updating Windows when I'm doing something and the main problem is: It has the same name (svhost.exe) as other processes.
Any ways to pull this off? I'm still a newbie
set service = GetObject ("winmgmts:")
Dim oShell : Set oShell = CreateObject("WScript.Shell")
Do While 11>10
for each Process in Service.InstancesOf ("Win64_Process")
If Process.Name = "xxx.exe" then
oShell.Run "taskkill /im xxx.exe", , True
End If
next
WScript.Sleep 60000
Loop

schedule vbscript without task scheduler

I have a VBScript, Abc.vbs, which contains:
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "D:\Abc.bat" & Chr(34), 0
Set WshShell = Nothing
How do I schedule Abc.vbs to run without Windows Task Scheduler ?
'Put this code above your own code in the VBS file.
Do While(cStr(Time) <> "22:00:00") 'Please check the date/time format on your computer and make changes accordingly. This code schedules the script to 10 PM
Wscript.Sleep = 100
Loop
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "D:\Abc.bat" & Chr(34), 0
Set WshShell = Nothing
Now double click this VBS and leave it as it is.
It depends on your environment, operating system, when you want it to run and what tools you have available to you.
In windows you could use registry keys or group policy to run your script at logon which can then run in a loop and then at set intervals run your batch file.
You could install an executable as windows service, which basically does what the above but uses an executable.
If you have access to SQL service it has the ability to schedule jobs as well.
There are probably a few more ways but again they depend on your specific use case.

How to check default host for VBScript is WScript or CScript?

I would like to know what is default host for VBScript on particular machine, whether that is set to WScript or CScript ? For example, if I use cscript //h:cscript //s then is there any way I can check host for VBScript is set to cscript?
I found commands to change default host but did not find command to check default host.
Edit:
C:\Windows\system32>cscript //h:cscript //s
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.
Command line options are saved.
The default script host is now set to cscript.exe.
C:\Windows\system32>ftype VBSFile
VBSFile="%SystemRoot%\System32\WScript.exe" "%1" %*
How Can I Determine the Default Script Host on a Computer Before I Run a Script?
Const HKEY_CLASSES_ROOT = &H80000000
strComputer = "."
Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = "VBSFile\Shell\Open\Command"
objRegistry.GetExpandedStringValue HKEY_CLASSES_ROOT,strKeyPath,vbNullString,strValue
strValue = LCase(strValue)
Wscript.Echo strValue
If InStr(strValue, "wscript.exe") then
Wscript.Echo "WScript"
Else
Wscript.Echo "CScript"
End If

How can I run this VBS script from a shared network drive, from command line on multiple computers?

I created a VBS script and put it on a shared drive IT uses in my office, what I want is to be able to use it with another computer via command line WITHOUT writing the entire path because it's very long, and defeats the purpose of the script.
The script is this:
Set oShell = WScript.CreateObject("WScript.Shell")
oShell.run "cmd.exe /c net stop AeXAgentSrvHost", 1, true
oShell.run "cmd.exe /c net stop ""Altiris Deployment Agent""", 1, true
oShell.run "cmd.exe /c net start AeXAgentSrvHost", 1, true
oShell.run "cmd.exe /c net start ""Altiris Deployment Agent""", 1, true
Set oShell = Nothing'
Here's an example of using the Win32_Service class within a VBScript to stop a service on a remote computer. There's no need to spawn a script on the remote PC (unless you really want to).
Const COMPUTER_NAME = "johndoe1"
Const SERVICE_NAME = "AeXAgentSrvHost"
Dim objWMI
Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & COMPUTER_NAME & "\root\cimv2")
With objWMI.Get("Win32_Service.Name='" & SERVICE_NAME & "'")
If .Started Then .StopService
End With
See the link above for a complete list of properties and methods for the Win32_Service class.
If you need to run this for many computers, you have a number of options:
Use a hard-coded array of computer names
Read a list from a text file, database, or other form of storage
Prompt for computer names using an InputBox() or read from StdIn
Pass one or more names as command-line parameters
Etc.
There should be plenty of examples for each method on this site.

VBScript Admin CMD Command Executed Remotely

Trying to execute a simple dns flush remotely from a machine, but for one reason or the other - I need to run the cmd as admin. The cmd runs fine under the account physically in person, but you need to do the whole right click -> run as admin bit.
Right now, remotePC is set to local or '.'
Set shl = WScript.CreateObject("WScript.Shell")
'Input remote PC
remotePC = "."
'Command which will be executed
strCommand = "cmd.exe /C cd C:\WINDOWS\system32 & ipconfig.exe /flushdns & pause"
'Connect to the remote PC
'Impersonate with the default level?
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & remotePC & "\root\cimv2")
Set objProcess = objWMIService.Get("Win32_Process")
errReturn = objProcess.Create(strCommand, null, null, intProcessID)
Use PsExec for this purpose. Don't bother with VBScript.

Resources