So I use task scheduler to run a specific file (c:\Newfile.exe) after a specific program has been started (c:\Program.exe).
I'd like to know how I can close "Newfile.exe" when "Program.exe" closes. I know this can't be done with task scheduler, does anyone know of a way to achieve this scenario outcome?
Kind regards
Tried using task scheduler
This program runs waiting for a program to exit, checks if it was Notepad.exe and if so terminates MyProgram.exe. Use an Exit Do after terminating your program to also quit the script.
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
Set objEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM Win32_ProcessStopTrace")
Do
Set objReceivedEvent = objEvents.NextEvent
If lcase(objReceivedEvent.ProcessName) = lcase("Notepad.exe") then
Msgbox "Process exited with exit code " & objReceivedEvent.ExitStatus
Set colItems = objWMIService.ExecQuery("Select * From Win32_Process where ProcessName=MyProgram.exe")
For Each itm in ColItems
itm.Terminate
Next
End If
Loop
Related
I have to create a vbscript with runas and hide the Dos windows.
The command in comment is working fine, but with the runas it's an another think.
this is my script :
' WshShell.Run "C:\Down\XP\Install_TV_Cmd_Line.bat", SW_HIDE,true
Set WshShell = WScript.CreateObject("WScript.Shell")
strcmd="""C:\Down\XP\Install_TV_Cmd_Line.bat"""
pass = "xxxxxx"
User = "xxxxxx\administrator"
Wshshell.run "runas.exe" & " /U:" & user & " " & strcmd
wscript.sleep(1000)
Wshshell.sendkeys pass & "{ENTER}"
How can i do for pass the SW_HIDE argument please ?
Thank you in advance
to run the command line in hide mode with vbscript
Wshshell.run "runas.exe" & " /U:" & user & " " & strcmd, 0, False
0 Hide the window (and activate another window.)
1 Activate and display the window. (restore size and position) when display a window for first time.
2 Activate & minimize.
3 Activate & maximize.
4 Restore. The active window remains active.
5 Activate & Restore.
6 Minimize & activate the next top-level window in the Z order.
7 Minimize. The active window remains active.
8 Display the window in its current state. The active window remains active.
9 Restore & Activate. Specify this flag when restoring a minimized window.
10 Sets the show-state based on the state of the program that started the application.
I've created a single file.bat that execute correctly two different vbs files:
cscript "\\server_1\dir\file_M_1.vbs" "\\server_1\dir\muc1.xlsm"
cscript "\\server_2\dir\file_H_2.vbs" "\\server_2\dir\muc2.xlsm"
the code of the two files.vbs is the same, because it was created to do the same thing in two different servers.
This is the contents of file_M_1.vbs, that simply runs the macro called "copy_M":
Dim args, objExcel
Set args = WScript.Arguments
Set objExcel = CreateObject("Excel.Application")
objExcel.Workbooks.Open args(0)
objExcel.Visible = False
objExcel.Run "copy_M"
objExcel.ActiveWorkbook.Save
objExcel.ActiveWorkbook.Close(0)
objExcel.Quit
Instead this is the code of file_H_2.vbs, that runs macro called "copy_H":
Dim args, objExcel
Set args = WScript.Arguments
Set objExcel = CreateObject("Excel.Application")
objExcel.Workbooks.Open args(0)
objExcel.Visible = False
objExcel.Run "copy_H"
objExcel.ActiveWorkbook.Save
objExcel.ActiveWorkbook.Close(0)
objExcel.Quit
I' d like to know if there' s the possibility to collect all three pieces of code in only one file.bat or file.exe
(to be even scheduled to run in a specific time with the task scheduler of windows7.)
I' ve solved the problem in this way. With the help of others we have created only one file.bat:
cscript "\\server1\dir\file.vbs"
it runs the file.vbs:
Dim objExcel
Set objExcel = CreateObject("Excel.Application")
objExcel.Workbooks.open("\\server1\dir\muc1.xlsm")
objExcel.Visible = False
objExcel.Run "copy_M"
objExcel.ActiveWorkbook.Save
objExcel.ActiveWorkbook.Close
objExcel.Workbooks.open("\\server2\dir\muc2.xlsm")
objExcel.Visible = False
objExcel.Run "copy_H"
objExcel.ActiveWorkbook.Save
objExcel.ActiveWorkbook.Close
objExcel.Quit
it runs perfectly. I 've put in the task scheduler of windows7 the file.bat and all it' s ok.
I don' t know if it' s possible to create only one .exe (not a .bat that runs .vbs)
When using the TAB key in the dos command prompt you can cycle through the names of files and folders in the current directory... (and it even seems to work with historical commands via DOSKEY as well). Does anyone know if it's possible to extend this somehow so that pressing TAB (or any other key combination) would autocomplete from a provided list of items as well as the previously mentioned sources? I think an example is in order....
My desired behavior is to add another source to the possible items that would appear when TAB is used. At my job we make heavy use of a scheduling product called AutoSys and administer it almost exclusively through command prompt. Basically I would love to find a way to cycle through job names so the prompt would autocomplete the names when we have the first part of the job name entered already...
Common command usage:
'autorep -J JOBNAME'
Example of what I'd like to do:
'autorep -J ABC_C_EXPORT_Re' [TAB]
where the TAB key press allows me to cycle through the jobs that start with 'ABC_C_EXPORT_Re' until I find the one I want.
It seems like a possible (but very poor) ïsolution would be to have one empty file created and named for each job in the environment... But this doesn't strike me as an effective solution to the problem, especially considering that at any one time we can have between fifty thousand and a hundred thousand jobs in our environment.
I apologize for posing this strange question in an even stranger way..... I hope I was at least able to convey a sense of the central question I'm asking. Something like this would be a huge help to our operations support staff who have to find jobs by command line all day long!
Thanks for having a look!
Scott
You can make your own command processor pretty easy.
Here's something from Filter.vbs. Unlike this you'd want to read characters rather than lines (so .read(1) rather than .readline). Echo out each character, do something special on tab, when user presses enter execute the command line you built in memory, capturing it's stdout using wshshell.exec.
Here's something from help
Do While Not WScript.StdIn.AtEndOfLine
Input = Input & WScript.StdIn.Read(1)
Loop
WScript.Echo Input
Here's a menu, not everything is included.
Set Arg = WScript.Arguments
set WshShell = createObject("Wscript.Shell")
Set Inp = WScript.Stdin
Set Outp = Wscript.Stdout
Showmenu
Sub ShowHelpMenu
outp.writeline " -----------------------------------------------------------------------------"
outp.writeblanklines(1)
outp.writeline " Menu"
outp.writeline " ----"
outp.writeblanklines(1)
outp.writeline " 1 Help 2 HTML Help 3 Version 4 History"
outp.writeblanklines(1)
outp.writeline " 5 Exit"
outp.writeblanklines(1)
outp.write "Filter>"
End Sub
'=============================================
Sub ShowMenu
Do
ShowHelpMenu
Answ=Inp.readline
If Answ = "1" Then
ShowGeneralHelp "TEXT"
Elseif Answ = "2" Then
ShowGeneralHelp "HTML"
Elseif Answ = "3" Then
Version
Elseif Answ = "4" Then
History
Elseif Answ = "5" Then
Exit Do
End If
Loop
End Sub
'=============================================
Sub History
On Error Resume Next
WshShell.Run """" & FilterPath & "FilterHistory.txt""" , 1, False
err.clear
End Sub
'=============================================
Sub Version
outp.writeblanklines(1)
outp.writeline " Version"
outp.writeline " -------"
outp.writeblanklines(1)
outp.writeline " Filter Ver 0.6 - 2015 (Public Domain)"
outp.writeblanklines(1)
outp.writeline " by David Candy"
outp.writeblanklines(1)
End Sub
I have a VBScript that checks to see if MS Word is running hidden, makes it visible, then hides it again.
here is the script code that works fine when I double click the file in Explorer:
dim oWord
Dim wshShell, btn
Set wshShell = WScript.CreateObject("WScript.Shell")
set oWord = getobject(, "Word.Application")
if isobject(oWord) then
on error goto 0
wshShell.Popup "Word is running, making visible", 7, "ALPS Push print", &H0 + &H40
oWord.visible=true
wshShell.Popup "MS Word is now visible" & vbcrlf & vbcrlf & "Waiting 30 seconds then hiding it", 30, "ALPS Push print", &H0 + &H30
oWord.visible=false
else
wshShell.Popup "Word is not running" & vbcrlf & vbcrlf & "Quitting", 7, "ALPS Push print", &H0 + &H40
end if
It works find when I run it, but when it runs under Task Scheduler it fails so I created a batch file to launch it
wscript C:\dev\checkALPS.vbs
Now when I try to run it from the Task Scheduler, it still fails with the below error message
---------------------------
Windows Script Host
---------------------------
Script: C:\dev\checkALPS.bat
Line: 7
Char: 1
Error: ActiveX component can't create object: 'getobject'
Code: 800A01AD
Source: Microsoft VBScript runtime error
What can I do to get this working?
I have had this similar issue, I bypassed it by utilizing cscript.exe application to activate the vbscript as a console application rather than a windows application. There is a possibility that there is a limitation on the domain or computer which does not allow windows applications to be executed via wscript. As an alternative, try activating the same script via "Cscript.exe" instead.
So the code would be:
cscript C:\dev\checkALPS.vbs
And the get object method is not activated off of the wscript executable. So you would need to activate it via wscript.
dim oWord
Dim wshShell, btn
Set wshShell = WScript.CreateObject("WScript.Shell")
set oWord = Wscript.GetObject(, "Word.Application")
if isobject(oWord) then
on error goto 0
wshShell.Popup "Word is running, making visible", 7, "ALPS Push print", &H0 + &H40
oWord.visible=true
wshShell.Popup "MS Word is now visible" & vbcrlf & vbcrlf & "Waiting 30 seconds then hiding it", 30, "ALPS Push print", &H0 + &H30
oWord.visible=false
else
wshShell.Popup "Word is not running" & vbcrlf & vbcrlf & "Quitting", 7, "ALPS Push print", &H0 + &H40
end if
Give that a swing and let me know how it works.
I have made a very simple Powershell script with WinForms GUI.
Everything works as intended but, when I run the .ps1 script with PowerShell a black empty console window appears at first and then the GUI shows.
Anyway to make the console window dissapear?
Best regards
I wrote a small article on this subject (sorry in french) one year ago.
Here is the common solution using a small VBS script to start PowerShell hidding his window (the trick is in the last ,0).
Set Args = Wscript.Arguments
'MsgBox "Chemin LDAP: " & Args(0)
'MsgBox "Classe: " & Args(1)
Set objShell = CreateObject("Wscript.Shell")
objShell.Run "c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -nologo -Noninteractive -file c:\SlxRH\RhModif.ps1 " & chr(34) & Args(0) & chr(34) , 0
I also embeded PowerShell in an executable with no console called slxPShell2.EXE.
I found the above didn't work for me. I used this:
Set objShell = CreateObject("WScript.Shell")
objShell.Run "CMD /C START /B " & objShell.ExpandEnvironmentStrings("%SystemRoot%") & "\System32\WindowsPowerShell\v1.0\powershell.exe -file " & "YourScript.ps1", 0, False
Set objShell = Nothing
Hope that helps.
This solution Minimizes Powershell window after it starts. Powershell window opens, then disapears, without using any outside code. Put at beginning of your script.
$t = '[DllImport("user32.dll")] public static extern bool ShowWindow(int handle, int state);'
add-type -name win -member $t -namespace native
[native.win]::ShowWindow(([System.Diagnostics.Process]::GetCurrentProcess() | Get-Process).MainWindowHandle, 0)
This is how I got this working:
Have the Winforms GUI script in one ScriptOne.ps1 file
Create another LaunchScriptOne.ps1 file with the content:
powershell.exe -WindowStyle Hidden -File "C:\path\to\ScriptOne.ps1".
The solution was provided in another thread on the same topic: Hide or Minimize the powershell prompt after Winform Launch
I hope someone will find a way to put this into one single script as well. The answers above in this thread did not help me, but maybe I did something wrong, idk.
I'm nube so no rep so can't comment inline... though wrt #Ipse's solution which I'm a fan of, I also make sure to close the hidden window when the script is done... not sure if PS gets around to this sort of auto-garbage collection, but suspect it's good best practice.
eg. at end of your script I'd suggest doing:
stop-process -Id $PID
(which should terminate that hidden window v. just leave it lurking around and tying up those resources).