2015-09-22 39 views
0

我想以下用C#PS CMD運行不能夠通過管道傳遞的對象在C#中運行PowerShell命令

Get-SCRunAsAccount -VMMServer VmmserverName -Name proName | Remove-SCRunAsAccount 

我曾嘗試下面的程序,但配置文件沒有被清除。當我在Powershell中運行上述cmd時,它工作,這意味着cmd沒有問題。但不知道爲什麼RunAsAccount對象沒有通過C#中的管道傳遞。

我也試過運行Get-process | Sort-Object VM在c#中使用管道工作。

幫助我什麼,我是缺少在這裏

 Pipeline pipeline = Runspace.CreatePipeline();    
     Collection<PSObject> results = null; 
     string output = null; 

     Command command = new Command("Get-SCRunAsAccount");    
     command.Parameters.Add("VMMServer", "VmmserverName"); 
     command.Parameters.Add("Name", "proName"); 

     Command command1 = new Command("Remove-SCRunAsAccount"); 

     pipeline.Commands.Add(command); 
     pipeline.Commands.Add(command1); 
     pipeline.Commands.Add("Out-String");   
     results = pipeline.Invoke(); 

回答

0

您需要導入SCVMM的cmdlet,然後才能使用它們:

pipeline.Commands.Add("Import-Module").AddParameter("Name","VirtualMachineManager"); 
+0

我忘了提VMM模塊在運行空間已被導入。如果我運行下它正確顯示RunasAccount。命令command = new Command(「Get-SCRunAsAccount」); command.Parameters.Add(「VMMServer」,「VmmserverName」); command.Parameters.Add(「Name」,「proName」); pipeline.Commands.Add(command); pipeline.Commands.Add(「Out-String」); results = pipeline.Invoke(); –