Exchange 2010 Powershell + c#:如何做WHERE查询
本文关键字:何做 WHERE 查询 2010 Powershell Exchange | 更新日期: 2023-09-27 18:09:09
我在c#中像这样使用exchange powershell:
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;
}
}
现在我想在get-adpermission命令中添加一个"where"子句,像这样:
Get-ADPermission | where {($_.ExtendedRights -like “*Send-As*”) -and ($_.IsInherited -eq $false) -and -not ($_.User -like “NT AUTHORITY'SELF”)}
我完全不知道在哪里放置where子句或如何在花括号中定义这些参数。有人知道吗?
我看到了
Collection<PSObject> commandResults = powershell.ExecutePowershellCommand(commands, out errorMessages);
但是commandResults
什么也没做——它们没有被传递回调用者。
看起来你需要的是像
这样的东西 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"))
这将(希望)为您提供一个包含您需要的项目的comandResults
过滤列表。您将希望以某种方式将其传递回调用者,或者对它们执行其他操作。