classic asp WScript.Shell run error - shell

first time post here.
I am using classic ASP (vbscript) on a windows 2008 server with IIS7.
I am using a command-line utility with the following code:
(the dir is not the actual code but used for testing purposes!)
dim oShell
Set oShell = CreateObject("WScript.Shell")
oShell.run "%COMSPEC% /C dir c:\ > c:/tmp/dir.txt", 0, TRUE
set oShell = nothing
Now here is the problem: this works on most of my customers servers without any problems,
but one in particular gives an error without a description on the oShell.run line.
If I kill w3wp.exe or in IIS manager, recycle the application pool, it starts working again...
but the error always returns...
I have tried using process monitor, but I don't know what to look for...
Any idea what could be causing this behavior? or possibly some sort of workaround?

Try set oShell = Server.CreateObject("wscript.shell")

Related

How to run below command from vbscript

I have written vbscript code to set classpath of putty from command prompt
Dim oShell
putty_path="setx path %path%;C:\putty"
Set oShell = WScript.CreateObject ("WScript.Shell")
oShell.run "cmd /K" & putty_path
Set oShell = Nothing
When i execute vbscript code i am getting below error in command prompt
ERROR: Invalid syntax. Default option is not allowed more than '2' time(s).
Type "SETX /?" for usage. Please help
Because it has already worked, so it can't do it a second or subsequent time as it worked the first time. Just ignore.
Plus it's unnecessary what you are doing, just shell to PuTTY (you don't even need to use the extension).
oShell.run "putty"

Batch file not running correctly when called from Visual Basics

I am attempting to write code that will call a batch file, wait until it is complete and then move on to the next line of code in Visual Basic. The code I have does open the batch file, however it does not actually execute. It opens the cmd window and displays some non-code I've written, but does not execute. I've tested the batch file by clicking it and running through cmd and it works fine. Just doesn't run through VBS.
Dim objShell As Object
Set objShell = CreateObject("Wscript.Shell")
Wait = True
objShell.Run """C:\Users\wjones\Documents\Data Loader\Scheduler\process.bat""", 1, Wait
try this:
objShell.Run "cmd /c C:\Users\wjones\Documents\Data Loader\Scheduler\process.bat", 1, Wait

Is there a way to start a program minimized with VBScript using WScript.Shell?

Here is some example code I have right now to launch an app:
Set objShell = Wscript.CreateObject("WScript.Shell")
objShell.Run """C:\Program Files\Handbrake\HandBrakeCLI.exe"""
I tried the following to launch the app minimized but it didn't work. I'm assuming this only works from a normal command prompt?
Set objShell = Wscript.CreateObject("WScript.Shell")
objShell.Run "start /MIN ""C:\Program Files\Handbrake\HandBrakeCLI.exe"""
I've also tried launching a shortcut (which just gave a null error and the script couldn't run) as well as trying to do a sendkeys to minimize it which didn't work at all.
This is from a VBScript running via cscript.exe by the way.
Does anyone know how I can start this app minimized within VBScript?
Check the docs and use the second parameter of the .Run method.
Evidence:
set s = createobject("WScript.Shell")
s.run "notepad", 2
starts Notepad minimized.

VBScript- Single line as administrator

Is it possible to use the shell.run command to run the specified program as an administrator?
for instance:
shell.run(cmd.exe) <---Run this as admin
shell.run(Notepad) <---Run this as normal
I know i can execute the script to run as admin but that means everything inside that script is executed as an administrator.
My other option was to seperate the scripts and run one as admin and include what needs to be ran as admin in that script, then call another script to run and run that one as normal.
Set oShell = CreateObject("Shell.Application")
oShell.ShellExecute "cmd.exe", , , "runas", 1
oShell.Run "nodepad.exe"
(Source: http://social.technet.microsoft.com/Forums/eu/ITCG/thread/310e57f9-2367-4293-9f97-53d0e077aacc)
(More Info: http://ss64.com/vb/shellexecute.html)
Windows (from XP through to Win7 at least) has a runas command which does what you need. See here for details.
So, instead of running cmd.exe, you would run runas.exe, giving cmd.exe as the program to run.
Try this:
Dim ObjShell
Set ObjShell = CreateObject ("WScript.Shell")
ObjShell.Run "runas /K (command goes here) "
ObjShell.Run "notepad.exe"

Running a VBS within a msi setup

I am trying to create a setup for an application that I'm developing using the Visual Studio 2010 setup.
One of the things I need to do is run some exe programs.
I am using a custome action to run a VBS.
This the method that im using to execute:
Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute """c:\prog.exe""","-parm bla" ,"","",""
The problem with this is that I cant wait for the program to finish using this method.
So I tried using this method:
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "c:\prog.exe -parm bla",1,True
But is seems that when the MSI runs the script is dosnt have the WScript object.
So my question is can i somehow get acess to the WScript object from the MSI or is there some better way to do this?
Indeed, Windows Installer does not support WScript objects directly. Have you tried to use the "CreateObject" function directly?
Set objShell = CreateObject("WScript.Shell")
Yes you cannot use WScript object in scripts that are called by MSI. As a workaround what you can do is create a new custom action with Action = NewAction, type =38, Source = (blank) TArget = add the vb script file as TARGET by running the following commands
CScript WiTextIn.vbs mymsi.msi CustomAction NewAction Target YourVBscript.vbs.
WiTextIn file is located in C:\Program Files\Microsoft SDKs\Windows\v7.0\Samples\sysmgmt\msi\scripts
(PS: When you try to run VBScript it might fail because vbscripts are disabled and you might have to delete the key from registry and enable vbscript)
This is what I did in my vbs script to open a firewall exception for the service. I couldn't use the standard interactive pop-up for a service (that asks for permission to open the firewall), since it doesn't have a UI.
set oShell = CreateObject("WScript.shell")
oShell.run "cmd /C netsh advfirewall firewall add rule program=""C:\Program Files (x86)\foo\bar\prog.exe"" name=""my-service"" dir=in action=allow"
I added this vbs script to the "Commit" CustomAction of the Setup&Deployment Project, leaving the properties as defaults.
To debug problems with the vbs stage, I ran the msi from DOS using
msiexec /i mysetup.msi /L* install.log
Note that I originally used "Wscript.CreateObject" but that failed. This worked.

Resources