Hope someone can help me determine why there is a difference in my data between v2 and v3 of Powershell. I do my development on my laptop which has version 3 installed, but the script is executed on a server which has version 2 installed.
#create directory searcher object and set it's porperties $searcher = New-Object DirectoryServices.DirectorySearcher # (!userAccountControl:1.2.840.113556.1.4.803:=2) - Filters out disabled accounts $searcher.Filter = '(&(objectCategory=person)(objectClass=user)(!samaccountname=ITS-*)(!userAccountControl:1.2.840.113556.1.4.803:=2))' $searcher.PageSize = 5 $searcher.SearchRoot = "LDAP://OU=District Offices,DC=myDomain,DC=com" #load only the following properties $params = @("samaccountname","sn","givenname","mail","physicaldeliveryofficename","department","title","manager","distinguishedname") foreach($param in $params) { $searcher.PropertiesToLoad.Add($param) | Out-Null } try { $found = $searcher.FindAll() $found | ForEach-Object { if (($_.Properties["distinguishedname"] -notlike "*,OU=Generic User Accounts*") -and ($_.Properties["title"] -notlike "*Consultant*") ` -and ($_.Properties["title"] -notlike "*Commissioner*") -and ($_.Properties["title"] -notlike "*Security Guard*") ` -and ($_.Properties["title"] -notlike "*OSC*") -and ($_.Properties["title"] -notlike "*DCC*Temp*") -and ($_.Properties["samaccountname"] -ne "tbjohn") ` -and (($_.Properties["mail"] -ne ""))) { $filtered += $_ } } }
Running this script on my machine produces the data that the user is looking for. When I run it on the server, there are users in the data file that should not be. In the filter statement if I change
-and (($_.Properties["mail"] -ne ""))) #TO BE -and (($_.Properties["mail"] -ne $null)))Then those users are removed, but then other users are included and I haven't figured out why yet. Why would there be a difference, with the above script? I would think that should work in any version but obviously that is not true.