I'm using the exchange powershell in C# like this:
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 = powershe开发者_运维百科ll.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;
}
}
Now I want to to add a "where" clause to the get-adpermission command like this:
Get-ADPermission | where {($_.ExtendedRights -like “*Send-As*”) -and ($_.IsInherited -eq $false) -and -not ($_.User -like “NT AUTHORITY\SELF”)}
I have absolutely no idea where to put the where clause or how to define those arguments in curly brackets. Anyone got an idea?
I see
Collection<PSObject> commandResults = powershell.ExecutePowershellCommand(commands, out errorMessages);
but nothing is done with commandResults
- they're not passed back to the caller.
What it looks like you'll need is something like
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"))
That will (hopefully) get you a filtered list of comandResults
with just the items you need. You'll want to pass that back to the caller somehow, or do something else with them.
精彩评论