Avatar of Parity123
Parity123Flag for United States of America asked on

Powershell: Code modification assistance

Hello,

I have the following code, and I also want to filter out "blanks/not set" values. Can you please assist.
$NotInA  (must not contain any users with blank values for employeeid) and (anything that does not match $CategoryA)
If have 3 users with values 10, 26,blank. $NotInA must contain only the user with the value 26.

$CategoryA = "10","12","15","17"
$NotInA = @()

$UserCount = $EnabledCount = $CatACount =$CatBCount = 0
foreach ($domain in Get-ADForest | select -Expand Domains) {
  Get-ADUser -Server $domain  -Filter * -Property EmployeeID | % {
     $UserCount++;
     if ($_.Enabled) { $EnabledCount++ }
     if ($CategoryA -contains $_.EmployeeID) {
       $CatACount++
     } else {
       $NotInA += $_ | ? { $_.EmployeeID } | Select Name, EmployeeID
     }
  }
}
cls
write-output "Total Users:`t`t$UserCount"
write-output "Users in CategoryA:`t$CatACount"
write-output "Users not in CategoryA:"$NotInA | ft -a
Powershell

Avatar of undefined
Last Comment
Qlemo

8/22/2022 - Mon
Qlemo

Does my code not work? The "empty" check is there, | ? { $_.EmployeeID }.
If it does not work, the EmployeeID contains something, e.g. a blank (which is not the same as nothing). But an unpopulated AD attribute is empty, not blank.
ASKER
Parity123

I am going to restest this now. How would I export the $NotInA to a file.
Qlemo

$NotInA | export-csv -NoType c:\Temp\NotInA.csv
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER
Parity123

@Qlemo:

can I use the following filter

Get-ADUser -Server $domain  -Filter 'employeeid -like "*" '  -Property EmployeeID  

so I don't have to process all the users.
Qlemo

Of course. But if EmployeeID contains a space (blank), that does not work either.
ASKER
Parity123

ok, if wanted to include the null values as well, how would I modify this line
$NotInA += $_ | ? { $_.EmployeeID } | Select Name, EmployeeID
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
Parity123

@Qlemo: One final thing is if I want to include the domain name along with employeeid and name, how would I modify the output code. The output would look like

domain1,user1,30
domain2,user10,45
Qlemo

Re filtering, you can
either use the "-filter" approach, and those are not included in the user count at all (and you do not have to check further);
or  If you want to have the user count correct, the code as posted originally should work.

Re: domain:
       $NotInA += $_ | ? { $_.EmployeeID } | Select @{n='Domain'; e={$Domain}}, Name, EmployeeID

Open in new window

ASKER
Parity123

@Qlemo: The following line should output the null values as well, right

$NotInA += $_.EmployeeID  | Select @{n='Domain'; e={$Domain}}, Name, EmployeeID
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Qlemo

No, it has to be
$NotInA += $_ | Select @{n='Domain'; e={$Domain}}, Name, EmployeeID

Open in new window

ASKER
Parity123

@Qlemo: How can I differentiate between a space and null value in employeeid. Is it possible to output null if the value is not set.
Thanks so much for your assistance.
ASKER CERTIFIED SOLUTION
Qlemo

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question