2014-03-30 1 views
3

따옴표로 PowerShell 코드를 올바르게 실행하지 못하는 것 같습니다. 나는 큰 따옴표를 사용하므로 C#은 하나의 따옴표로 간주하지만 이스케이프 처리합니다. 그러나 문제가 발생하면 우리는 변수를 화면에 씁니다. 그리고 추측 할 수있는 것처럼 따옴표가 있습니다. 그러나이를 프로세스에 넣으면 따옴표가 사라집니다.C에서 PowerShell 코드를 따옴표로 올바르게 실행할 수 없습니다.

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 + "}"); 
+0

귀하의 축 어적 문자열에는 새로운 행 문자가 포함됩니다. 명령 행으로 넘어갈 때'[ENTER]'키 누르기로 변환 될 것입니다 (또는 아마도 전혀 기억이 안나기도합니다). 새 줄을 제거하고 한 줄로 만들어야합니다. –

+0

@Simon Whitehead 거기에 있었으니. – Collinscripter

+1

http://stackoverflow.com/a/22365350/2855568 – DeveloperGuo

답변

2

PowerShell에서 코드를 올바르게 해석하려면 따옴표를 이스케이프 처리해야 할 수도 있습니다. 그것은이 Microsoft page에서 것, 그리고 this other page에서 PowerShell을 이스케이프 문자 무덤 악센트입니다 : `

해당 페이지의 두 번째의 예 useage 명령 프롬프트에서 탈출입니다,하지만 같은 원리에 적용해야 이 경우.

나는 사용하여 코드의 일부를 테스트 : 자홍색 배경으로 PowerShell 프롬프트를 생산, 그래서 그냥``로 문자열에 ""의 모든 인스턴스를 교체하는 경우이어야한다
System.Diagnostics.Process.Start("powershell.exe", @"powershell.exe -noexit -ExecutionPolicy Bypass -Command {[console]::BackgroundColor = '""Magenta'""}"); 

"".

편집 : 탈출에 대한 추가 정보 : Escaping quotes in powershell.exe via command prompt. 또한 DeveloperGuo가 주석에 링크 된 대답은 유망 해 보인다.

+0

우리는 powershell의 내부에서 작은 따옴표를 사용할 수없고 C#' " ''1 '"{Console} :: BackgroundColor = "'Black '"}''의 외부에서 큰 따옴표를 사용할 수 없습니까? –

+0

잠재적으로, 집에 가면 시도 = D – Chris

+0

@Chris 나를 위해이 대답은 오류를 던집니다. :/한 줄로 작동합니다. :/이상 하네. PowerShell 버전 4를 실행 중입니다. (문제가있는 경우) – Collinscripter

관련 문제