Avatar of Parity123
Parity123
Flag for United States of America asked on

Powershell: Help with array

Hello,
I have the following script:

I need to output the users name and employeeid  - values that are not in CategoryA  and where employeeid values that are not blanks.
 Please help.

$CategoryA = "10","12","15","17"


$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++ }
       }
}
cls
write-output "Total Users:`t`t$UserCount"
write-output "Users in CategoryA:`t$CatACount"
Powershell

Avatar of undefined
Last Comment
Qlemo

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Qlemo

THIS SOLUTION 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
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Parity123

ASKER
Thanks Qlemo. How do I also exclude employeeid with blank values, and only output values not in CategoryA and not a blank value.
Qlemo

The code does that! $NotInA contains entries with valid EmployeeID not contained in CategoryA.
Parity123

ASKER
Yes, but CategoryA does contain blanks. Can I add "" to the array to something like

$category = $CategoryA = "10","12","15","17",""
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Parity123

ASKER
I meant the CategoryA does not contain blanks.
Qlemo

Why would you want to do that? This part | ? { $_.EmployeeID } checks if EmployeeID contains something. If not, nothing will get added to $NotInA.
Parity123

ASKER
ok, thanks.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Parity123

ASKER
One quick question, if have an array like

$CategoryA="employee","consultant"

How would I check against the array if the values entered are sometimes lowercase,uppercase, mixedcase. for employeeID. What should the following line be changed to

if ($CategoryA -contains $_.EmployeeID)

Thanks.
Qlemo

The default mode for string compares in PowerShell is to be case-insensitive, so you need not to care about that. -contains is the same as -icontains, and if you need case-sensitive comparision that is done with -ccontains.