Executing .bat files from Powershell via Ansible - windows

I am using Ansible to execute a Powershell script on several Windows hosts. In the Powershell script, I am using Start-Process -FilePath "cmd.exe" -ArgumentList "C:\Path\to\some\bat\file.bat"
The bat files are several programs that run indefinitely.
The issue I'm having is that when I run the Powershell script manually, everything works. The bat files are executed and done. When I execute the file via Ansible, the bat files are not executed. No errors reported that I could find.
Any idea how I can get these two bat files up and running via Ansible?
Thanks.

use win_command
- name: Simple batch script
win_command: "C:\Path\to\some\bat\file.bat"
You can use win_shell too.

Related

Powershell script running from usb stick - privileges and arguments

I'm making Powershell script that can configure pc's. I'm working with a lot of computers so script is placed on my usb stick, and I'm launching it from it. First I created 2 separated scripts, one when stick was assigned as D:\ and another when it was E:\ but it was very frustrating to make changes in both scripts, so I changed it and make it universal for every disk letter. Everything worked fine except commands were script was using built in Windows programs like netsh or cmd. Adding full path solved the problem, but Script is launching also another script when there isn't specific task in Task Register. I cannot change this second script to add there full path to programs
I've tried:
1. changing execution policy of launched script
2. changing path to script
3. changing path to powershell (full path)
bat file that launch main script:
#echo off
SET SCRIPT=path-on-usb-stick\script.ps1
SET PATH="%~dp0%SCRIPT%"
#echo on
%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\powershell.exe -executionpolicy bypass -command "& %path%"
Part of the main script which cause problems:
if(!(Get-Scheduledtask | Where-Object{$_.TaskName -eq ".check-vpn-new"})) {
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command "& 'C:\...\script2.ps1' -Verb RunAs -windowstyle hidden -Wait"
}
I'm getting few errors in my output:
1. Arguments in script are null or empty but they are not
2. Script don't have access to files with administrator privileges (even if it's launched with Admin privileges)
3. script don't see Windows.old folder which should be cleaned
(everything works fine when script2 is launched from bat file)

Execute Batch Script not working in Jenkins Pipeline Job

I have created a powershell script which will transfer(using winscp.dll) the files from Jenkins windows server to Linux server. In Jenkins batch command, I have executed that powershell script and it works fine.
But when i tried the same in Jenkins pipeline job, it calls the powershell script and comes to the next step. Its not waiting for powershell script response.
bat 'powershell.exe -ExecutionPolicy Bypass "D:\\Test\\Deploymentscripts\\PowerShellScript\\FileTransfer.ps1 $env:EndMarket $env:Environment"'
I have tried with another powershell script which will connect to Linux server and execute some commands. It works fine in pipeline job
Kindly guide me to fix this issue.
Interesting. Your problem doesn't seem to be in your script because you already explained that it works in a batch job.
I don't know how your pipeline is written, but I would suggest taking a look to Stage, Lock and Milestone which is probably what you need.
The stage step is a primary building block in Pipeline, dividing the
steps of a Pipeline into explicit units and helping to visualize the
progress using the "Stage View" plugin
I guess you could add a stage block like this one in your pipeline:
stage("Previous Step") {
// Some previous step
}
stage("Wait for Script Execution") {
// Call your script
bat 'powershell.exe -ExecutionPolicy Bypass "D:\\Test\\Deploymentscripts\\PowerShellScript\\FileTransfer.ps1 $env:EndMarket $env:Environment"'
}
stage("Next Step") {
// Script already finished its execution
}
But without your pipeline info is just a guessing. Also to improve your script compatibility avoiding "bat and ExcutionPolicy" and using the PowerShell plugin, with that plugin you could simplify your code like this:
powershell -File your_script.ps1
EDIT: I forgot to mention that you can try a different alternative to powershell and the winscp lib using "scp" direct compatibility between Windows and Linux, I'm talking about Cygwin.
With Cygwin installed (with scp) you can use scp as it was a Linux box and a bash script instead powershell:
D:/cygwin-64/bin/run.exe /usr/bin/bash -lic \"/home/user/file.sh\"
In this case I'm running a script silently through Cygwin within a Jenkins Project with a "Run Windows Batch" option. In the script you can add shell commands and the scp instructions you want.
It may seems a little bit more complex but adds more flexibility to perform Windows - Linux tasks.
I've detailed more examples in my blog, you may find it useful.
As avvi mentioned you should check if that variables are being loaded.
You are not calling the script correctly. You are passing in $Env in the powershell invocation which is a powershell variable.
You are in batch mode so should be passing in %.
bat 'powershell.exe -ExecutionPolicy Bypass "D:\\Test\\Deploymentscripts\\PowerShellScript\\FileTransfer.ps1" "%EndMarket%" "%Environment%"'

