Output file not created as coded

36 views Asked by At

Below is an extract from my script. The script checks if users are part of a specific AD groud then saves the users and the group they're a member of to a .CSV file.

    function Process-UsersInOU {
    param (
        [string[]]$ouDistinguishedName,
        [string]$ouType
    )

    $outputFile = Join-Path -Path $location -ChildPath "src\Results\User_Profiles_$ouType_$($scriptStartTime.ToString('dd-MM-yyyy HHmmss')).csv"

    $results = @()
    foreach($ou in $ouDistinguishedName){

        Write-Output "Processing $($ou) ..."
        $Users = Get-ADUser -Filter 'enabled -eq $true' -SearchBase $ou
        write-output "This OU has $($Users.count) users..."
        foreach ($user in $Users) {
            $Groups = Get-ADPrincipalGroupMembership $user | Where-Object { $_.Name -like "*profiles*" }

            if ($Groups.Count -eq 0) {
                $result = [PSCustomObject]@{
                    'UserID' = $user.SamAccountName
                    'ProfilesGroup' = "Not a member of any group"
                }
            } else {
                $result = [PSCustomObject]@{
                    'UserID' = $user.SamAccountName
                    'ProfilesGroup' = ($Groups | ForEach-Object { $_.Name }) -join ', '
                }
            }

            $results += $result
        }
    }
    # Export results to CSV
    $results | Export-Csv -Path $outputFile -NoTypeInformation
}

Below is the function call:

    $laptopOUs = Get-ADOrganizationalUnit -Filter 'Name -like "*Users - Laptops*" ' | where {$_.DistinguishedName -like "*OU=MEA,*" -and $_.DistinguishedName -notlike "*Inactive*" -and $_.DistinguishedName -notlike "*test*"}

$nonlaptopOUs = Get-ADOrganizationalUnit -Filter 'Name -like "*Users - Thin clients*" -or Name -like "*Users - Desktops*" ' | where {$_.DistinguishedName -like "*OU=MEA,*" -and $_.DistinguishedName -notlike "*Inactive*"}

Process-UsersInOU -ouDistinguishedName $laptopOUs.DistinguishedName -ouType "laptop"
Process-UsersInOU -ouDistinguishedName $nonlaptopOUs.DistinguishedName -ouType "non-laptop" 

My issue, is that the output file is missing the $ouType in the file name.

0

There are 0 answers