I'm trying to read a user's PKI card data with Certutil and dump the data to a text file. The idea is to put this as part of a login script to gather some data on user's with expiring certificates.
Here is my code section:
set oShell = WScript.CreateObject("WScript.Shell")
strcommand = "cmd /c certutil -scinfo -silent > " & StrPath
oShell.Run strcommand, true
it seems to work, dumping the cert data to a text file (strpath variable), but once I add more lines to the script it never waits for the command window to finish. It just closes in a fraction of a second or so. I know it takes aobut 7 seconds to read the PKI Card. I've tried sleep as well as a do/while loop and nothing seems to allow the command window run its course. I've also tried the various intwindowstyle options listed here.
Appreciate any help.
oShell.Run strcommand, ,true should work. The second argument is the intWindowStyle argument, not bWaitOnReturn. You could also use
oShell.Run strcommand, bWaitOnReturn := true
If this doesn't work, you could try to use the Exec Method . The documentation that this link leads to has a nice example of using a loop with sleep that runs until the process finishes.
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.
Alright so I have been searching for the answer to this, and although I found similar examples to this issue, they did not seem to work for me. I am trying to have a task scheduled to kill skype when a user logs in so that people don't have to close out every time they log on. I have to do this by writing code rather than manually because it will be a script run to set up new computers. here is my code that writes the task:
please note that the commented lines are from solutions I have tried but failed to work the way I wanted it to
Option Explicit
Dim wShell, outFile, objFSO, ret
Set wShell = CreateObject ("Wscript.Shell")
wShell.Run "N:\Internfolder\INCA_Scripts\SkypeKill.vbs"
'wShell.Run "cmd start ""N:\Internfolder\INCA_Scripts\Schedule_SkypeKill.bat"""
'wShell.Run "N:\Internfolder\INCA_Scripts\Schedule_SkypeKill.bat"
wShell.Run "SCHTASKS /create /tn ""Schedule_Kill_Skype"" /tr ""N:\Internfolder\INCA_Scripts\SkypeKill.vbs"" /sc onlogon", 0
'set wShell = Nothing
'WScript.sleep(15000)
WScript.echo "completed"
and here is the .vbs file for SkypeKill:
Dim oShell : Set oShell = CreateObject("WScript.Shell")
dim timer
timer = 0
Do
'attempt to kill skype (lync.exe)
r = oShell.Run("taskkill /F /IM lync.exe",0, True)
'if r = 0, then skyp was opened, then killed
if r= 0 then
WScript.echo "Skype was opened, but has been termiated. Hit 'Enter' to exit."
WScript.quit
'else, we wait until it is opened for 35 seconds, and kill it if it appears
else
WScript.sleep(3000)
timer = timer + 3
if timer = 35 Then Exit Do
end if
Loop
WScript.echo "Skype kill has been attempted"
the last bit of important information is that when I schedule the task to run this script, it says that the system cannot find the file specified, however the SkypeKill.vbs file is in the specified location (N:\Internfolder\INCA_Scripts\SkypeKill.vbs), which is a bit odd.
So with this I have a couple questions. I am assuming that there is something wrong with how I am attempting to schedule the task, given that it will not show up in the task scheduler when i run the first block of code. but, neither code throws any errors. How can I get this to actually write into the task scheduler and kill skype once the user has logged in and started up? Does the fact that I am running on Windows 7 and Windows 10 matter when trying to complete this task? or is there an easier way to do this via code that I am overlooking. Please help soon! Thanks!
Thank you, However I have resolved the issue simply by including the following code before the shell functions are executed:
If WScript.Arguments.Count = 0 Then
Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "wscript.exe", "c:\Users\admin\Documents\selfConfigure.vbs -1", "", runas", 1
End If
this elevates my priveleges to access the drive/file paths I need to access, as I was receiving an "access denied" error in command prompt. the issue has been resolved and the task has been written to the scheduler.
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
I am working on a VBScript where it activates an application, sends a password to that application and minimizes the application. My script fails sending the keys. The application does not get the password, but when I double-click on the script it receives the password and minimizes. I don't know where the error is.
It's called like this:
InitliszeUSb.bat:
pause
START %myDrive%"RunSanDiskSecureAccess_Win.exe"
pushd %~dp0
ping 10.10.10.10 -n 1 -w 10000 >nul
start /b "" cscript "D:\min.vbs"
Min.vbs:
Option Explicit
Dim oSHL : Set oSHL = CreateObject("WScript.Shell")
oSHL.AppActivate "SanDisk SecureAccess"
oSHL.SendKeys "pass1_word~" 'Enters Password
WScript.Sleep(3000)
oSHL.SendKeys "% n" 'Minimises the window
WScript.Quit
In Windows 7, I experienced issues when using SendKeys to send keys to other apps (the way like you do), when SendKeys is not running in elevated process. Sometimes the macro/script works 100%, sometimes it looks like keystrokes are getting lost. The only way around I found for that was running sender application (or script) elevated (i.e. "As Administrator").
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