1

We have a powershell script which we are shelling from a c# .Net 4.5 program on systems with powershell 4. One of its main purposes is to find and stop an associated windows service so we can maintain it.

The problem is on some sites the name of the service has brackets in the name i.e.

Acme Management Service (support.internal)

This causes powershell to generate an exception as I think it is trying to resolve (support.internal) as a property of an object. Is there a way to escape the brackets as we are unlikely to be able to rename all the potential windows service names out in the field.

An example which replicates this error is below

This is the argumentstring we pass to cmd.exe.

/c powershell -executionpolicy unrestricted c:\psrunner\robotest.ps1 -source "Acme Management Service (support.internal)" -destination "" -logName "c:\psrunner\logs\\03062015_14_04_51.txt"  > "c:\psrunner\logs\\powershell_log03062015_14_04_51.txt"

When we call the script i.e.

support.internal : The term 'support.internal' 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 line:1 char:52 + c:\psrunner\robotest.ps1 -source Neutrino Updater (support.internal) -destinatio ... + ~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (support.internal:String) [], Co mmandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

1
  • Try either backtick "`" or backslash to escape, or really do a service rename everywhere. Commented Jun 3, 2015 at 13:21

2 Answers 2

3

Single quotes instead of double quotes should also let PS see the input as a pure string.

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

Comments

1

Just escape with `` bracket if you don't want to use single quote (for example, in launch.json in Visual Studio Code, where single quote is forbidden for string arguments)

/c powershell -executionpolicy unrestricted c:\psrunner\robotest.ps1 -source "Acme Management Service `(support.internal`)" -destination "" -logName "c:\psrunner\logs\\03062015_14_04_51.txt"  > "c:\psrunner\logs\\powershell_log03062015_14_04_51.txt"

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.