我希望在与此类似的PowerShell查询中获取“组名”,“按名称管理”和“通过电子邮件管理”。
Get-ADGroup -filter {Name -like "*Admins" }
输出看起来类似于:
Group Name | Managed By Name | Managed By Email
我遇到的问题是加入Get-ADGroup
和Get-ADUser
。在SQL中,这种“连接”将在get-adgroup.managedby = get-aduser.distinguishedname
上发生。我知道这不是它在Powershell中的工作方式,只是想我会抛出一个我想要做的例子。
任何帮助都会受到欢迎和赞赏。
答案 0 :(得分:5)
我看到@Mathias R. Jessen打败了我,但这就是我所拥有的:
Get-ADGroup -filter {Name -like "*IT*" } -Properties managedBy |
ForEach-Object { `
$managedBy = $_.managedBy;
if ($managedBy -ne $null)
{
$manager = (get-aduser -Identity $managedBy -Properties emailAddress);
$managerName = $manager.Name;
$managerEmail = $manager.emailAddress;
}
else
{
$managerName = 'N/A';
$managerEmail = 'N/A';
}
Write-Output $_; } |
Select-Object @{n='Group Name';e={$_.Name}}, @{n='Managed By Name';e={$managerName}}, @{n='Managed By Email';e={$managerEmail}}
答案 1 :(得分:3)
你可以这样做:
$Groups = Get-ADGroup -Filter { Name -like "*Admins" } -Properties managedBy,mail
$Groups |Select-Object Name,@{Name='ManagedBy';Expression={(Get-ADUser $_.managedBy).Name}},Mail
@{}
之后的Select-Object
语法称为calculated property。
您还可以将群组管道传输到ForEach-Object
并在流程脚本块中调用Get-ADUser
:
Get-ADGroup -Filter {Name -like "*Admins"} -Properties managedBy,mail |ForEach-Object {
# Find the managedBy user
$GroupManager = Get-ADUser -Identity $_.managedBy
# Create a new custom object based on the group properties + managedby user
New-Object psobject -Property @{
Name = $_.Name
ManagedBy = $GroupManager.Name
Email = $_.mail
}
}
答案 2 :(得分:0)
我会这样做:
# Get all groups into a variable
$Group = Get-ADGroup -Filter {Name -like "*Admin*"} | Select-Object -expandProperty Name
foreach ($Groups in $Group){
# Get ManagedBy name for each group (If ManagedBy is empty group wil not be listed)
$User = Get-ADGroup $Groups -Properties * | Select-Object -ExpandProperty ManagedBy
foreach ($Users in $User){
# Get Name and EmailAddress for each User
$Name = Get-ADUser $Users | Select-Object -ExpandProperty Name
$Email = Get-ADUser $Users -Properties * | Select-Object -ExpandProperty EmailAddress
# Write output
Write-Host $Groups "," $Name "," $Email
}
}
希望这有帮助。