2010-03-27 2 views
3

.net을 통해 powershell 개체를 만들어 명령을 호출했습니다. 내가 호출 할 때 '- 프로세스를 가져 오기'내가 가진 문제없이 같은 일반 명령 :PowerShell 클래스를 사용하여 "[namespace.class] :: method"스타일 명령 호출

ps.AddCommand("Get-Process").AddParameter(...).Invoke() 

하지만 "[namespace.class] :: 방법"구문과 .NET 메서드를 호출 할 수 아니에요, [System.IO.File] :: Exists ("c : \ boo.txt")를 호출하는 예를 만들면됩니다.

나는

ps.AddCommand("[System.IO.File]::Exists(\"c:\\boo.txt\")").Invoke() 

ps.AddCommand("[System.IO.File]::Exists").AddArgument("c:\\boo.txt").Invoke() 

일부 다른 사람들과 함께했습니다. 지정된 명령이 인식되지 않는다는 예외를 항상 throw합니다.

해당 유형의 명령을 호출하는 방법이 있습니까? 감사

당신은 .NET으로 부르고 있기 때문에 파이프 라인에 script를 추가 할 필요가

답변

2

이 필요 .NET 방법이 PowerShell을 commands 예컨대을 : 간주되지 않습니다 즉 스크립트

그것은 작동 보인다
static void Main() 
{ 
    PowerShell ps = PowerShell.Create(); 
    ps.AddScript(@"[IO.File]::Exists('C:\Users\Keith\foo.txt')"); 
    foreach (PSObject result in ps.Invoke()) 
    { 
     Console.WriteLine(result); 
    } 
} 
+0

, 감사합니다 :) – Marco

관련 문제