3

I have this command prompt script that I need to convert to a powershell script, but I am confused with where to even start since I am unfamiliar with powershell. To begin I think I would need to declare the variables in a different way and refer to them differently within the long command.

@echo On
setlocal

set base_dir=D:\Temp\peijun\LoadLegacyDocs
set java_home=D:\Programs\jre6.31
set environment=stg
set result=logs\%environment%\%1.properties

"%java_home%\bin\java" -javaagent:%base_dir%\lib\openejb-3.1.4\lib\openejb-javaagent-3.1.4.jar -classpath %base_dir%\config\%environment%;%base_dir%\lib\openejb-3.1.4\lib\*;%base_dir%\lib\jar\*;%base_dir%\lib\common_lib\*;d:\documentum\config;d:\Programs\Documentum\dctm.jar -Dlog4j.configuration=file:///%base_dir%/config/%environment%/log4j.properties com.eds.jdc.util.LoadLegacyDocs %1 %2 %3

EDIT:

This is what I currently have. Not much change except changing how the variables are declared and referred to in the long command line. Am I on the right track?

Write-Host On

$env:base_dir="D:\Temp\peijun\LoadLegacyDocs"
$env:java_home="D:\Programs\jre6.31"
$env:environment="stg"
$env:result="logs\$environment\%1.properties"

"$java_home\bin\java" -javaagent:$base_dir\lib\openejb-3.1.4\lib\openejb-javaagent-3.1.4.jar -classpath $base_dir\config\$environment;$base_dir\lib\openejb-3.1.4\lib\*;$base_dir\lib\jar\*;$base_dir\lib\common_lib\*;d:\documentum\config;d:\Programs\Documentum\dctm.jar -Dlog4j.configuration=file:///$base_dir/config/$environment/log4j.properties com.eds.jdc.util.LoadLegacyDocs %1 %2 %3
6
  • 3
    Hardly classifiable as a java question regardless of what the script does imo. Commented Sep 22, 2021 at 14:56
  • What are the three in-params %1 %2 %3 supposed to be? Complete file paths? Does it always has to be three parameters? Commented Sep 22, 2021 at 15:13
  • The three in-params are two file paths and a boolean. Commented Sep 22, 2021 at 15:30
  • What does the boolean symbolize? And is it paths to directories or files? Commented Sep 22, 2021 at 15:31
  • Is the boolean a true boolean or just the text True/False? Commented Sep 22, 2021 at 15:38

1 Answer 1

4

The argumentlist for java is probably right (now) in my example. Try this out...

[CmdletBinding()]
param (
    [Parameter(Mandatory)]
    [ValidateNotNullOrEmpty()] 
    [string]$dir1,

    [Parameter(Mandatory)]
    [ValidateNotNullOrEmpty()]
    [string]$dir2,

    [Parameter(Mandatory)]
    [boolean]$Save
)

$Env:base_dir = 'D:\Temp\peijun\LoadLegacyDocs'
$Env:java_home = 'D:\Programs\jre6.31'
$Env:environment = 'stg'
$Env:result = "logs\$Env:environment\$dir1.properties"

& "$Env:java_home\bin\Java" "-javaagent:$Env:base_dir\lib\openejb-3.1.4\lib\openejb-javaagent-3.1.4.jar" "-classpath $Env:base_dir\config\$Env:environment;$Env:base_dir\lib\openejb-3.1.4\lib\*;$Env:base_dir\lib\jar\*;$Env:base_dir\lib\common_lib\*;d:\documentum\config;d:\Programs\Documentum\dctm.jar" "-Dlog4j.configuration=file:///$Env:base_dir/config/$Env:environment/log4j.properties com.eds.jdc.util.LoadLegacyDocs $dir1 $dir2 $Save"

But .ps1 files can't be started "stand alone", you must use a PowerShell host (i.e. the PowerShell console) and not cmd.exe to run it.

Sign up to request clarification or add additional context in comments.

Comments

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.