0

If the user is disabled, I want to export them as a single line. I won't export enabled users and starting with S-1-5-21-.

e.g : CONTOSO\user11 , CONTOSO\user53 and CONTOSO\user32 are disabled.

My input file:

Permissions
CONTOSO\user11;CONTOSO\user23;CONTOSO\user53;S-1-5-21-609545082-2795152396-2074981628-14751
CONTOSO\user32;CONTOSO\user236

My desired output:

CONTOSO\user11;CONTOSO\user53
CONTOSO\user32

I have single column called 'permissions'.

Currently I am getting an console output like below. it works

https://i.stack.imgur.com/uf86v.jpg

My desired output:

If the user is disabled, I want to export them as a single line.

https://i.stack.imgur.com/CRCsl.jpg

Script :

$userList = (Import-Csv -Path C:\temp\test.csv).permissions -split ';'

foreach ($user in $userList) {
    $adUser = $user -replace '.*\\' | Get-ADUser -ErrorAction SilentlyContinue -Property enabled   

    [PSCustomObject]@{
        Identity = $user
        Status   = switch ($adUser) {
            { -not $_ } {
                'deleted'
                continue
            }
            { $_.Enabled } {
                'enabled'
                continue
            }
            default {
                'disabled'
            }
        }
    }
}

EDIT :

My input file:

Permissions
CONTOSO\user11;CONTOSO\user23;CONTOSO\user53;S-1-5-21-609545082-2795152396-2074981628-14751
CONTOSO\user32;CONTOSO\user236

My desired output:

CONTOSO\user11;CONTOSO\user53
CONTOSO\user32

theo script output:

CONTOSO\user11;CONTOSO\user53;CONTOSO\user32

EDIT2:

user32 , user45 , user200 are enabled

CONTOSO\user11;CONTOSO\user53
CONTOSO\user32
CONTOSO\user45;CONTOSO\user200
CONTOSO\user43;CONTOSO\user99

My desired output:

CONTOSO\user11;CONTOSO\user53
                             <-- blank line
                             <-- blank line
CONTOSO\user43;CONTOSO\user99

EDIT 3 :

user01 , user02 : enabled user03 : disabled

Permissions
CONTOSO\user01
CONTOSO\user02
CONTOSO\user03

4 blank line is coming instead of 2 blank line

             <-- blank line
             <-- blank line
             <-- blank line
             <-- blank line
CONTOSO\user03
Arbelac
  • 1,698
  • 6
  • 37
  • 90
  • Please, [DO NOT post images of code, data, error messages, etc.](https://meta.stackoverflow.com/a/285557), see [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – iRon Mar 28 '23 at 05:44

1 Answers1

1

It seems you want a text file with one single line that holds the user IDs of disabled users delimited with a semi-colon (;)

In that case just do

$userList = (Import-Csv -Path D:\Test\test.csv).Permissions -split ';' |
             Where-Object { !$_.StartsWith("S-1-5-21-") }  # skip SIDs starting with "S-1-5-21-"

# look for disabled users
$result = foreach ($user in $userList) {
    $adUser = $user -replace '^.*\\' | Get-ADUser -ErrorAction SilentlyContinue
    if ($adUser) {
        if (!$adUser.Enabled) { $user }
    }
    else {
        Write-Warning "User '$user' could not be found in AD.."
    }
}

# output as single line in plain-text file (-PassThru will also display on screen)
$result -join ';' | Set-Content -Path 'X:\Somewhere\DisabledUsers.txt' -PassThru

If it is just the format of the output, then use this instead:

$userList = Import-Csv -Path D:\Test\test.csv
$result = foreach ($row in $userList) {
    $newRow = $row.Permissions -split ';' | ForEach-Object {
        if (!$_.StartsWith("S-1-5-21-")) {
            $adUser = $_ -replace '^.*\\' | Get-ADUser -ErrorAction SilentlyContinue
            if ($adUser) {
                if (!$adUser.Enabled) { $_ }
            }
            else {
                Write-Warning "User '$_' could not be found in AD.."
            }
        }
    }
    if (@($newRow).Count) { $newRow -join ';' }
}
# output to new file
$result -join [environment]::NewLine | Set-Content -Path 'X:\Somewhere\DisabledUsers.txt'
Theo
  • 57,719
  • 8
  • 24
  • 41
  • @Arbelac Then change the title of your question too. It now clearly states: _**as single-line plain-text**_ – Theo Mar 28 '23 at 12:51
  • @Arbelac Yep, that would do it – Theo Mar 28 '23 at 15:16
  • You could add `else { [Environment]::NewLine }` directly underneath `if (!$adUser.Enabled) { $_ }` – Theo Mar 29 '23 at 13:29
  • @Arbelac you keep changing your mind as to how the output should be and honestly, I have no idea why you now want it with blank lines in between. I mean.. what's the use of such output? Why not create a csv where you have the username and a field of that user being enabled or not or even not existing in AD? Why oh why combine disabled users in a single line using a semi-colon character, whilst for every enabled user you want an empty line really is beyond me.. – Theo Mar 29 '23 at 15:16
  • 1
    You're right man. I am overthinking :) like you said I have figured it out another method. (enabled and disabled filtering) thanks again – Arbelac Apr 01 '23 at 19:04
  • How can I update my input CSV file based on server status? I have a question like that . https://stackoverflow.com/questions/76065787/updating-input-csv-file-based-on-server-status – Arbelac Apr 20 '23 at 15:27