'executing powershell with brackets in argument string

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



Solution 1:[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"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alejandro Galera