We currently use Windows Batch (DOS) command files to control our process flow. To display messages to the Console, we would use the ECHO command. These messages would show up in our Scheduler software, which used to be Tivoli and now is CA WA Workstation\ ESP.
I would like to start using VBS files instead of CMD\BAT files and am trying to figure out how to do the equivalent of an ECHO to the console.
When I try to use either the WScript.Echo command or write to Standard Out, the messages are displayed in dialog boxes for both and they require the OK button to be pushed to continue. Not surprisingly, when I run unattended though a scheduler, the job hits one of these commands and just hangs since there is no one to OK the messagebox.
SET FS = CreateObject("Scripting.FileSystemObject")
SET StdOut = FS.GetStandardStream(1)
StdOut.Write("Test 1")
WScript.echo("Test 2")
I realize I could write the messages to a Log file using the Scripting object, but this could fail if an invalid path is provided or because of insufficient permissions. Besides, being able to see feedback write within the Scheduler is awfully convenient.
How do I write to the Console using VBScript? I’ve seen other posts here that suggest that the above methods which didn't work for the reason describe above were the way to do it.
wscript.echo is the correct command - but to output to console rather than dialogue you need to run the script with cscript instead of wscript.
You can resolve this by
running your script from command line like so:
cscript myscript.vbs
changing the default file association (or creating a new file extension and association for those scripts you want to run with cscript).
change the engine via the script host option (i.e. as per http://support.microsoft.com/kb/245254)
cscript //h:cscript //s
Or you can add a few lines to the start of your script to force it to switch "engine" from wscript to cscript - see http://www.robvanderwoude.com/vbstech_engine_force.php (copied below):
RunMeAsCScript
'do whatever you want; anything after the above line you can gaurentee you'll be in cscript
Sub RunMeAsCScript()
Dim strArgs, strCmd, strEngine, i, objDebug, wshShell
Set wshShell = CreateObject( "WScript.Shell" )
strEngine = UCase( Right( WScript.FullName, 12 ) )
If strEngine <> "\CSCRIPT.EXE" Then
' Recreate the list of command line arguments
strArgs = ""
If WScript.Arguments.Count > 0 Then
For i = 0 To WScript.Arguments.Count
strArgs = strArgs & " " & QuoteIt(WScript.Arguments(i))
Next
End If
' Create the complete command line to rerun this script in CSCRIPT
strCmd = "CSCRIPT.EXE //NoLogo """ & WScript.ScriptFullName & """" & strArgs
' Rerun the script in CSCRIPT
Set objDebug = wshShell.Exec( strCmd )
' Wait until the script exits
Do While objDebug.Status = 0
WScript.Sleep 100
Loop
' Exit with CSCRIPT's return code
WScript.Quit objDebug.ExitCode
End If
End Sub
'per Tomasz Gandor's comment, this will ensure parameters in quotes are covered:
function QuoteIt(strTemp)
if instr(strTemp," ") then
strTemp = """" & replace(strTemp,"""","""""") & """"
end if
QuoteIt = strTemp
end function
Related
I'm trying to make a vb script that will restart another vb script if it crashes.
I have searched, and searched but all I get is how to restart a program and since a vb script is a background process it doesn't work when you search in Win32_Process.
Here is my code
set Service = GetObject ("winmgmts:")
set Shell = WScript.CreateObject("WScript.Shell")
sEXEName = "Test_To_Block.vbs"
while true
bRunning = false
for each Process in Service.InstancesOf ("Win32_Process")
if Process.Name = sEXEName then
bRunning=true
msgbox("I am active")
End If
next
if bRunning=False then
msgbox("I am not active.")
Shell.Run sEXEName
end if
WScript.Sleep(100)
wend
The problem is that it never see's the file running and just opens hundreds of "Test_To_Stop.vbs"'s which resolves in me having to restart the computer.
In my opinion what should be changed is where the code is looking for.
for each Process in Service.InstancesOf ("Win32_Process")
Instead of looking in "Win32_Process" you need to look in wherever background process' run.
I am new to coding so sorry if this is a simple question.
Thank you in advance.
Regards,
A Viper
The below code restarts itself via WshShell.Exec() method and trace state of the running script via .Status property of returned object:
If Not WScript.Arguments.Named.Exists("task") Then
Do
With CreateObject("WScript.Shell").Exec("""" & WScript.FullName & """ """ & WScript.ScriptFullName & """ ""/task""")
Do While .Status = 0
WScript.Sleep 1
Loop
End With
Loop
End If
MsgBox "This script will be restarted immediately after termination"
Another way is to use .Run() method with third parameter set to True to wait until launched process terminated:
If Not WScript.Arguments.Named.Exists("task") Then
Do
CreateObject("WScript.Shell").Run """" & WScript.FullName & """ """ & WScript.ScriptFullName & """ ""/task""", 1, True
Loop
End If
MsgBox "This script will be restarted immediately after termination"
Or even simplier:
If Not WScript.Arguments.Named.Exists("task") Then
Do
CreateObject("WScript.Shell").Run """" & WScript.ScriptFullName & """ ""/task""", 1, True
Loop
End If
MsgBox "This script will be restarted immediately after termination"
Probably because the name of the running process is 'wscript.exe' and not 'Test_To_Block.vbs'. You may be able to use the hack mentioned on this page to change the name of the process:
If you're running the scripts locally and running some regular scripts, one
common hack is just to copy and rename wscript.exe to a particular name,
such as "MyScript1.exe". Then run the script with a shortcut as
"...\MyScript1.exe MyScript1.vbs". Then the process will show up as
MyScript1.exe.
Then you can use sEXEName = "MyScript1.exe"
Note: instead of using Shell.run sExeName use Shell.run "Test_To_Block.vbs"
I created a hyperlink in a pdf, this hyperlink is tied to vbs script, until this step all ok. When I run the script manually (double click), the script does that I want (open telnet connection). The problem is when I run the script through the hyperlink, CMD show the message " "telnet" is not recognized as an internal or external command". Please, Can anyone tell me why occurs that??
This is the script:
Dim WshShell, regexp
set regular = New RegExp
direccion = inputbox("Ingresa ip del equipo:")
' Set pattern.
regular.Pattern = "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
' Set case insensitivity.
regular.IgnoreCase = True
' Set global applicability.
regular.Global = True
if regular.test(direccion) = TRUE then
set WshShell=CreateObject("WScript.Shell")
WshShell.run "cmd.exe"
WScript.Sleep 1000
'Send commands to the window as needed - IP and commands need to be customized
'Step 1 - Telnet to remote IP'
WshShell.SendKeys "telnet " & direccion
WshShell.SendKeys ("{Enter}")
WScript.Sleep 1000
else
msgbox "Ingresa una ip válida"
end if
probably working directory not set correctly.
try
WshShell.SendKeys "c:\windows\system32\telnet.exe " & direccion
p.s: you're doing it wrong. "sendkeys" is somehow understandable when telnet terminal is already running, but there is no reason to be manually sending keys to the standard command prompt.
Telnet isn't in my Windows 10 and 8. But anyway if you have "telnet" command couldn't you do:
WshShell.run "cmd.exe /k telnet"
That should work and saves space.
I have been trying to get this script to work all day!
Here are some facts about my situation...
I have a program named "ffmpeg.exe" in my "C:\Windows\System32\" folder.
I DO NOT have that program in my "C:\Windows\SysWOW64\" folder.
Currently this is the script I have...
Option Explicit
Dim oFSO, oShell, sCommand
Dim sFilePath, sTempFilePath
Set oFSO = CreateObject("Scripting.FileSystemObject")
sFilePath = "C:\test\in_video.mkv"
sTempFilePath = "C:\test\out_video.mp4"
sCommand = "%comspec% /k ffmpeg -n -i """ + sFilePath + """ -c:v copy -c:a copy """ + sTempFilePath + """"
WScript.Echo sCommand
Set oShell = WScript.CreateObject("WScript.Shell")
oShell.Run sCommand, 1, True
Set oShell = Nothing
Set oFSO = Nothing
If I run this script manually at a command prompt then it seems to work just fine. But if I let another app run it (for example in this case uTorrent), it runs the script as expected but when it tries to process the oShell.Run command it runs that in a 32bit environment! Then I get this...
If I try to open up a new command prompt (nothing special) i seems to default to a 64bit environment and then I can type "ffmpeg" and it shows me the help content as expected.
So for some reason I can't get the script to run applications (specifically CMD) in the 64bit environment. Anyone know how I can achieve this?
Update
Seems that my script is in fact being ran in 32bit mode! Even though the script title bar says "C:\Windows\System32\cscript.exe", which is a 64bit environment!!
I used the following script to determine that it was running in a 32bit environment...
Dim WshShell
Dim WshProcEnv
Dim system_architecture
Dim process_architecture
Set WshShell = CreateObject("WScript.Shell")
Set WshProcEnv = WshShell.Environment("Process")
process_architecture= WshProcEnv("PROCESSOR_ARCHITECTURE")
If process_architecture = "x86" Then
system_architecture= WshProcEnv("PROCESSOR_ARCHITEW6432")
If system_architecture = "" Then
system_architecture = "x86"
End if
Else
system_architecture = process_architecture
End If
WScript.Echo "Running as a " & process_architecture & " process on a " _
& system_architecture & " system."
If it is only for cmd or some file in System32 you can use sysnative as suggested by the comment. It will lead to the 64Bit System32 even from 32Bit executables. Just replace "system32" with "sysnative" for every use. (unfortunately this is not present on 32bit windows so you need to check if you use a script on systems with both architectures... )
If you have a lot of accesses or use com objects I found it easier however to just use the same method to restart your script. The following code:
If fso.FileExists("C:\Windows\SysWOW64\wscript.exe") Then ' very basic check for 64bit Windows, you can replace it with a more complicated wmi check if you find it not reliable enough
If InStr(1, WScript.FullName, "SysWOW64", vbTextCompare) <> 0 Then ' = case insensitive check
newFullName = Replace(WScript.FullName, "SysWOW64", "Sysnative", 1, -1, vbTextCompare) ' System32 is replaced by Sysnative to deactivate WoW64, cscript or wscript stay the same
newArguments = "" ' in case of command line arguments they are passed on
For Each arg In WScript.Arguments
newArguments = newArguments & arg & " "
Next
wso.Run newFullName & " """ & WScript.ScriptFullName & """ " & newArguments, , False
WScript.Quit '32 Bit Scripting Host is closed
End If
End If
Basically closes the script whenever it was called with 32Bit Scripting host and restarts it with the 64Bit one so everything is found where it is expected to be.
First of all, thanks for reading.
I have a HTA to centralize some repetitive task.
Login into several servers via ssh and send multiple commands is one of them.
This code is working like a charm inside a vbs file
Option Explicit
Dim Shell, WMI, wql, process
Set Shell = CreateObject("WScript.Shell")
Set WMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
wql = "SELECT ProcessId FROM Win32_Process WHERE Name = 'putty.exe'"
dim cmd : cmd=InputBox("Enter command")
For Each process In WMI.ExecQuery(wql)
Shell.AppActivate process.ProcessId
Shell.SendKeys cmd & " {ENTER}"
Next
But this equivalent, inside a HTA only sends the command to one or two windows.
sub sendToPuttyWindow(cmd)
Dim Shell, WMI, wql, process
Set Shell = CreateObject("WScript.Shell")
Set WMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
wql = "SELECT ProcessId FROM Win32_Process WHERE Name = 'putty.exe'"
For Each process In WMI.ExecQuery(wql)
Shell.AppActivate process.ProcessId
Shell.SendKeys cmd & " {ENTER}"
Next
end sub
Currently I'm calling the vbs file from the hta, but I would like to maintain the HTA file as independent as possible.
Could you please help me?
Do not use SendKeys for automating PuTTY. It sends all emulated keystrokes to the current foreground window, whatever that may be. If you need to run several commands in a row: use plink from the PuTTY suite. It was built for this exact purpose.
plink -ssh -batch -m file user#host
currently, to improve some inefficiencies on a daily process, I am trying to write a c# Winform app that will combine a mix of user-input with VBscripts that will expedite a previously all user-input process of looking at an excel file and moving files from VSS to certain folders of certain servers.
I was hoping to get some questions answered, pointed in the right way:
Using command line or other workaround instead of manually,
1) Is it possible to log into a 2003 remote desktop with a Smartcard/pin?
2) Is it possible to run a file/start a process on the remote desktop from a command on your machine?
Thanks for the help and time
I have only experience with the second question.
You can do this with remote scripting or with utilities like SysInternals PsExec http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx
here a vbscript that remotely starts a ipconfig command and redirects it to a textfile
Note that you can't start interactive processes like this, they would start but not show up
Dim sComputer 'computer name
Dim sCmdLine 'command line of the process
Dim sCurDir 'working directory of the process
Dim oProcess 'object representing the Win32_Process class
Dim oMethod 'object representing the Create method
sComputer = "." 'this is the local computer, use a pcname or ip-adress to do it remote
sCmdLine = "cmd /c ipconfig.exe > c:\ipconfig.txt"
Set oProcess = GetObject("winmgmts://" & sComputer & "/root/cimv2:Win32_Process")
Set oMethod = oProcess.Methods_("Create")
Set oInPar = oMethod.inParameters.SpawnInstance_()
oInPar.CommandLine = sCmdLine
oInPar.CurrentDirectory = sCurDir
Set oOutPar = oProcess.ExecMethod_("Create", oInPar)
If oOutPar.ReturnValue = 0 Then
WScript.Echo "Create process method completed successfully"
WScript.Echo "New Process ID is " & oOutPar.ProcessId
Else
WScript.Echo "Create process method failed"
End If