简体   繁体   English

无法通过C#中的管道传递对象以运行Powershell命令

[英]Not able to pass Object over pipeline in C# to run powershell commands

I want to run following PS cmd using C# 我想使用C#跟随PS cmd运行

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

I have tried below program but profile is not getting deleted. 我已经尝试过以下程序,但配置文件没有被删除。 When i run the above cmd in Powershell it worked, which mean no issue with cmd. 当我在Powershell中运行上述cmd时,它起作用了,这意味着cmd没问题。 but not sure why Object of RunAsAccount is not passing over pipeline in C#. 但不确定为什么RunAsAccount的对象未在C#中传递管道。

also i have tried run Get-process | Sort-Object VM 我也试过运行Get-process | Sort-Object VM Get-process | Sort-Object VM using pipeline in c# it worked. 在c#中使用管道对Get-process | Sort-Object VM进行Get-process | Sort-Object VM工作正常。

help me what i am missing here 帮我我在这里想念的东西

        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(); 

您需要先导入SCVMM cmdlet,然后才能使用它们:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM