0

I'm trying to call wlst/jython/python from powershell

set classpath with setWLSEnv.cmd is not set in the right session? so I have tried to set -cp as argument

& C:\bea\tpc\weblogic1033\server\bin\setWLSEnv.cmd; 
$cp='C:\bea\tpc\WEBLOG~1\server\lib\weblogic.jar'
$wlst='weblogic.WLST'
$script='C:\domains\tpc\Domain\bin\status.py'
$java="C:\PROGRA~1\Java\JROCKI~1.0\bin\java"
& "$java $cp $wlst $script"
#or
. "`"$java`" -cp `"$cp`" $wlst `"$script`""
#or
& "`"$java`" -cp `"$cp`" $wlst `"$script`""

I have tried to quote the command string in various ways without success

The term '"C:\PROGRA~1\Java\JROCKI~1.0\bin\java" -cp "C:\bea\tpc\WEBLOG~1\server\lib\weblogic.jar" weblogic.WLST "C:\domains\tpc\SasTT pcDomain\bin\status.py"' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At C:_WORK_\SAS\statusAll.ps1:15 char:2 + . <<<< ""$java" -cp "$cp" $wlst "$script"" + CategoryInfo : ObjectNotFound: ("C:\PROGRA~1\Ja...\bin\status.py":String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

2 Answers 2

1

When you use the call operator &, the next token needs to be the name of a command and nothing else. So instead of this:

& "$java $cp $wlst $script"

Try this:

& $java $cp $wlst $script

Sometimes getting arguments to native exes can get ugly. A technique that usually works but is unsafe if any of your arguments come from user input is this:

Invoke-Expression "$java $cp $wlst $script"
Sign up to request clarification or add additional context in comments.

Comments

0

In addition to the trouble with how you're formatting your command into a string, setWLSEnv.cmd is a script for the Windows Command Prompt. PowerShell cannot execute this file; it does not know how to interpret it, much like how Notepad does not know how to interpret a docx file. Windows associates .cmd files with the command prompt, so your first line is equivalent to

& cmd.exe /c C:\bea\tpc\weblogic1033\server\bin\setWLSEnv.cmd

(Note that the semicolon is unnecessary since you don't have any other commands on the same line.)

This creates a new process using cmd.exe. This new process executes the batch file, setting the environment variables you expect, and then the process exits, discarding the environment changes.

If you need setWLSEnv.cmd to set environment variables prior to executing your program, you should write a batch file that calls it instead of a PowerShell script. Otherwise, you will need to find or write a PowerShell equivalent to set up your environment.

3 Comments

As I thought. I was was just hoping that there was some magic that could force it to execute in the right session like in unix . ./env.sh
As an alternative, you can use the Invoke-BatchFile command in the PowerShell Community Extensions (pscx.codeplex.com) that will execute the batch file and propagate environment changes back to the PowerShell process.
@ReneHH PowerShell does have an equivalent, but it only works for PowerShell files, just like sh-derivatives can only interpret the files written in their dialect. It's not that PowerShell is missing functionality; it's that PowerShell and the Command Prompt are two very, very different types of command line shells. Asking PowerShell to run a .cmd file is kind of like asking an sh-derivative to run a batch file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.