使用 .txt 文件查找 AD 组中的 ManagedBy 和 ManagedBy 电子邮件

发布于 2025-01-16 12:20:29 字数 766 浏览 0 评论 0原文

我在尝试读取我的脚本并将 AD 组与我的脚本一起应用时遇到问题。现在,它只是发布我的脚本中的内容,但我想让脚本读取我的 .txt 文件中的内容并将其与脚本的其余部分一起使用。

$filePath = "C:\Users\UserName\Downloads\ADGroupList.txt"
Get-Content -Path $filePath

Get-ADGroup -filter {Name -like "$filePath" } -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}}

导出 Csv -路径“C:\Users\用户名\Documents\ADGroupManagerList.csv”

I'm having issues trying to have my script read and apply the AD groups with my script. Right now, it's just posting what's in my script, but I would like to have the script read what's in my .txt file and use it with the rest of my script.

$filePath = "C:\Users\UserName\Downloads\ADGroupList.txt"
Get-Content -Path $filePath

Get-ADGroup -filter {Name -like "$filePath" } -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}}

Export-Csv -Path "C:\Users\UserName\Documents\ADGroupManagerList.csv"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

简单爱 2025-01-23 12:20:29

最简单的方法是循环遍历 ADGroupList.txt 文件中的组名称(假设这是组名称列表,每个组名称位于单独的行上)

$filePath = "C:\Users\UserName\Downloads\ADGroupList.txt"

# just loop over the group names you have in the text file and capture the output
$result = Get-Content -Path $filePath | ForEach-Object {
    $group = Get-ADGroup -Filter "Name -like '$_'" -Properties managedBy
    # create an object pre filled in when no manager was found
    $obj = [PsCustomObject]@{
        'Group Name'       = $group.Name
        'Managed By Name'  = 'N/A'
        'Managed By Email' = 'N/A'
    }
    # test if the ManagedBy is populated
    if (-not [string]::IsNullOrWhiteSpace($group.ManagedBy)) {
        # try to use the DN in property ManagedBy to find the manager
        try {
            $manager = Get-ADUser -Identity $group.ManagedBy -Properties EmailAddress -ErrorAction Stop
            $obj.'Managed By Name'  = $manager.Name
            $obj.'Managed By Email' = $manager.EmailAddress
        }
        catch {
            Write-Warning "No user found for '$($group.ManagedBy)'.. Please check AD."
        }
    }
    # output the object so it gets collected in variable $result
    $obj
}

# write the file
$result | Export-Csv -Path "C:\Users\UserName\Documents\ADGroupManagerList.csv" -NoTypeInformation

The easiest way is to loop over the group names you have in the ADGroupList.txt file (assuming this is a list of group names, each on a separate line)

$filePath = "C:\Users\UserName\Downloads\ADGroupList.txt"

# just loop over the group names you have in the text file and capture the output
$result = Get-Content -Path $filePath | ForEach-Object {
    $group = Get-ADGroup -Filter "Name -like '$_'" -Properties managedBy
    # create an object pre filled in when no manager was found
    $obj = [PsCustomObject]@{
        'Group Name'       = $group.Name
        'Managed By Name'  = 'N/A'
        'Managed By Email' = 'N/A'
    }
    # test if the ManagedBy is populated
    if (-not [string]::IsNullOrWhiteSpace($group.ManagedBy)) {
        # try to use the DN in property ManagedBy to find the manager
        try {
            $manager = Get-ADUser -Identity $group.ManagedBy -Properties EmailAddress -ErrorAction Stop
            $obj.'Managed By Name'  = $manager.Name
            $obj.'Managed By Email' = $manager.EmailAddress
        }
        catch {
            Write-Warning "No user found for '$($group.ManagedBy)'.. Please check AD."
        }
    }
    # output the object so it gets collected in variable $result
    $obj
}

# write the file
$result | Export-Csv -Path "C:\Users\UserName\Documents\ADGroupManagerList.csv" -NoTypeInformation
独﹏钓一江月 2025-01-23 12:20:29

要解决此问题,您可以使用此 powershell 脚本来获取组的 ma​​ngedBy

Get-ADGroup -filter * -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}}

输入图片此处描述

For workaround you can use this powershell script to get the mangedBy of groups.

Get-ADGroup -filter * -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}}

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文