2011-09-21 7 views
1

이 같은 C#으로 교환 PowerShell을 사용하고 있습니다 :Exchange 2010의 파워 쉘 + C 번호 : 수행하는 방법에 WHERE 쿼리

public PowershellResult GetSendAsPermissions(string TargetIdentity) 
    { 
     string CommandName = "get-adpermission"; 

     Command cmd1 = new Command(CommandName); 
     cmd1.Parameters.Add("Identity", TargetIdentity); 
     if (powershell.DomainController.Length > 0) 
     { 
      cmd1.Parameters.Add("DomainController", powershell.DomainController); 
     } 

     List<Command> commands = new List<Command>(); 
     commands.Add(cmd1); 
     string errorMessages = string.Empty; 
     Collection<PSObject> commandResults = powershell.ExecutePowershellCommand(commands, out errorMessages); 


     PowershellResult psr = null; 

     if (errorMessages.Length == 0) 
     { 
      psr = new PowershellResult(true, "Successfully managed SendAs permission.", 0); 
     } 
     else 
     { 
      psr = new PowershellResult(false, string.Format("Error managing SendAs permission: {0}", errorMessages), 0); 
     } 
     return psr; 
    } 

public Collection<PSObject> ExecutePowershellCommand(List<Command> commands, out string errorMessages) 
    { 
     errorMessages = ""; 
     this.ps.Commands.Clear(); 
     ps.Streams.Error.Clear(); 
     if (commands != null) 
     { 
      foreach (Command cmd in commands) 
      { 
       this.ps.Commands.AddCommand(cmd); 
      } 
      Collection<PSObject> ret = null; 
      try 
      { 
       ret = this.ps.Invoke(); 
       if (this.ps.Streams.Error.Count > 0) 
       { 
        StringBuilder stb = new StringBuilder(); 
        foreach (ErrorRecord err in this.ps.Streams.Error) 
        { 
         if (err.Exception.Message != null) 
         { 
          stb.AppendLine(err.Exception.Message); 
         } 
        } 
        errorMessages = stb.ToString(); 
       } 
      } 
      catch (Exception ex) 
      { 
       StringBuilder stb = new StringBuilder(); 
       if (ex.Message != null) 
       { 
        stb.Append(ex.Message); 
       } 
       if (ex.InnerException != null) 
       { 
        if (ex.InnerException.Message != null) 
        { 
         stb.Append(string.Format(" {0}", ex.InnerException)); 
        } 
       } 
       errorMessages = stb.ToString().Trim(); 
      } 

      return ret; 
     } 
     else 
     { 
      return null; 
     } 
    } 

지금 내가 얻을-해서 ADPermission을 명령에 "여기서"절을 추가 할 다음과 같이하십시오 :

Get-ADPermission | where {($_.ExtendedRights -like “*Send-As*”) -and ($_.IsInherited -eq $false) -and -not ($_.User -like “NT AUTHORITY\SELF”)} 

where 절을 넣을 위치 나 중괄호 안에 해당 인수를 정의하는 방법에 대해서는 전혀 알지 못합니다. 누구나 아이디어있어?

답변

1

나는

Collection<PSObject> commandResults = powershell.ExecutePowershellCommand(commands, out errorMessages); 

를 볼 수 있지만 아무것도 commandResults으로 수행되지 않습니다 - 그들은 호출자에게 다시 전달되지 것입니다. 당신이 필요로하는 것처럼 보이는 무엇

는 (희망) 당신이 필요로하는 단지 품목이 당신에게 comandResults의 필터링 된 목록을 얻을 것이다

var filteredResults = commandResults.Where(cr =>  
     (((string)cr.Properties["ExtendedRights"].Value).Contains("Send-As")) && 
     (((bool)cr.Properties["IsInherited"].Value) == false) && 
     (((string)cr.Properties["User"].Value) == "NT AUTHORITY\\SELF")) 

같은 것입니다. 어떻게 든 호출자에게 다시 전달하거나 그 밖의 다른 작업을 수행 할 수 있습니다.

+0

"Value"는 "object"유형의 것일 뿐이므로 메서드에 "Contains"라는 메서드가 없습니다. – hoetz

+0

아, 이제 문자열로 변환해야합니다. 업데이트 된 답변보기 –

+0

PSObject와 같은 몇 가지 예외는 문자열에 캐스트 할 수 없습니다. 캐스팅과 검사가 많이 필요합니다 ... – hoetz

관련 문제