C# 调用PowerShell';对象来自C的位置#

C# 调用PowerShell';对象来自C的位置#,c#,powershell,powershell-4.0,C#,Powershell,Powershell 4.0,如何从C#调用Where Object(不带过滤器)?我无法解析输出,因为我想将其传递到管道(不在下面的示例中) 附言: C#: 采集结果=null; Command command1=新命令(“Get-MailboxPermission”); command1.Parameters.Add(“标识”,邮箱); command1.Parameters.Add(“DomainController”,_DomainController); //我不能使用过滤器。这在PS中也是不可能的。 //comm

如何从C#调用
Where Object
(不带过滤器)?我无法解析输出,因为我想将其传递到管道(不在下面的示例中)

附言:

C#:

采集结果=null;
Command command1=新命令(“Get-MailboxPermission”);
command1.Parameters.Add(“标识”,邮箱);
command1.Parameters.Add(“DomainController”,_DomainController);
//我不能使用过滤器。这在PS中也是不可能的。
//command1.Parameters.Add(“过滤器”、“访问权限-匹配\“完全访问\”);

这个问题类似于:对于我的问题,这个答案是不够的。

检查下面的代码示例。我已经根据您的要求从代码项目中更新了一个示例

注意:

  • 使用
    \“
  • 若要将
    {
    }
    大括号添加到脚本文本中,请改用双大括号将其转义到字符串中。格式类似于
    {
    }

    // create Powershell runspace
    Runspace runspace = RunspaceFactory.CreateRunspace();
    
    // open it
    runspace.Open();
    
    // create a pipeline and feed it the script text
    Pipeline pipeline = runspace.CreatePipeline();
    
    //Getting all command variables
    string computerName = "YourComputerName";
    string matchingPattern = "con";
    
    //Create Script command
    String customScriptText = String.Format("Get-Process -ComputerName {0} | Where-Object {{$_.ProcessName -match \"{1}\"}}", computerName,matchingPattern);
    
    pipeline.Commands.AddScript(customScriptText);
    
    // add an extra command to transform the script output objects into nicely formatted strings
    // remove this line to get the actual objects that the script returns.
    pipeline.Commands.Add("Out-String");
    
    // execute the script
    Collection<PSObject> results = pipeline.Invoke();
    
    // close the runspace
    runspace.Close();
    
    // convert the script result into a single string
    StringBuilder stringBuilder = new StringBuilder();
    foreach (PSObject obj in results)
    {
        stringBuilder.AppendLine(obj.ToString());
    }
    

    Filter
    应该是
    ScriptBlock
    而不是
    string
    。你知道为什么我总是得到
    脚本块文本在受限语言模式或数据段中是不允许的。
    exception?你需要在目标机器上运行
    Set ExecutionPolicy Unrestricted
    。如果你没有使用“Set ExecutionPolicy Unrestricted”的权限?是否仍可以在不使用自定义脚本的情况下使用where object子句?
    Collection<PSObject> results = null;
    
    Command command1 = new Command("Get-MailboxPermission");
    command1.Parameters.Add("Identity", mailbox);
    command1.Parameters.Add("DomainController", _domainController);
    //I cannot use Filter. This is not possible in PS also.
    //command1.Parameters.Add("Filter", "AccessRights -match \"FullAccess\"");
    
    // create Powershell runspace
    Runspace runspace = RunspaceFactory.CreateRunspace();
    
    // open it
    runspace.Open();
    
    // create a pipeline and feed it the script text
    Pipeline pipeline = runspace.CreatePipeline();
    
    //Getting all command variables
    string computerName = "YourComputerName";
    string matchingPattern = "con";
    
    //Create Script command
    String customScriptText = String.Format("Get-Process -ComputerName {0} | Where-Object {{$_.ProcessName -match \"{1}\"}}", computerName,matchingPattern);
    
    pipeline.Commands.AddScript(customScriptText);
    
    // add an extra command to transform the script output objects into nicely formatted strings
    // remove this line to get the actual objects that the script returns.
    pipeline.Commands.Add("Out-String");
    
    // execute the script
    Collection<PSObject> results = pipeline.Invoke();
    
    // close the runspace
    runspace.Close();
    
    // convert the script result into a single string
    StringBuilder stringBuilder = new StringBuilder();
    foreach (PSObject obj in results)
    {
        stringBuilder.AppendLine(obj.ToString());
    }
    
        //Getting all command variables
        string username = "YourUserName";
    
        //Create Script command
        String customScriptText = String.Format("Get-MailboxPermission {0} | Where-Object {{$_.AccessRights -match \"FullAccess\" -and $_.IsInherited -eq $False}}", username);