views:

22

answers:

1

Synopsis: I am trying to convert a shell script from UNIX to Powershell. The script cannot "hardcode" the path, but must instead list a directory to dynamically build the path at runtime.

Also, the script must pass in 1 parameter to indicate the "type" of the run.

I am placing my "attempted" re-write of the script (after much googling/binging)

This script might be very close as the "echo" statements produced what seems to be correct.

$EXE_CLASS="com.sungard.sims.dacar.standalone.Dacar"
#$DACAR_TAG=%1
$JAVA_EXE="$ENV:JDK15\jre\bin\java"
#JAVA_EXE=\opt\java\bin\java

$JARS="dist\lib\Dacar-Engine.jar;dist\lib\Dacar-Common.jar;dist\lib\Dacar-Collector.jar;dist\lib\standalone-Dacar.jar;dist\lib\DACAR-CRG.jar;"
#dynamically append all .jar files from the jars directory.
ls jars\*.jar | %  { $JARS +=";jars\"+ $_.name   }

$JAVA_OPTS="-server -Xloggc:.\engine.gc.log -Xmx2048m -Xms2048m -XX:NewSize=512m -XX:MaxNewSize=512m -XX:SurvivorRatio=8 -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:MaxTenuringThreshold=8 -XX:CMSInitiatingOccupancyFraction=60 -XX:+PrintGCTimeStamps -XX:MaxPermSize=128m -XX:PermSize=128m -DinstName=${DACAR_TAG} -DprocType=ENGINE"

echo "exe:   -> $JAVA_EXE"
echo "opts:  -> $JAVA_OPTS"
echo "jars:  -> $JARS"
echo "class: -> $EXE_CLASS"

& $JAVA_EXE $JAVA_OPTS "-classpath .\configuration;$JARS $EXE_CLASS 1> dacar.out 2> dacar.err"

please note that I have commented out the use of the DACAR_TAG which would normally be passed in as %1 in batch script containing "TEST|DEV|PROD|QA|STRESS|[CLIENT SPECIFIC VALUE]"

Here is the output:

PS C:\dacar> .\startDacar.ps1
exe:   -> c:\Program Files\Java\jdk1.5.0_22\jre\bin\java
opts:  -> -server -Xloggc:.\engine.gc.log -Xmx2048m -Xms2048m -XX:NewSize=512m -XX:MaxNewSize=512m -XX:SurvivorRatio=8 -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:MaxTe
nuringThreshold=8 -XX:CMSInitiatingOccupancyFraction=60 -XX:+PrintGCTimeStamps -XX:MaxPermSize=128m -XX:PermSize=128m -DinstName=TESTING -DprocType=ENGINE
jars:  -> dist\lib\Dacar-Engine.jar;dist\lib\Dacar-Common.jar;dist\lib\Dacar-Collector.jar;dist\lib\standalone-Dacar.jar;dist\lib\DACAR-CRG.jar;;jars\activation.jar;jars
\commons-beanutils-1.6.jar;jars\commons-codec-1.3.jar;jars\commons-collections-3.2.jar;jars\commons-configuration-1.4.jar;jars\commons-dbcp-1.4.jar;jars\commons-digester
-1.6.jar;jars\commons-discovery-0.4.jar;jars\commons-id.jar;jars\commons-lang-2.2.jar;jars\commons-logging-1.0.3.jar;jars\commons-pool-1.5.4.jar;jars\concurrent.jar;jars
\graphics.jar;jars\ha-javamail-1.0-beta-1.jar;jars\hsqldb.jar;jars\ibatis-core-3.0.jar;jars\jcommon-1.0.6.jar;jars\jconn3.jar;jars\jdom.jar;jars\jetty-6.1.15.jar;jars\je
tty-util-6.1.15.jar;jars\jfreechart-1.0.3.jar;jars\jgroups-all.jar;jars\jmxri.jar;jars\jta.jar;jars\junit.jar;jars\log4j-1.2.9.jar;jars\mailapi.jar;jars\postgresql-8.0.3
09.jdbc2ee.jar;jars\postgresql-8.4-701.jdbc3.jar;jars\quartz-1.6.0.jar;jars\servlet-api-2.5-20081211.jar;jars\smtp.jar;jars\svnant.jar;jars\svnClientAdapter.jar;jars\svn
javahl.jar;jars\truezip-6.jar;jars\uic-optional.jar;jars\uic.jar
class: -> com.sungard.sims.dacar.standalone.Dacar
Unrecognized option: -server -Xloggc:.\engine.gc.log -Xmx2048m -Xms2048m -XX:NewSize=512m -XX:MaxNewSize=512m -XX:SurvivorRatio=8 -XX:+UseParNewGC -XX:+UseConcMarkSweepG
 -XX:MaxTenuringThreshold=8 -XX:CMSInitiatingOccupancyFraction=60 -XX:+PrintGCTimeStamps -XX:MaxPermSize=128m -XX:PermSize=128m -DinstName=TESTING -DprocType=ENGINE
Could not create the Java virtual machine.

Adding the tag will be my next question if I can't figure this out.

JUST AS AN FYI, from with in a ps1 script I can start it the program with the following command successfully:

& "c:\Program Files\Java\jdk1.6.0_20\jre\bin\java" -server -Xloggc:.\engine.gc.log -Xmx2048m -Xms2048m -XX:NewSize=512m -XX:MaxNewSize=512m -XX:SurvivorRatio=8 -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:MaxTenuringThreshold=8 -XX:CMSInitiatingOccupancyFraction=60 -XX:+PrintGCTimeStamps -XX:MaxPermSize=128m -XX:PermSize=128m -DinstName=TESTING -DprocType=ENGINE -classpath ".\configuration;dist\lib\Dacar-Engine.jar;dist\lib\Dacar-Common.jar;dist\lib\Dacar-Collector.jar;dist\lib\standalone-Dacar.jar;dist\lib\DACAR-CRG.jar;;jars\activation.jar;jars\commons-beanutils-1.6.jar;jars\commons-codec-1.3.jar;jars\commons-collections-3.2.jar;jars\commons-configuration-1.4.jar;jars\commons-dbcp-1.4.jar;jars\commons-digester-1.6.jar;jars\commons-discovery-0.4.jar;jars\commons-id.jar;jars\commons-lang-2.2.jar;jars\commons-logging-1.0.3.jar;jars\commons-pool-1.5.4.jar;jars\concurrent.jar;jars\graphics.jar;jars\ha-javamail-1.0-beta-1.jar;jars\hsqldb.jar;jars\ibatis-core-3.0.jar;jars\jcommon-1.0.6.jar;jars\jconn3.jar;jars\jdom.jar;jars\jetty-6.1.15.jar;jars\jetty-util-6.1.15.jar;jars\jfreechart-1.0.3.jar;jars\jgroups-all.jar;jars\jmxri.jar;jars\jta.jar;jars\junit.jar;jars\log4j-1.2.9.jar;jars\mailapi.jar;jars\postgresql-8.0.309.jdbc2ee.jar;jars\postgresql-8.4-701.jdbc3.jar;jars\quartz-1.6.0.jar;jars\servlet-api-2.5-20081211.jar;jars\smtp.jar;jars\svnant.jar;jars\svnClientAdapter.jar;jars\svnjavahl.jar;jars\truezip-6.jar;jars\uic-optional.jar;jars\uic.jar" com.sungard.sims.dacar.standalone.Dacar

But that is not really an option, as That is just invoking the thing in a hard coded fashion.

A: 

One thing to note about PowerShell variable expansion is that if it's anything more than a simple $VARIABLE expression you typically need to escape it using curly braces or a sub-expression.

So this line:

$JAVA_EXE="$ENV:JDK15\jre\bin\java"

Could be represented as either:

$JAVA_EXE="${ENV:JDK15}\jre\bin\java"

or...

$JAVA_EXE="$($ENV:JDK15)\jre\bin\java"

EDIT: Apparently PowerShell does correctly evaluate all 3 examples. I seem to recall the first syntax not working but perhaps that was in PowerShell 1.0 or a CTP...

Another useful tip that I often suggest people do when working with external .exe commands is to map it to an alias using the Set-Alias command. This way it makes working with the command a lot more natural as you don't have to use the &"string" syntax.

Set-Alias java "$($ENV:JDK15)\jre\bin\java.exe"
java blah1 blah2 etc
Josh Einstein
made the changes suggested, and while it might add to the readability of the script, it give generally the same error:java.exe : Unrecognized option: -server -Xloggc:.\engine.gc.log -Xmx2048m -Xms2048m -XX:NewSize=512m -XX:MaxNewSize=512m -XX:SurvivorRatio=8 -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:MaxTenuringThreshold=8 -XX:CMSInitiatingOccupancyFraction=60 -XX:+PrintGCTimeStamps -XX:MaxPermSize=128m -XX:PermSize=128m -DinstName= -DprocType=ENGINEAt C:\dacar\startDacar.ps1:16 char:5+ java <<<< $JAVA_OPTS "-classpath .\configuration;$JARS" $EXE_CLASS 1> dacar.out 2> dacar [truncated]
Q Boiler
Josh: Within strings a colon is still considered part of the variable name, so `$Env:Foo` *does* work as expected within a double-quoted string. You don't need to use `$()` or `${}` there.
Joey
Any Idea why I can't get this simple test to work? Should I make a simple downloaded example? or is there enough detail from above to see a potential issue?
Q Boiler
@Johannes, yes I edited my answer shortly after posting once I realized the mistake. Do you happen to know if 1.0 treated that differently? I consider myself a pretty advanced PowerShell user and somehow all this time I thought the colon "terminated" the variable expression.
Josh Einstein
@Q Boiler, I think the issue is that you are packing multiple options into a single string variable which is then being passed to the external command line parser as a single parameter. In other words, consider this: $f = "C:\Path with space\blah.txt"; SomeCommand $f; When PowerShell calls SomeCommand it will pass $f with quotes around it so it's treated like a single parameter. Can you break up your $java_opts variable into a combination of static text and multiple variables? Finally, the redirection args at the end won't be interpreted the way you think. Move them out of the string literal.
Josh Einstein
Josh: PowerShell v1 was the same in that regard; just tried it. It can make things a little awkward sometimes when you want to print a colon directly after a variable expansion, where you then have to escape the colon (or wrap the variable expansion; but escaping the colon is shorter). In fact, if you don't consider the special syntax to access items in PSDrives (which `$Env:Foo` is, essentially), then it's probably perfectly natural to assume that a colon isn't part of a variable name – most languages aren't that forgiving about naming ;-)
Joey