Jenkins windows batch command starts before earlier windows batch command completed - windows

In Jenkins: I'm having a windows batch command which runs:
wmic /node:[server name] process call create "cmd.exe /c [bat file]".
The problem is that the coming step in the jenkins project (also windows batch command) runs before the batch tasks (restore DB etc.) complete.
I would like to know how can make the first windows batch command wait until completed, so that the second step won't run yet.

Related

Run PowerShell script on remote server as different user without any pre-requisites

I need to trigger the execution of a powershell script on remote server under different user. This script uses 'gpg.exe' so the execution has to be on the remote server with a path pointing to 'gpg.exe'.
I created a bat file on remote server with following commands:
REM RUNTEST.BAT
time /T
SET PowerShellDir=C:\Windows\System32\WindowsPowerShell\v1.0
CD /D "%PowerShellDir%"
echo started
start /wait powershell.exe "Start-Transcript -Path D:\logs\testlog.txt;import-module
'\\ServerName\FolderName\Script\AutomationScript.ps1';Stop-Transcript"
time /T
echo done
exit /b
Then invoked this bat file from PowerShell on another machine:
runas /user:DOMAIN\serviceuser "\\ServerName\FolderName\Script\Execute.bat"
These steps do execute the PowerShell script successfully and the output is also as expected. However, this requires 'gpg.exe' to be available on the path mentioned in the PowerShell script on the machine which will invoke the execution. Is there a way the script can look for pre-requisites (like gpg.exe, folder structure etc) only on the remote server, not the machine which invoked the bat file?
Not sure if OS makes any difference, the remote server has Windows Server 2019 and calling machine has Win 10. The remote server has the required folder structure, gpg.exe, bat file to execute the PowerShell script successfully. The calling machine will not have 'gpg.exe' & the folder structure.

Why do PS1 calls inside a PS1 Script run when executed in Powershell, but are not executed when run as a Windows Scheduled Task?

I am running a Powershell script (TaskScript.ps1) using Powershell v4.0 (from Get-Host cmdlet) w/ File Version: 6.3.9600.17415 (Windows Explorer File Properties.) I am trying to run this script from a regularly scheduled task on Windows Server 2012 R2; the TaskScript.ps1 does this:
& .\OtherScript1.ps1
#... log messages to LogFile ...
& .\OtherScript2.ps1
#... log more messages to LogFile ..
When invoked manually in Powershell, the entire TaskScript.ps1 -- including calls to OtherScript1.ps1 and OtherScript2.ps1 -- execute as expected and logging to LogFile is executed as desired; that is, after OtherScript.ps1 and OtherScript2.ps1 complete.
However when TaskScript.ps1 is run from a Windows Scheduled Task, the script is run and LogFile is updated immediately without invoking OtherScript1 or OtherScript2 executing.
I've checked that:
LogFile is clear before manually running the script and manually executing the Task
running the script manually executes all of the included scripts as
expected (each subscript has its own logging which has been checked)
LogFile is written to (at the appropriate times) when executed manually (I've watched the output as it happens)
The Scheduled Task is running and invoking TaskScript.ps1 (LogFile is updated whenever the task is run)
TaskScript.ps1 is not executing other scripts when run through Task (regardless of if Task is run manually or is scheduled) -- OtherScript1.ps1 and OtherScript2.ps1 have logging that is not triggered when the Task is run, but is triggered when TaskScript.ps1 is manually run from directly within Powershell. ie. powershell.exe .\TaskScript.ps1
LogFile is written to immediately when executed through the Task. No delay for the other two scripts; as happens when executing within Powershell.
I've tried a few methods for invoking the scripts within TaskCript.ps1 including the call operator syntax above, the Start-Process cmdlet and dot sourcing (which could work in this case, but is not preferred).
I've also tried temporarily adjusting the Execution Policy on the Scheduled Task using -ExecutionPolicy Bypass argument in my Task, thinking maybe the calls couldn't be invoked from within a script because of execution policy.
Some user here had an issue with Windows Task Scheduler not liking script file off of local drive. In case it's relevant, my TaskScript.ps1, OtherScript1.ps1 and OtherScript.ps2 scripts (and others) are located on a local VHD (virtual disk) at P:\PathTo\Scripts\TaskScript.ps1.
This is what my task looks like:
Program/script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Add arguments (optional): -NoProfile -NoLogo -NonInteractive -ExecutionPolicy Bypass -Command "& Path\To\TaskScript.ps1"
Why won't OtherScript1.ps1 and Otherscript2.ps1 execute if TaskScript.ps1 is run from a Windows Scheduled Task but will run just fine if invoked manually in Powershell?

Task Scheduler Action parameter should generate log, but doesn't

As the title suggests, I have an added parameter in my Task Scheduler Actions that logs stdout and stderr to a log.txt file. The logging works when the action is run through the command prompt, but not when the action is run by the actual Task Scheduler (at its specified time). Task scheduler reports the action runs successfully, but I can't be sure it does because there's no logging:)
Command looks like this
powershell.exe -file "D:\Scripts\TimeSync2.ps1" > "D:\Scripts\timeSync_log.txt" 2>&1
I'm unfortunately not a native Windows user, so any help would be appreciated. I'm running Windows Server 2008 R2 Enterprise.
Thanks!
Cmd.exe handles command redirection. You have to run it under cmd.exe. Powershell probably also can do redirection but in your script (.NET can).
A black window just means a console program is running. Only if cmd is running does cmd features become available. By starting cmd or by putting it in a batch you canget redirection from cmd.
cmd /c powershell.exe -file "D:\Scripts\TimeSync2.ps1" > "D:\Scripts\timeSync_log.txt" 2>&1
See for Help
cmd /?
Place the command you listed in a batch file and then schedule the batch file.
If you are doing so and it fails, then try it with your account credentials as authentication in task scheduler, to see if it is a permissions issue.
#echo off
powershell.exe -file "D:\Scripts\TimeSync2.ps1" > "D:\Scripts\timeSync_log.txt" 2>&1