I have a bat file that needs to run in powershell, via the start-process command is slow

I have a powershell script that runs a bat file, it has to run in powershell as it is being called by an application that can only run powershell (not bat files)
I use the Start-Process command
start-process -FilePath D:\versioned\pf_trunk\deployment\environments\development\aws\deployment\awx\development\files\PublishMYPlanetFootprint.bat -Wait
The batch file runs some visual studio builds and then moves some files.
If I run the batch file on its own, it runs and completes and closes the command window.
When I run it in powershell, it runs, completes and closes the command window, however the script continues to run for another 17 minutes before timing out.
I thought the wait would only wait for the bat to finish and then terminate.
Can somebody help with my powershell. What am I missing to ensure the script terminates on completion of the bat file operation.
Thanks

Copy-Item command working in Powershell Command line but not in bat file

Trying to copy files from one folder to another using Copy-Item command. The following command is working in powershell command line but throwing error when run inside a *.bat file:
Copy-Item C:\script\* D:\
It throws the following error:
'Copy-Item' is not recognized as an internal or external command,
operable program or batch file.
You can't run PowerShell cmdlets in batch directly, instead you have to invoke powershell.exe and pass the command to it:
powershell -Command "Copy-Item C:\script* D:\"
Or, you need to make not a *.bat file but a *.ps1 file. These are run in Powershell, and therefore accept any Powershell commands, so Copy-Item will work without extra efforts.

Task Scheduler fails to run batch file despite reporting task completed

I have a simple batch file which calls a powershell script.
I've looked at the following 3 previous questions on the subject as well:
Run a batch file from Task Scheduler is not working with a java command
Windows Task Scheduler doesn't start batch file task
Task Scheduler not executing batch (bat) file with MSTest commands
It seems like I've tried every single combination of running the task and it still doesn't execute my powershell script.
batch file contents:
powershell.exe "E:\SIS\fileCopy.ps1"
If I run the command in the batch file manually, it runs just fine. Here are things I've tried to do to get it working:
I've change the Security options to run as System with "Run with highest priveleges" checked
I've tried every other combination of "Run only when user is logged on", "Run whether user is logged on or not" and "Run with highest priveleges"
I've tried adding the "Start in (optional" setting to the folder where the files are located.
I'm at my wits end and can't believe Microsoft hasn't figured out a way to make this easier.
You need to have task scheduler execute Powershell.exe and have the arguments be the path to your .ps1 file.
To validate your script is running properly, you should set the Security options to 'Run only when user is logged on'. It will then pop the powershell dialog when it runs. I often also use start-transcript to view the results of scheduled poweshell scripts.
After you validate the script is running correctly, you can set the security options however best fits your situation.
Use the execution policy flag to flag that instance to unrestrisicted because your powershell settings may be blocking script execution.
powershell -executionpolicy unrestricted -Command "E:\SIS\fileCopy.ps1"
I found that Task Scheduler can't run a batch file if it lives in a folder that is being synced by OneDrive. I had to move the batch file to another folder to get Task Scheduler to be able to run it.

Resources