I am using Git on Windows and trying to include unit testing in the workflow. If the commit message contain a keyword. The commit-msg hook will trigger Powershell command to run some Nunit tests.
This is my ruby code in the hook
#!/usr/bin/env ruby
message_file = ARGV[0]
message = File.read(message_file)
puts "The commit message is " + message
$regex = /(#runtest)/
if $regex.match(message)
exec 'powershell.exe -ExecutionPolicy RemoteSigned -Command {RunNunitTestCase}'
end
However when I commit a changes, the result is like below. The exec line was run but do nothing.
PS D:\testfolder> git commit -am '#runtest'
The commit message is #runtest
[authorupdate b14878d] 123
1 files changed, 1 insertions(+), 0 deletions(-)
I am new to ruby and powershell. Feel free to comment if this workflow is feasible or you have a better approach.
Thank you.
The curly bracket needs to be escaped.
exec "powershell.exe -ExecutionPolicy RemoteSigned -Command & \{RunNunitTestCase\}
I tried the exact script with the exec line being:
exec 'powershell.exe -ExecutionPolicy RemoteSigned -Command gps'
and it did print out the output of gps when I did git commit -m "#runtest"
So it does run the command and it does work.
Make sure that whatever you are executing does work. Grab the powershell line and try it out on commandline directly.
Related
I have created a Powershell script that I call from a batch file, and everything works fine when I call the batch file. The problem I am running into is I need to set the batch file to run in Task Scheduler. It starts fine, but it keeps hanging up because the task scheduler never says "The operation completed successfully" (0x0). Instead, it stays at "The task is currently running" (0x41301). Please advise, and I understand this is not the most ideal way to call a Powershell Script but for our environment and limited knowledge of scripting it works the best for us.
You should just use Task Scheduler to run PowerShell.
Create a new task, go to Actions tab, then choose New..., and inside this new window, you can run any program, like you run something from cmd.
Inside Program/script square, you simply put Powershell.exe, and inside Add arguments (optional) powershell arguments. This will work the same, as you would type in normal command line:
powershell <arguments>
So if you want to run script, that is saved in your disk, simply put this in arguments list:
C:\LocalisationOfScript\script.ps1 "argument 1" argument2
If you want more options, just add common parameters before this:
-windowstyle hidden -executionpolicy bypass C:\LocalisationOfScript\script.ps1 "argument 1" argument2
Of even this:
-windowstyle hidden -executionpolicy bypass if (Test-path C:\script\script.ps1) { C:\script\script.ps1 "argument 1" argument2 } else { return -1 }
And finally:
Start-Process Powershell.exe -argumentslist "-a -b -c copy" -windowstyle hidden -wait -erroraction stop
You can even add try, catch to last example.
Thank you for all the comments, i researched you advice and came across the exit command i forgot to add to the end of my script, so when i call my script it left the session to exchange open after i applied the exit command to the end of the script the program has been running without error (knock on wood) sense the fix and after i closed and reopened Task Scheduler the last run message changed to (0x0)
I have a batch file structured like below:
cd "C:\my\scripts\directory
powershell -f myPowershellSCript.ps1
exit %errorlevel%
This batch file is being sent through an in house remote shell application (which is mostly a black box to me) in a non-interactive way to another machine to be run. I can execute the application and watch it's output on the terminal locally. The script is completing the powershell script and then just dropping back to the remote shell on the test machine without ever running the last line in the batch file. I see the cmd.exe shell drop back to a prompt at C:\my\scripts\directory on the remote machine and just wait. Because it's non-interactive the script never completes.
I'd like to tag that last exit line onto the end of the line that calls powershell, but everything I've tried (below) has not worked. I fear that powershell is taking everything as input instead of batch interpreting them as two separate commands.
powershell -f SecurePaymentsTestLauncher.ps1 && exit 1
powershell -nonInteractive -f SecurePaymentsTestLauncher.ps1 && exit 1
powershell -nonInteractive -command "& 'SecurePaymentsTestLauncher.ps1'" && exit 1
powershell -nonInteractive -command "& 'SecurePaymentsTestLauncher.ps1'" ; exit 1
powershell -nonInteractive -f SecurePaymentsTestLauncher.ps1 ; exit 1
still produces the same result. No return from the remote execution.
How do I append a second command to a batch file line when the first command is a call to powershell?
Is your PowerShell process exiting status 0? The && conditional operator only executes the command it precedes when the command it follows exits 0. If you want to exit 1 regardless of whether PowerShell exits zero or non-zero, use a single &.
In your powershell command, you might also need to call the .ps1 script name as .\SecurePaymentsTestLauncher.ps1 and add the -ExecutionPolicy RemoteSigned arguments.
Try
cd "C:\my\scripts\directory"
start powershell -f myPowershellSCript.ps1
exit %errorlevel%
Using 'start' in front of powershell instantiates a separate powershell host outside of the batch script host, which will allow your ps1 file to do its thing while your batch script goes straight to 'exit %errorlevel%'
Is there a way to pass switch operators to the powershell prompt that is created to execute scripts. Essentially I have a state file which executes a powershell script:
function1:
cmd.script:
- source: salt://utils/scripts/function1.ps1
- shell: "powershell"
- env: "-ExecutionPolicy bypass"
But this doesn't work.
Because a dirty solution is to run the script via cmd:
%windir%\System32\WindowsPowerShell\v1.0\powershell.exe -NonInteractive -executionpolicy Bypass -File
Any way to set the executionpolicy flag in the state file itself?
Thanks!
try using a file.managed to deploy the sls that holds the .exe and then use and cmd.run instead of cmd.script for the execution
I have the following in my .git/hooks/pre-commit file
#!/bin/sh
exec c:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -ExecutionPolicy RemoteSigned -Command " Get-Location | % { '$_\pre-commit-hook.ps1'} | % { & $_ }"
exit
This successfully executes the code in the pre-commit-hook.ps1 file in the same directory, but does not capture the exit code. According to tldp.org the last exit code will be returned if only exit is specified. Git hooks will fail if the exit code is non-zero, but even though my powershell script returns a status code of 1, it always succeeds. What can I do to capture the exit code from the powershell script so the hook will function correctly?
Keep the invocation of the ps1 script simple and you should have it working. The following works for me:
#!/bin/sh
echo
exec powershell.exe -ExecutionPolicy RemoteSigned -File '.\.git\hooks\pre-commit-hook.ps1'
exit
The ps1 script just had an exit 1 and the commit did not happen.
When you are doing stuff like -command, Powershell is not known to work properly and you might have to do something like -command {& .\test.ps1; exit $lastexitcode}
I am trying to execute the following post build event code but I am getting an non-useful error :
"c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -file "$(SolutionDir)tools\nuget_pack.ps1"
I have run the following PS script before I try :
Set-ExecutionPolicy unrestricted
What am I missing?
UPDATE
This is strange, I am not getting an error on VS now. but the script is not working. When I run it with powershell console I get the following error :
Visual Studio writes the post-build event script to a .BAT file and executes that using cmd.exe. So using & "<path-to-powershell>" won't work. Just execute:
Powershell.exe -file "$(SolutionDir)tools\nuget_pack.ps1"
And if you think you're likely to run into execution policy issues on other machines that build the solution, consider going this route:
Powershell.exe -ExecutionPolicy Unrestricted -file "$(SolutionDir)tools\nuget_pack.ps1"
You can reproduce the error in Powershell as follows:
"this is a string" -file "my.ps1"
It is taking the first as a string, the -file as the -f format flag and saying it doesn't have a value expression on the right for the format substitution.
Try like this:
& "c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -file "$(SolutionDir)tools\nuget_pack.ps1"
(as Keith notes, this will not work as this is run from a bat file than Powershell.)
Or just:
powershell.exe -file "$(SolutionDir)tools\nuget_pack.ps1"
Before calling power-shell script from visual studio, set the ExecutionPolicy to unrestricted from power-shell window like this...
Set-ExecutionPolicy -Scope CurrentUser;
ExecutionPolicy: unrestricted;
the call power-shell script in the following manner...
powershell.exe $(SolutionDir)Setup.ps1 -SolutionDir $(SolutionDir) -ProjectPath $(ProjectPath)
then in the script, you can always read the parameter like this...
param([string]$SolutionDir,
[string]$ProjectPath);
#Write-Host ($SolutionDir +" Call this script with following aruments");
#Write-Host ($ProjectPath +" Call this script with following aruments");