简体   繁体   中英

Powershell script to fetch name, title, department, manager name, and manager email from text file of groups

I'm trying to write a script that reads from a text file containing the target group names (that have been filtered to have ATLEAST one active user) and fetches the following parameters: name, title, department, manager name, and manager email. Ideally the result only shows users that are enabled and have a manager name and email.

From some intense googling and my limited knowledge in coding and ps, this is what I have so far:

[string[]]$arrayFromFile = Get-Content -Path 'C:\sslvpn-active.txt'
foreach($group in $arrayFromFile){
    $searchb = "CN="+"$group"+",OU=SSLVPN,OU=UserGroupsRAS,DC=xi,DC=xxxinc,DC=net"

    foreach($user in $searchb)
        {
            Write-Output $group
            Write-Output "----------------------------"
            Get-ADUser -Filter { Enabled -eq $true } -Properties Title,Department,Manager -SearchScope Subtree | select Name, Title, Department, @{n="ManagerName";e={get-aduser $_.manager | select -ExpandProperty name}}, @{n="ManagerMail";e={get-aduser $_.manager -properties mail | select -ExpandProperty mail}}

        }
 }

The text file looks something like this

SSLVPN-APC
SSLVPN-XYZ
SSLVPN-Microsoft
SSLVPN-Google
...

I would like the result to look something like this

SSLVPN-ABC
----------------------
Name        : Joe Smith
Title       : IT Engineer 2
Department  : IT Support
ManagerName : Billy George
ManagerMail : Billy.George@xxx.com

Name        : Matt Damon
Title       : IT Engineer 3
Department  : IT Support
ManagerName : Billy George
ManagerMail : Billy.George@xxx.com

SSLVPN-XYZ
----------------------
Name        : Jen Loo
Title       : Product Designer 3
Department  : Product Design
ManagerName : Ben Smit
ManagerMail : Ben.Smit@xxx.com
...

Any help is greatly appreciated! Thanks

It doesn't look like you are ever getting the group membership. Starting from your sample try the below:

    $Properties = @( 'Title', 'Department', 'Manager' )
    
    [string[]]$arrayFromFile = Get-Content -Path 'C:\sslvpn-active.txt'
    foreach($group in $arrayFromFile){
        $searchb = "CN="+"$group"+",OU=SSLVPN,OU=UserGroupsRAS,DC=xi,DC=xxxinc,DC=net"
    
        Get-ADGroupMember $searchb |
        Get-ADUser -Properties $Properties |
        Where-Object{ $_.Enabled } |
        Select-Object Name, Title, Department, 
            @{Name = "ManagerName"; Expression = { (Get-ADUser $_.Manager).Name }}, 
            @{Name = "ManagerMail"; Expression = { (Get-ADUser $_.Manager -Properties mail).Mail }}
    }

Note: The lack of the inner loop. However, I'm not sure you need the loop at all. Moreover, you may not need to concatenate the distinguished name.

    $Properties = @( 'Title', 'Department', 'Manager' )

    Get-Content -Path 'C:\sslvpn-active.txt' |
    Get-ADGroupMember |
    Get-ADUser -Properties $Properties |
    Where-Object{ $_.Enabled } |
    Select-Object Name, Title, Department, 
        @{Name = "ManagerName"; Expression = { (Get-ADUser $_.Manager).Name }}, 
        @{Name = "ManagerMail"; Expression = { (Get-ADUser $_.Manager -Properties mail).Mail }

This somewhat depends on the naming of your groups if they are unique in the environment etc...

You can also park some of the expressions at the top:

    $Properties = @( 'Title', 'Department', 'Manager' )
    
    $ManagerName = @{Name = "ManagerName"; Expression = { (Get-ADUser $_.Manager).Name }}
    $ManagerMail = @{Name = "ManagerMail"; Expression = { (Get-ADUser $_.Manager -Properties mail).Mail }}
    
    Get-Content -Path 'C:\sslvpn-active.txt' |
    Get-ADGroupMember |
    Get-ADUser -Properties $Properties |
    Where-Object{ $_.Enabled } |
    Select-Object Name, Title, Department, $ManagerName, $ManagerMail

I wanted to add an example to get the formatting you mentioned:

$ADProps = @( 'Title', 'Department', 'Manager' )
$Props =
$ADProps +
@(
    $ManagerName = @{Name = "ManagerName"; Expression = { (Get-ADUser $_.Manager).Name }}
    $ManagerMail = @{Name = "ManagerMail"; Expression = { (Get-ADUser $_.Manager -Properties mail).Mail }}
)

ForEach($Group in Get-Content -Path 'C:\temp\sslvpn-active.txt' )
{
    $Group = Get-ADGroup $Group

    $Group |
    Get-ADGroupMember |
    Get-ADUser -Properties $ADProps |
    Select-Object $Props |
    ForEach-Object{
        $Group.Name
        '----------------------'
        ($_ | Format-List | Out-String).Trim()
        ""
    }
}

If I think of a better approach edit further.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM