简体   繁体   English

C#中的Powershell管道和Foreach对象

[英]Powershell Pipe and Foreach-Object in c#

I am invoking a get-msoluser cmdlet of office365 and i use the following cmdlet in powershell 我正在调用office365的get-msoluser cmdlet,并且在Powershell中使用以下cmdlet

 Get-MsolUser -UserPrincipalName user@organization.onmicrosoft.com | ForEach-Object{ $_.licenses}

The output is a collection of licenses and i wanted the same script to be run in c#. 输出是许可证的集合,我希望在C#中运行相同的脚本。 so i have written the code as follows 所以我写的代码如下

private void displayLicenses(){
     Command cmd = new Command("Get-MsolUser");
     cmd.Parameters.Add("UserPrincipalName","user@organization.onmicrosoft.com");
     Command cmd2 = new Command("ForEach-Object");
     cmd2.Parameters.Add("$_.licenses.AccountSku");
     Pipeline pipe = Office365Runspace.CreatePipeline();
     pipe.Commands.Add(cmd);
     pipe.Commands.Add(cmd2);
     Console.WriteLine("Before invoking the pipe");
     ICollection<PSObject> result = pipe.Invoke();
     CheckForErrors(pipe);
     Console.WriteLine("Executed command {0} + {1} with no error", cmd.CommandText, cmd2.CommandText);
      foreach(PSObject obj in result){
            foreach(PSPropertyInfo propInfo in obj.Properties){
                Console.WriteLine(propInfo.Name+": "+propInfo.Value+" "+propInfo.MemberType);
          }
     }
}

But i still get an error on executing this function saying 但是我在执行此功能时仍然遇到错误

Unhandled Exception: System.Management.Automation.CommandNotFoundException: The term 'ForEach-Object' is not recognized as the name of a cmdlet, function, scrip t file, or operable program. 未处理的异常:System.Management.Automation.CommandNotFoundException:术语'ForEach-Object'不被识别为cmdlet,函数,脚本文件或可运行程序的名称。 Check the spelling of the name, or if a path was in cluded, verify that the path is correct and try again. 检查名称的拼写,或者是否包含路径,请确认路径正确,然后重试。

I checked that my project has a reference to System.management.Automation.dll file that contains the ForEach-Object cmdlet. 我检查了我的项目是否对包含ForEach-Object cmdlet的System.management.Automation.dll文件进行了引用。

I found the dll using this cmd in powershell 我在Powershell中使用此cmd找到了dll

(Get-Command ForEach-Object).dll

Thanks, Satya 谢谢,萨蒂亚

I figured out the problem causing for the issue. 我找出导致问题的原因。 It is due to the misconfigured runspace i created. 这是由于我创建的运行空间配置错误。

    InitialSessionState initalState = InitialSessionState.Create();
    initalState.ImportPSModule(new String[] { "msonline" });
    //initalState.LanguageMode = PSLanguageMode.FullLanguage;

    Office365Runspace = RunspaceFactory.CreateRunspace(initalState);
    Office365Runspace.Open();

i was creating the initalstate with empty one,When i changed it to default one it worked fine.On creating the default one it includes all the modules that were obtained by default. 我创建的初始状态为空,当我将其更改为默认状态时,它可以正常工作。创建默认状态时,它包括默认情况下获得的所有模块。

  InitialSessionState initalState = InitialSessionState.CreateDefault();

it worked fine. 工作正常。

Thanks, Satya 谢谢,萨蒂亚

It sounds like your're trying to run that in the remote session at the Exchange server. 听起来您正在尝试在Exchange服务器的远程会话中运行它。 Those are NoLanguage constrained sessions, meaning that you can only run the Exchange cmdlets in those sessions. 这些是NoLanguage受限的会话,这意味着您只能在这些会话中运行Exchange cmdlet。 If you want to use PowerShell core language cmdlets (like foreach-object), you have to do that in a local session and either use Import-PSSession to import the Exchange functions into your local session (implicit remoting) , or use Invoke-Command and point it at the remote session on the Exchange server. 如果要使用PowerShell核心语言cmdlet(例如foreach-object),则必须在本地会话中执行此操作,并使用Import-PSSession将Exchange函数导入到本地会话中(隐式远程处理),或使用Invoke-Command并将其指向Exchange服务器上的远程会话。

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

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