PowerShell command not executed? - powershell-2.0

In the following PowerShell v2.0 script, the command & is not executed :
$SqlPackageExe = "C:\Program Files (x86)\Microsoft SQL Server\120\DAC\bin\sqlpackage.exe"
Write-Host "Before SqlPackage"
& "$SqlPackageExe" /Action:Publish $SqlPackageParameters
Write-Host "After SqlPackage"
On the console output, I see :
Before SqlPackage
After SqlPackage
I have no error, no log, nothing of the SqlPackage.exe. What's happen ?
My command "& "$SqlPackageExe" /Action:Publish $SqlPackageParameters" works a first time, a second time (database is deployed), and then stops working.
If I close the PowerShell session and open a new PowerShell session, the command works several times, then stops working.
Thank you for your help

You need to redirect StdError and StdOut, see the answer that discusses " -RedirectStandardError -RedirectStandardOutput":
Powershell: Capture program stdout and stderr to separate variables

Related

powershell v2 possible redirection problem

I know this must be easy,
i have the following:
ls | % { file.exe $_.fullname }
What i want to do is just run the file.exe command on all files in the current folder.
Here is my problem: in powershell ISE console everything works out. The results from the file.exe (console program) are shown in the results pane for all files in the folder.
When i run the command in my shell though nothing is shown. All the results are redirected dont know where.
After that, whatever command i type in the shell i get no output.
Help!
Try opening PowerShell using PowerShell -NoProfile. Executing this command will open the shell igonring you profile script. This could be because of some conflicting setting in you profile script, if any.

Jenkins post build task script aborting when result of `{cmd}` is empty in script

I got strange behavior of Jenkins post build task script.
its purpose is showing build error in slack like following.
EDIT: our Jenkins running on Mac OSX Yosemite (10.10.4) and using Unity3d as build tool.
SLACK_BOT_PATH=$WORKSPACE/tools/bot.rb
SLACK_BOT_NAME="cortana"
SLACK_BOT_TOKEN=`cat $WORKSPACE/../../sendchat_token`
ERRORS=`tail -5000 ~/Library/Logs/Unity/Editor${BUILD_NUMBER}.log | grep ": error"`
ruby $SLACK_BOT_PATH $SLACK_BOT_NAME $SLACK_BOT_TOKEN "build fails : $ERRORS"
and strange behavior is, it aborted on the ERRORS= line when ERRORS has no contents (empty string). Jenkins console output is like following.
[workspace] $ /bin/sh -xe /var/folders/j3/8x825bdn2l9dm497yjs2144c0000gn/T/hudson7348609981772923445.sh
+ SLACK_BOT_PATH=*snip*
+ SLACK_BOT_NAME=cortana
++ cat *snip*/../../sendchat_token
+ SLACK_BOT_TOKEN=*snip*
++ tail -5000 ~/Library/Logs/Unity/Editor1710.log
++ grep ': error'
+ ERRORS=
POST BUILD TASK : FAILURE
after I change grep filter so that ERRORS has some contents, the post build script runs correctly again.
I want to report some general error message (eg. build fails) when no actual ERRORS found in logs. but also report detail error message when its available.
of course it is easy that inserting some line to send general message before grep error log so that every time such a general message sent to slack, but I want know the reason why empty ERRORS terminate entire script.
does anyone encounter same issue? if so, finally you know the cause of problem? thanks.
To be precise Jenkins will terminate your build when ERRORS is empty in you code because when there is no output from the grep command, your shell errorlevel will be set to 1 automatically , therefore it terminates your build with error ERRORS= , you can debug the same by printing errorlevel value. echo $ERRORLEVEL [linux] or echo %ERRORLEVEL% [windows]. Error 0 stand for success and other error codes stands for failure. if you want your post build script to be executed ignoring even if grep output is empty , then set errorlevel value in post build step of jenkins build.
ERRORLEVEL=0
Try the above and see if that helps
As #prudviraj explained, the issue is that your grep command returns without finding anything, and therefore returns with exit code 1.
Read detailed answer here: Jenkins Build Script exits after Google Test execution
In short: your script is launched with /bin/sh -xe, the -e means "fail immediately on any error", and your grep "errors out" when nothing is found.
The quick fix is to put set +e before the command. But the better way is proper error handling, as explained in the other answer I've linked.

Powershell why popup window, how to stop it

Not sure why when I execute a Python tools like pip or nosetests inside powershell, a separate popup command line windows will show, execute my command, then disappeared. This is annoying because I can hardly see the executable output, especially the last few lines before the popup close.
I assume there are some setting I can change to stop the popup?
I am using Powershell 2.0 in Windows 7.
Powershell is not cmd.exe, and it has a different console interface. More than likely, your py tools are writing to a non-existent shell window. You may be able to get around this by using the following syntax:
cmd /c script.py
What you do when you execute the python scripts directly from the PS prompt is fire-off a DOS shell for the period of time it takes for the command to complete. Since there's no 'pause' implemented, the shell window closes when the command completes.
A test script
# tester.py, just a test
print "This is a test script, that is all."
Output in PS:
C:\src\python
{powem} [36] --> .\tester.py
C:\src\python
{powem} [37] --> cmd /c .\tester.py
This is a test script, that is all.
mp
For someone has similar problem, please have a look at this answer, I think this solution eventually solved my problem. and in my case, I have to restart my computer to get it all working.

Executing robocopy using Powershell 2.0 command line is doing nothing. No errors

I am newbie to Powershell and Robocopy.
I am trying to run Robocopy command from Powershell 2.0 command-line nothing is happening no error messages.
But when I execute the same command using the Windows cmd-line it works fine.
The command details are as follows
$src="\\Test1\"
$dest="\\Test2\"
robocopy $src $dest /V /E /R=0 /W=0
I tried this also
robocopy \\Test1\ \\Test2\ /V /E /R=0 /W=0
My system is Windows 7 professional. The same script also works when executed using the Sapian powershell studio.
Am I missing anything here?
Any help in this will be greatly appreciated.
Thanks
Try this:
robocopy $src $dest "/V" "/E" "/R:0" "/W:0"

when executing Powershell Script from Ruby "Can't convert true into String" error

I am trying to execute a powershell script from Ruby, I have entered the below command:
scriptPath = system('powershell \"C:\\Scripts\\DB_Setup.ps1\"')
The ruby Script is handling exceptions when an error is raised to stop the script as below command:
rescue => ex
message = "\nscript on server '#{`hostname`.strip()}' terminated unexpectedly:\n\nMessage: '#{ex.message}"
raise ex
Output Error:
script on server 'TestDB1' terminated unexpectedly:
Message: 'can't convert true into String'
Thanks
Impossible to tell without seeing your script. Looks like your .ps1 script echos out a Boolean object, and Ruby isn't liking it. Either silence it, or change it to a string:
PS C:\> (1 -eq 1).gettype().name
Boolean
PS C:\> (1 -eq 1).tostring().gettype().name
String
PS C:\>
Thanks for your quick reply, I know that it is pretty easy to run a powershell script from ruby as mentioned in this link http://www.hanselman.com/blog/DNRTVScreencastPowershellIsStillShiny.aspx
, but for some reason I kept getting the same error message even though running a very basic powershell script such as below:
function Main()
{
Write-Host "Servername"
}
. Main
Plus on the otherside I tried your above solution.
This is very strange issue, after spending so much time on it I did a workaround to run a bat file from ruby kicking off the powershell script, I know this is not a good practice...

Resources