Powershell: Script modify help

Hello,

I am trying to get a report of all the users with specific employee id's. If employeeid is 10, 12,15,17 they belong to one category called CategoryA, and I need to get a totalcount for this category, and I have another category called CategoryB where employeeid is 5,14,16,20 .
I need a total count of all users and count of CategoryA users and CategoryB users. I need this value for all the domains in the forest.

Your help is appreciated. I have the following script:
$CategoryA = "10","12","15","17"
$CategoryB = "5,"14","16","20"

$domains = (Get-ADForest).domains
Foreach ($domain in $domains) {

$UserCount = (get-aduser -server $domain -filter *).count
$EnabledCount= (get-aduser -server $domain -filter 'enabled -eq $true').count

write-host "Total Users in domain: $UserCount"
write-host "Total Enabed Users in domain: $EnabledCount"

}
Parity123Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Since you have to check against multiple "categories", processing each user individually is best:
$CategoryA = "10","12","15","17"
$CategoryB = "5,"14","16","20"

$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 ($_.EmployeeID -in $CategoryA) { $CatACount++ }
     if ($_.EmployeeID -in $CategoryB) { $CatBCount++ }
  }
}

Open in new window

Parity123Author Commented:
It is prompting me with the following

cmdlet ForEach-Object at command pipeline position 2
Supply values for the following parameters:
Process[0]:
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Drag the { of line 6 to the end of line 5, that should work.
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Parity123Author Commented:
also if I wanted the count of all users that are not in CategoryA and CategoryB, how would I do it
David Johnson, CD, MVPOwnerCommented:
append this to the end of Qlemo's code
cls
write-output "Total Users:`t`t$UserCount"
write-output "Users in CategoryA:`t$CatACount"
write-output "Users in CategoryB:`t$CatBCount"
$NoCategory = $UserCount - ($CatACount + $CatBCount)
write-output "Users Not in any Category:`t$NoCategory"

Open in new window

Parity123Author Commented:
I modified the code as qlemo suggested, I am still getting the same error

cmdlet ForEach-Object at command pipeline position 2
 Supply values for the following parameters:
 Process[0]:

The code is below

$CategoryA = "11","23"
$CategoryB = "5","14","16","20"

$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 ($_.EmployeeID -in $CategoryA) { $CatACount++ }
     if ($_.EmployeeID -in $CategoryB) { $CatBCount++ }
  }
}
David Johnson, CD, MVPOwnerCommented:
$CategoryA = "10","12","15","17"
$CategoryB = "5","14","16","20"

$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 ($_.EmployeeID -in $CategoryA) { $CatACount++ }
     if ($_.EmployeeID -in $CategoryB) { $CatBCount++ }
     
  }
}
cls
write-output "Total Users:`t`t$UserCount"
write-output "Users in CategoryA:`t$CatACount"
write-output "Users in CategoryB:`t$CatBCount"
$NoCategory = $UserCount - ($CatACount + $CatBCount)
write-output "Users Not in any Category:`t$NoCategory"

Open in new window

Parity123Author Commented:
Thanks Dave and Qlemo

I am trying to get this report from all the AD forests, in some of my forests, there is powershell 2.0 installed, and it does not recognize -in operator. Is there an alternative I can use.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Yes, we can reverse the arguments and use -contains instead. That works with all PS releases.
$CategoryA = "10","12","15","17"
$CategoryB = "5","14","16","20"

$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++ }
     if ($CategoryB -contains $_.EmployeeID) { $CatBCount++ }
  }
}
cls
write-output "Total Users:`t`t$UserCount"
write-output "Users in CategoryA:`t$CatACount"
write-output "Users in CategoryB:`t$CatBCount"
$NoCategory = $UserCount - ($CatACount + $CatBCount)
write-output "Users Not in any Category:`t$NoCategory"

Open in new window

Parity123Author Commented:
OK, thank you so much. One thing I learnt is that I did not know you can store an array like

$CategoryA = "1", "2" as opposed to $CategoryA =@("1","2").

Is it one and the same.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Yes. @() just makes sure it is an array, even if containing only one value.
   ,1   is the same as   @(1)     but not as     1
This is useful in particular with cmdlet results, which might be none, one or n values. Only the later creates an array automatically.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.