how do runas in vbscript and parse parameter SW_HIDE - batch-file

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.

Related

Batch file login website

Each 1 hour the internet of our school need to be reconnected by entering the username and password. How we can add a batch file in task scheduler to automate the login
Thanks in advance
:start
ping -n 1000 127.0.0.1 > nul
REM call local html with the form info that will submit onload
goto start
Replacing 1000 with a higher number, as you cannot wait in a batch. This will send x amount of ping to localhost. Do some testing to get something close to an hour.
Now for the html file, You could save the login page as html,add value="myUsername" and value="myPassword" to the right input fields.
Adding/using the name of the form add <script>document.formName.submit();</script> to the very end of the document.
Now keep in mind that the website might validate the HTTP_REFERER, in which case this would not work...
You could also use a vbscript to type text in.
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "%windir%\notepad.exe"
WshShell.AppActivate "Notepad"
WshShell.SendKeys "Hello World"
I stumbled on this guide during my latest googling marathon and it remindeded me of your question ! therevisionist.org It is a vbs, not a batch. Hopefully it will fit the bill !
Option Explicit
Dim ie, ipf
Set ie = CreateObject("InternetExplorer.Application")
Sub WaitForLoad
Do While IE.Busy
WScript.Sleep 500
Loop
End Sub
ie.Left = 0
ie.Top = 0
ie.Toolbar = 0
ie.StatusBar = 0
ie.Height = 200
ie.Width = 1020
ie.Resizable = 0
ie.Navigate "https://www.exemple.com/"
Call WaitForLoad
ie.Visible = True
ie.Document.All.Item("email").Value="TestEmail#gmail.com"
ie.Document.All.Item("pass").Value="ThePassword"
ie.Document.All.Item("login_form").Submit
You could use my previous answer to call the .vbs periodically.

Is it possible to extend the functionality of the TAB based autocomplete?

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

VBScript and batch file fails when run by Task Scheduler

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.

Hide empty console window in a all GUI Powershell script?

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).

.VBS to run DNSCMD /enumrecords in a for loop on an array of node names. CLI shows as if I ran a DNSCMD /?

I'm trying to run DNSCMD.exe /enumrecords from a .vbs on our Windows Server 2003 DNS server and dump the results into a .csv file for each node name under a DNS Zone.
I have entered the Nodes I want to run the command against under the zone into an array and entered a list of filenames into another array.
The idea being to run a for loop to walk through each node name and file name 0-42 and run the command for each node name and outputting to each filename in sequence.
Option Explicit
Dim ncpArr, NODE, fnameArr, FILE, DNSCMD, objWSHShell, QComm
Set objWSHShell = WScript.CreateObject("WScript.Shell")
ncpArr = Array(42 item array of DNS Nodes)
fnameArr = Array(42 item array of filenames)
QComm = "DnsCmd DNSservername /enumrecords contoso.com " & ncpArr(NODE) & " /Additional> c:\DNSData\" & fnameArr(NODE) & ".csv"
For NODE = 0 to 42
objWSHShell.Run QComm,1,True
'objWshShell.Exec QComm
'wscript.echo "| " & ncpArr(NODE) & " | | " & fnameArr(NODE) & " |"
'wscript.echo DNSCMD
Next
The intent was to save myself some time but if anything I've tripled the time it would have taken to run these 42 commands while writing this script.
What I have figured out so far is:
When I run this command on the DNS server it outputs a CSV exactly like I want it to
DnsCmd dnsservername /enumrecords zone.name node.st.name /Additional /continue> c:\DNSData\state_city_net.csv
When I run the same command like this:
Set objWSHShell = WScript.CreateObject("WScript.Shell")
objWSHShell.run "dnscmd /enumrecords zone.name node.st.name /additional> c:\DNSData\state_city_net.csv"
The command line box pops up briefly showing the instructions for DNSCMD as if I ran a DNSCMD /?. I've only been able to get a look at it by running the loop above and hitting Pause/Break at just the right time. Not sure if it's possible to get the Command Line box to stay open to show any errors
One thing I noticed is normally when you run it with an incorrect syntax it will have some information about the error at the top. In this case it literally looks like the DNSCMD /? instructions with no error at the top.
I'm seriously wondering if it's just not possible to run DNSCMD /enumrecords using a .vbs. as you can see from the commented out parts I've tried a few things to show the syntax is correct. When I Echo the output of the loop I can enter the exact syntax into the command line on the DNS server and it works!
If ANYONE could just peek at what I have above and tell me why when I run my script it does that or if you could even just confirm that my scripting is sound and it's something with DNSCMD it would really help me sleep better.
Thanks
Drew
When you enter
DnsCmd dnsservername /enumrecords zone.name node.st.name /Additional /continue> c:\DNSData\state_city_net.csv
in a shell ('DOS box'), the redirection (>) is provided by this shell. Your
objWSHShell.run "dnscmd /enumrecords zone.name node.st.name /additional> c:\DNSData\state_city_net.csv"
just runs a process and can't do redirection. Change your line to
objWSHShell.run "%comspec% /c dnscmd /enumrecords zone.name node.st.name /additional> c:\DNSData\state_city_net.csv"
and think about the (missing) /continue. For (single) testing, you can change the /c (close when done) to /k (keep open). Probably re-reading about (using?) all three parameters of the .Run method and paying attention to the return value may be a good idea.

Resources