3

I can't seem to run PowerShell code correctly with quotes. I have double quotes so the C# will see it as one quote, but escape it. But then the problem occurs, we write the variable onto the screen and it has quotes, as you would guess, but when I put that into the process the quotes just, disappear.

string var = @"function blink {while ($a -ne 1) {$color = get-random -max 17  -min 1
switch($color) {
""1"" {[console]::BackgroundColor = ""Black""}
""2"" {[console]::BackgroundColor = ""DarkBlue""}
""3"" {[console]::BackgroundColor = ""DarkGreen""}
""4"" {[console]::BackgroundColor = ""DarkCyan""}
""5"" {[console]::BackgroundColor = ""DarkRed""}
""6"" {[console]::BackgroundColor = ""DarkMagenta""}
""7"" {[console]::BackgroundColor = ""DarkYellow""}
""8"" {[console]::BackgroundColor = ""Gray""}
""9"" {[console]::BackgroundColor = ""DarkGray""}
""10"" {[console]::BackgroundColor = ""Blue""}
""11"" {[console]::BackgroundColor = ""Green""}
""12"" {[console]::BackgroundColor = ""Cyan""}
""13"" {[console]::BackgroundColor = ""Red""}
""14"" {[console]::BackgroundColor = ""Magenta""}
""15"" {[console]::BackgroundColor = ""Yellow""}
""16"" {[console]::BackgroundColor = ""White""}}
}}
blink";
Console.WriteLine(var);
Console.ReadLine();
System.Diagnostics.Process.Start("powershell.exe", @"powershell.exe -ExecutionPolicy Bypass -Command {" + var + "}");
Collin
  • 137
  • 1
  • 1
  • 8

1 Answers1

2

It looks like you might need to escape the quotes to get PowerShell to interpret your code correctly. It seems from this Microsoft page, and from this other page that the PowerShell escape character is the grave accent: `

The example useage on the second of those pages is for escaping from the command prompt, but the same principle should apply in this case.

I tested a portion of your code using:

System.Diagnostics.Process.Start("powershell.exe", @"powershell.exe -noexit -ExecutionPolicy Bypass -Command {[console]::BackgroundColor = '""Magenta'""}");

Which produced a PowerShell prompt with a magenta background, so it should just be a case of replacing all the instances of "" in your string with ``""`.

Edit: Additional info on escaping: Escaping quotes in powershell.exe via command prompt. Also, the answer DeveloperGuo has linked in the comments looks promising.

Community
  • 1
  • 1
Chris
  • 8,268
  • 3
  • 33
  • 46
  • Can we not use single quotes on the inside for powershell, and double quotes on the outside for c# `"'1'" {[console]::BackgroundColor = "'Black'"}` – Knuckle-Dragger Mar 31 '14 at 05:20
  • Potentially, I'll give that a try when I get home =D – Chris Mar 31 '14 at 07:37
  • @Chris For me this answer throws an error. :/ But with one line of it it works. :/ Weird. Also I'm running powershell version 4. (If that matters) – Collin Mar 31 '14 at 15:20
  • D'oh, I'm not familiar enough with PowerShell to instantly see what's going on (using the entire script), if I get a chance, I'll try running it and seeing what happens. – Chris Mar 31 '14 at 18:25