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
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
Parity123
Flag of United States of America image

ASKER

I am going to restest this now. How would I export the $NotInA to a file.
Avatar of Qlemo
Qlemo
Flag of Germany image

$NotInA | export-csv -NoType c:\Temp\NotInA.csv
Avatar of Parity123
Parity123
Flag of United States of America image

ASKER

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

Of course. But if EmployeeID contains a space (blank), that does not work either.
Avatar of Parity123
Parity123
Flag of United States of America image

ASKER

ok, if wanted to include the null values as well, how would I modify this line
$NotInA += $_ | ? { $_.EmployeeID } | Select Name, EmployeeID
Avatar of Parity123
Parity123
Flag of United States of America image

ASKER

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

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

Avatar of Parity123
Parity123
Flag of United States of America image

ASKER

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

$NotInA += $_.EmployeeID  | Select @{n='Domain'; e={$Domain}}, Name, EmployeeID
Avatar of Qlemo
Qlemo
Flag of Germany image

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

Open in new window

Avatar of Parity123
Parity123
Flag of United States of America image

ASKER

@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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Powershell
Powershell

Windows PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework. PowerShell provides full access to the Component Object Model (COM) and Windows Management Instrumentation (WMI), enabling administrators to perform administrative tasks on both local and remote Windows systems as well as WS-Management and Common Information Model (CIM) enabling management of remote Linux systems and network devices.

27K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo