Powershell: Help with array

Parity123
Parity123 used Ask the Experts™
on
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"
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
$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

Open in new window

Author

Commented:
Thanks Qlemo. How do I also exclude employeeid with blank values, and only output values not in CategoryA and not a blank value.
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
The code does that! $NotInA contains entries with valid EmployeeID not contained in CategoryA.
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

Author

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

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

Author

Commented:
I meant the CategoryA does not contain blanks.
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

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

Author

Commented:
ok, thanks.

Author

Commented:
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"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial