I've got a script which needs to run on various different versions of windows server, including 2003. Yeah, I already KNOW it's "unsupported".
My script has to launch an executable, in a hidden window (though the code to do this is not shown below, because I was asked to cut it down to the bare minimum). I'm currently using win32_Process.Create as follows:
Set objProcess = GetObject("winmgmts:root\cimv2:Win32_Process")
errReturn = objProcess.Create("C:\myprog.exe", null, null, intProcessID)
This works ok on 2008 and 2012, but it is to be failing on 2003 with error code 3 "Insufficient privilege" returned in errReturn. It also works when run through cmd.exe, as an ordinary user, but the parent program is a service, check_mk_agent.exe, and so is not an "ordinary user". This script is run as one of check_mk_agent.exe's plugins.
I'm now going to work out how to use runas to try to simulate running it as the same user as the service runs as.
The two most common ways to launch a .exe from vbscript is WScript.Shell Run and Exec method. The main difference between the two is that you can capture the StdIn/StdOut/StdErr with the Exec method because applications are run in a child command shell.
Exec example:
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("calc")
Run example:
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "%windir%\notepad " & WScript.ScriptFullName
Related
I'm automating process of connection to remote PC which includes VPN+SSH+VNC and ssh stage automatization opens too much new windows instead of one expected.
Code:
Set oShell = CreateObject("WScript.Shell")
oShell.Run("""G:\Git\git-bash.exe""")
oShell.AppActivate "MINGW64:/"
Dim command
command = "ssh -A username#adress options~"
oShell.SendKeys command
Opens from 3 to 5 git-bash instances instead of 1.
Sending keys works as intended in 1 of this 3-5 window. How to prevent opening others?
oShell.Run(oShell.ExpandEnvironmentStrings("%COMSPEC% /C (start G:\Git\git-bash.exe)")) should help.
Might also try adding a WScript.Sleep 3000 before calling oShell.AppActivate to give the OS time to launch git-bash.exe and its dependencies.
Hopefully, drive G: is a local drive. If not, increase the Sleep time to give any anti-virus time to scan the process.
I am running the following VBScript (check.vbs):
Set service = GetObject ("winmgmts:")
For Each Process In Service.InstancesOf("Win32_Process")
If Process.Name = "cmd.exe" Then
WScript.Echo "cmd running"
WScript.Quit
End If
Next
CreateObject("WScript.Shell").Run("C:\system\file.bat")
This script will check whether cmd.exe is running or not. If it is running, this script will display a message "cmd running". If it is not running, this script will open a batch file C:\system\file.bat.
But what I actually need is: when I run this script check.vbs it needs to keep on checking until it finds that cmd.exe is not running.
Only if it found cmd.exe is not running it needs to run file.bat - after repeated checking in background (like any loop program).
In simple words, when opening check.vbs the script need to continously check that cmd.exe is running or not, once it found it's not running, it need to open file.bat.
Still not sure if I understand the question correctly, but assuming that you actually want a monitor that watches and re-spawns a particular process you could do something like this:
Set wmi = GetObject ("winmgmts://./root/civm2")
Sub CheckProcess(name, script)
For Each p In wmi.ExecQuery("SELECT * FROM Win32_Process")
If p.Name = name Then Exit Sub
Next
CreateObject("WScript.Shell").Run script
End Sub
Do
CheckProcess "cmd.exe", "C:\system\file.bat"
WScript.Sleep 100
Loop
During my internet searches, I have found a script that is supposed to stop a service. The current script runs, finds the services specified in an array, but doesn't seem to stop them. When the script outputs the services' State, it's still running. Below is the script.
sComputer = "."
aTargetSvcs= Array("mysql","Apache2.4")
Set oWMIService = GetObject("winmgmts:" & "{impersonationlevel=impersonate}!\\" _
& sComputer & "\root\cimv2")
Set cServices = oWMIService.ExecQuery("SELECT * FROM Win32_Service")
For Each oService In cServices
For Each sTargetSvc In aTargetSvcs
If LCase(oService.Name) = LCase(sTargetSvc) Then
If oService.State <> "Stopped" Then
oService.StopService()
Wscript.Echo oService.State
End If
End If
Next
Next
I am just testing it out with mysql and Apache2.4 services, but when this works, it will be deployed with a group policy to temporarily stop some AV services that are interfering with a domain modifier script.
The issue is likely a lack of permission in the context of the running script.
If you run the script from the command line make sure to start it through an Elevated Command Prompt, in modern Windows Operating Systems an elevated Command Prompt is denoted by the prefix Administrator: in the Window Title.
If you run the script from a Shortcut link make sure to specify Run As Administrator in the Advanced Properties screen.
I am logging into the server as Administrator to winserver2008.
I created a script called: vbscript.vbs
The purpose of this script is to auto login to linux via putty, then perform command line task.
Dim Shell
Set Shell = CreateObject("WScript.Shell")
output = Shell.Run("C:\putty.exe 1.2.3.4 9321")
wscript.sleep(500)
Shell.Sendkeys "root" & VBCrLf
wscript.sleep(30)
Shell.Sendkeys "password" & VBCrLf
wscript.sleep(30)
When I manually click on vbscript.vbs to execute it, vbscript will fill in root and password to putty.
When I use windows scheduler call to vbscript.vbs to execute it, vbscript won't fill in root and password to putty.
I suspect some permission issue.
I already set putty.exe to run as administrator, allow administrator, administrators group permission for it, but still fail to work when call via windows scheduler.
=====
I just tried with the second scenario, send 2 to the windows calculator, fail too..
testcalc.vbs
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "Calc.exe"
objShell.AppActivate "Calculator"
objShell.SendKeys "2"
Give up on trying to get SendKeys to work from a scheduled task, it's not going to happen. Instead simply pass the login and password on the command line:
output = Shell.Run("C:\putty.exe -l root -pw password 1.2.3.4 9321")
Alternatively do it with a session file and use -load.
If you are then going to execute commands over this connection then I believe you actually want plink rather than putty.
Running IIS6 on Windows 2003. I'm trying to set up a simple ASP page which runs a bash script:
dim wshShell
set wshShell = CreateObject("WScript.Shell")
dim command
command = "c:\inetpub\wwwroot\bin\bash.exe /cygdrive/c/inetpub/wwwroot/test.sh"
wshShell.Run(command)
set wshShell = nothing
I've configured IIS6 to use the account IUSR_SERVERNAME as the identity for the default application pool, and confirmed that the script executes when I run it from the command line using
runas /usr:IUSR_SERVERNAME [command]
If I set the command to be notepad.exe, IIS6 launches it (with no window, of course, but I can see it in Task Manager, and the user name is set to IUSR_SERVERNAME).
Is there something I'm overlooking that I need to configure? I've got a similar script running on Windows 7 / IIS7, and it wasn't difficult to get running.
Resolved: switched to the lightweight Mongoose web server (using a Tcl CGI script instead of ASP). For our purposes -- running a simple demo -- this works fine, and it was much less painful to get it up and running.