Is there any way to put delay between commands e.g we have 5 command i want that run first command and wait for 3 second to run new command, Then run next command and wait for 2 second, Then run 3rd command and wait for 5 second etc etc.
I found WScript.sleep or WScript.sleep()
vbscript:
WScript.sleep(3000) '3000 milliseconds are 3 seconds
powershell:
start-sleep -seconds 3
Related
I have a job that runs every 15 minutes and gets a datetime. The batch file contains the following PowerShell command to get the current datetime minus 15 minutes:
for /f "delims=" %%a in ('"powershell [DateTime]::Now.AddMinutes(-15).ToString('yyyy-MM-ddTHH:mm:ss')"') do set "dt=%%a"
I write the results of the PowerShell command in my logs. I've been able to notice that every 5 or so job runs, there is a small delay of anywhere between 1-7 seconds. For example, if the job is scheduled to run at 15:00:00, and there is a delay of 5 seconds, my logs will show 14:45:05 as the datetime returned by the PowerShell command.
What could be the reason for this delay? Perhaps the PowerShell command takes longer than expected sometimes? Or could it be that the OS is not running the task on time because it might be struggling to handle resources?
Please let me know if I should provide any more info - I'm having a hard time to be less general/open-ended with the question.
I had written a piece of code for the execution of a executable file that completes installation. The part of code where I am failing is that I had given time slots in seconds so that installation will finish within that duration and then take keystroke Enter to finish the installation.
But it is not reliable at all because the installation speed depends upon the processor speed of machine and I have to use this code in different machines, so if the installation is taking 20 sec and I had given 15 seconds then it will take Enter keystroke before the completion of installation, which will click on "Cancel" in dialog box, and the installation is terminated without completion.
Is there any way that PowerShell can detect new popup dialog box or it will check whether the process is finished or not and then execute rest of the program?
$InstallFile = "D:\File for installation\folder\file.exe"
Start-Process $InstallFile
$wshell = New-Object -ComObject WScript.Shell;
$wshell.AppActivate($InstallFile)
Sleep 1
$wshell.SendKeys('')
Sleep 1
$wshell.Sendkeys('')
$wshell.SendKeys(" ")
$wshell.SendKeys("{TAB}")
$wshell.SendKeys("{TAB}")
$wshell.SendKeys('')
Sleep -s 10
$wshell.SendKeys('')
Sleep -s 5
I have a c# program, integrated with a command line program. I want to run the command line program twice(start, finish, start again, finish again). Now I use a timer to set a special time period for every run, for example, give first run 10 seconds, no matter it is finished or not, after 10 seconds, the program starts the second run.
I want the second run can run automatically after the fist run finshed, How to do it? How to detect the first run is finished, and then take a trigger to start the second run?
Assume you run the command line as a process, see this answer to check if the process has finished:
Find out if a process finished running
if (process.WaitForExit(timeout))
{
// user exited
} else {
// timeout (perhaps process.Kill();)
}
In a command line you can launch this command:
start "" /w will execute the command and wait until it is finished before proceeding.
for %a in (1 2) do start "" /w "programA.exe"
I have a problem of understanding with the use of a background process.
I have to convert a logic of treatment which is on Linux towards Windows. I have a script which in its treatment, launche among others, another script in background, and pursues its treatment. The script which throws(launches) the treatment background does not have to care about the result of this one thus has to end even if the treatment in backgroud still execute.
What I experimented in Powershell is when my first script ends, it looks like the processing of the script thrown in background breaks off.
Here is an example of the tests I have made in my environment (PowerShell version 2.0)
Here's the simple code of my first script script_1.ps1:
start-job -filepath d:\script\script_2.ps1
sleep 3
get-item d:\log\log
And my second script script_2.ps1:
sleep 1
"test" > d:\log\log-1.log
sleep 1
"test" > d:\log\log-2.log
sleep 1
"test" > d:\log\log-3.log
sleep 1
"test" > d:\log\log-4.log
sleep 1
"test" > d:\log\log-5.log
Here's what's happen when I submit it:
C:\Documents and Settings\user1>Powershell -command "D:\script\script_1.ps1"
Id Name State HasMoreData Location Command
-- ---- ----- ----------- -------- -------
1 Job1 Running True localhost sleep 1...
LastWriteTime : 2013-10-10 10:19:13
Length : 14
Name : log-1.log
LastWriteTime : 2013-10-10 10:19:14
Length : 14
Name : log-2.log
The result show me that when the first script terminate, (after 3 sec.) the second script
stop also because we see that only the first 2 logs files were created.
I tought that it would be like on Linux (&) where the background job continue anyway.
Is it the way it suppose to be with PowerShell or is somethings I do wrong?
When you use the -Command switch to start PowerShell it runs the supplied command, and then exits the session. Jobs in PowerShell are specific to the session they're started in. You can see this for yourself if you want by starting two instances (sessions) of PowerShell. Create a job in one, e.g. start-Job {sleep 600}, and do get-Job, you'll see that a job is Running. If you type get-Job into the other PowerShell instance you'll see that there are no jobs.
The start-Job cmdlet is creating a job, but it doesn't wait for that job to complete, it simply starts it on its merry way. In your case what's happening is that start-Job is creating the job, you're waiting one second, then listing the contents of the log directory. Then you reach the end of the script and once that happens the session is terminated. This is why only some of your log files are created. The session is being ended because you've reached the end of the script, before the created job has had a chance to finish.
To use jobs in the manner you describe you need for the PowerShell session to remain started for the entirety of the job's lifetime. For that, you'll need the -NoExit parameter on your PowerShell command that runs the job. You may want to browse around powershell /? for other switches you might find useful. If you got extra creative with your script that starts the job you could eventually make a hidden, noninteractive session that automatically quit once the job was done. But baby steps.
On linux, the background job will keep on running only if launched with the nohup command, or the nohuponexit option is set in the shell (or the process is later disowned with the eponym command)
Hints on how to do the same with PS can be found in this thread of emails on freelist.
This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
Sleeping in a DOS batch file
How to wait in a batch script
I have a program that is kicked off with a batch file.
The first module takes 10 seconds or so to initialize, and I want a way to "sleep" for 15 seconds before the second module is called, but I don't want it to require the user to hit a key like "pause" seems to require.
So, this is what I mean:
echo %PATH%
pause 10
echo %PATH%
In this example, I want there to be 10 seconds in between the echos. Is this possible? I've seen some examples using "ping 1.1.1.1" but it doesn't seem to work all the time correctly.
ping -n 11 -w 1000 127.0.0.1 > nul
Update
Beginner's mistake. Ping doesn't wait 1000 ms before or after an request, but inbetween requests. So to wait 10 seconds, you'll have to do 11 pings to have 10 'gaps' of a second inbetween.
If choice is available, use this:
choice /C X /T 10 /D X > nul
where /T 10 is the number of seconds to delay.
Note the syntax can vary depending on your Windows version, so use CHOICE /? to be sure.