Running a windows batch command using chef blocks the chef provision

I'm using chef for windows and need to run a batch file that starts up the selenium-server java service (java -jar seleniumserver.jar) as a daemon. When I try to use a windows_batch resource, it causes chef to hang during it's provisioning.
The problem is that the selenium server stays running in whatever command line you start it in, but chef won't continue provisioning the machine until the command is finished. The thing is, the command never finishes, it's not supposed to.
Here's what I've tried so far:
Executing java -jar seleniumserver.jar & (with the windows_batch resource)
Using a template to create a batch file for the command, then using windows_batch to execute file.bat
Using windows_batch to execute the batchfile with an & (file.bat &)
I'm open to any ideas, even if it's kind of hacky. Thanks in advance!
If I understand the question correctly, you can start a separate process so the main batch file ends. For example:
start java -jar seleniumserver.jar
You can control several execution parameters through the different start options.
Ending command lines with & does not do the same as *nix.
async process on windows that doesn't block chef run
batch "run" do
code 'powershell -c "start-process notepad.exe"'
end
The following is an example of using start-process to start a command with arguments.
Try running the command by itself in the command prompt to familiarize yourself with its output.
net statistics server
Now run it using powershell and start-process in the command prompt and verify the correct output shows in c:\output.txt. Please pay careful attention to the use of single or double quotes. My experience says start-process won't work with double quotes for some reason.
powershell -c start-process net -ArgumentList 'statistics workstation' -RedirectStandardOutput c:\output.txt
Now put the following in a Chef execute resource and run it. Again, be aware of the single quotes, double quotes and escaped single quotes.
execute "run" do
command 'powershell -c "start-process net -ArgumentList \'statistics workstation\' -RedirectStandardOutput c:\chef-output.txt"'
end
You should find the correct output in c:\chef-output.txt.

start external cmd prompt and update it concurrently with java application

I want to be able to run an external cmd prompt concurrently with my Java code. While running java application need to start external cmd prompt ,and this command prompt need to updated from my java application as well as i need to get values from this command prompt into java application.Please provide suitable suggestion
Try this
Runtime rt = Runtime.getRuntime();
rt.exec("cmd.exe /c start command");
UPDATION 2
To execute commands try this
rt.exec("cmd.exe /c start cmd.exe /k \"ping localhost\"");

Resources