Link to home
Start Free TrialLog in
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
Avatar of Qlemo
Qlemo
Flag of Germany image

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.
Avatar of Parity123

ASKER

I am going to restest this now. How would I export the $NotInA to a file.
$NotInA | export-csv -NoType c:\Temp\NotInA.csv
@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.
Of course. But if EmployeeID contains a space (blank), that does not work either.
ok, if wanted to include the null values as well, how would I modify this line
$NotInA += $_ | ? { $_.EmployeeID } | Select Name, EmployeeID
@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
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

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

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

Open in new window

@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
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial