Showing posts with label PowerShell. Show all posts
Showing posts with label PowerShell. Show all posts

Monday, June 07, 2010

Solution to: Powershell never exists when executed from a batch file / fdbuild

The short story: use <NUL

More details:

I have been using Powershell as part of a build script for a
FlashDevelop project.

The problem was that the Powershell process did not terminate
and so fdbuild.exe did not continue to run.

I searched the web quite a lot until I found the following
two:


They deals with a similar problem that happens when executing
Powershell from WScript.

The explanation given that was that Powershell is waiting for
its STDIN stream to close before it exists and a workaround is
suggested there that fits WScript.

This got me to the following solution for running Powershell
from Flashdevelop/fdbuild/batch:

since IMHO Powershell in those cases does not need any input
from STDIN I assigned NUL as its input stream.

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe
-Noninteractive -Command "& {$postbuild = (get-item ((get-item
%1).parent.parent.Fullname + '/postbuild.ps1')); & $postbuild
-outputDir %1 -outputName %2; exit $lastexitcode}" <NUL

I use this in a batch file that is called from the post-build
event using the following command line


$(ProjectDir)\..\postbuild.bat $(OutputDir)
$(OutputName)


I guess I could call Powershell directly from the post-build
command line but this will have to wait.

As far as I know I am the first to suggest this workaround,
please add a link to this post if youare quoting it. Thanks.

Sunday, January 03, 2010

Using PSCX from multiple users on same machine

I was having a problem with an automation script I was working on,
It used PowerShell and PowerShell community extensions (PSCX) and it worked fine of for me, but when other people used the same machine - it did not work.

after some looking around - the problem was that PSCX was installed from my user so it was automatically loaded on from my profile.

The resolution:

$pscxInstalled = $false;
$pscxLoaded = $false;
$registeredPSSnapins = Get-PSSnapin -registered
foreach ($registeredPSSnapin in $registeredPSSnapins) {
if ($registeredPSSnapin.Name -eq 'Pscx') {
write-host "PowerShell Community Extensions Found on system" -ForegroundColor green -BackgroundColor black
$pscxInstalled=$true;
$loadedPSSnapins = Get-PSSnapin
foreach ($loadedPSSnapin in $loadedPSSnapins) {
if ($loadedPSSnapin.Name -eq 'Pscx') {
$pscxLoaded=$true
write-host "PowerShell Community Extensions are loaded" -ForegroundColor green -BackgroundColor black
}
}
if (-not $pscxLoaded) {
write-host "Loading PowerShell Community Extensions from registered PSSnapin" -ForegroundColor green -BackgroundColor black
Add-PSSnapin Pscx

}
}
}

Sunday, December 06, 2009

single and double quotes in PowerShell (PoSh)

I was dealing with converting a batch to PoSh and had to create some wired mixture of double and single quotes. I was searching the net and got to this page http://www.vistax64.com/powershell/23546-mix-single-double-quotes.html where he used the following notation:

# $XY$X

so it hit me:
instead of odd things like
write-host '$X'"Y"'$X'

I can do:

$DQ = '"'
$SQ = "'"

and then
write-host '$X$SQ$DQY$DQ$SQ$X'

I guess there might clearer strings to read but at least it works.

maybe
$_DQ_ = '"'
$_SQ_ = "'"

write-host '$X$_SQ_$_DQ_Y$_DQ_$_SQ_$X'

 
Clicky Web Analytics