We help IT Professionals succeed at work.

Powershell: Code modification assistance

Parity123
Parity123 asked
on
36 Views
Last Modified: 2015-12-08
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
Comment
Watch Question

Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

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

Author

Commented:
I am going to restest this now. How would I export the $NotInA to a file.
Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
$NotInA | export-csv -NoType c:\Temp\NotInA.csv

Author

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

Commented:
Of course. But if EmployeeID contains a space (blank), that does not work either.

Author

Commented:
ok, if wanted to include the null values as well, how would I modify this line
$NotInA += $_ | ? { $_.EmployeeID } | Select Name, EmployeeID

Author

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

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

Author

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

$NotInA += $_.EmployeeID  | Select @{n='Domain'; e={$Domain}}, Name, EmployeeID
Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

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

Open in new window

Author

Commented:
@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.
"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.