0

I have a bat file and I need to use it to call a powershell script.

I need to pass this powershell script a jdbc connect string which has lots of brackets and @'s. I have made a simple example which demos my problem.

my test.bat is as follows:

@PowerShell.exe -Command "C:\off_desk\ci\test\test.ps1 %1 %2 %3 %4 %5 %6 %7 %8 %9"

I use this with test.ps1 which is as follows:

Param(
  [string]$one
)

Write-Host "Got $one"

When I run:

test jdbc:oracle:thin:@(DESCRIPTION=(enable=broken)(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=cc-rjmetcal)(PORT=1521)))(CONNECT_DATA=(service_name=CITESTDB_001))))

I get an error:

Missing closing ')' in expression.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingEndParenthesisInExpression

I have tried running with quotes:

test "jdbc:oracle:thin:@(DESCRIPTION=(enable=broken)(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=cc-rjmetcal)(PORT=1521)))(CONNECT_DATA=(service_name=CITESTDB_001))))"

but this also errors.

I have tried many things I have found on the internet. Surrounding it with @and @" and various other things. None worked. I am limited by what a bat can do so I can't search the string for all the escape chars and prefix them with.

Does anyone have a solution?

4
  • I keep counting and counting, but it seems your statement has 9 opening and 10 closing brackets... Does not explain the message you get with a missing closing bracket though... Commented Sep 19, 2016 at 11:32
  • The content shouldn't matter. I just want powershell to leave the string alone Commented Sep 19, 2016 at 11:36
  • Oops right! During testing powershell complained about a bracket too much nevertheless ;) Commented Sep 19, 2016 at 12:11
  • Any particular reason why you constrain yourself to at most nine arguments? Wouldn't %* be simpler? Commented Sep 19, 2016 at 12:11

2 Answers 2

1

Change your test.bat to have single quotes around the arguments passed to PowerShell. Run with double quotes as you did before (specifies one argument, no need for %2 %3 etc)

@PowerShell.exe -Command "C:\off_desk\ci\test\test.ps1 '%1'"
Sign up to request clarification or add additional context in comments.

Comments

1

After testing and searching I found that neither the @ nor the brackets are causing problems, but the equal signs.

In this post is a workaround for that:

Passing your connection string enclosed in first single-quotes and the double-quotes like this:

'"connectionstring goes here"'

Working with your way (commandline-> batch-file -> powershell) this seems to be working correctly.

3 Comments

Hi, I re-ran the test with test a=b and the output was just a. I ran it again with test "a=b" and the output was got a=b. I don't think it's the equals signs as none of those tests errored as I described
The solution you proposed does work. (I tried lots of combinations including "'xxx'" but not the otherway around
After I tried your example from your question above no equal sign showed up when the error message came up and the problem laid after the last one: after that the connection string just ended. Try it yourself with the string you used above :)

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.