Different Results with Powershel v2 and v3


hope can me determine why there difference in data between v2 , v3 of powershell. development on laptop has version 3 installed, script executed on server has version 2 installed.

#create directory searcher object , 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 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 script on machine produces data user looking for. when run on server, there users in data file should not be. in filter statement if change

-and (($_.properties["mail"] -ne "")))    #to    -and (($_.properties["mail"] -ne $null)))
users removed, other users included , haven't figured out why yet. why there difference, above script? think should work in version not true.


if find post has answered question, please mark answer. if find post helpful in anyway, please click vote helpful.

don't retire technet

try usinga simple , straighforward method. 

$searcher=[adsisearcher]'(&(objectcategory=person)(objectclass=user)(mail=*)(!samaccountname=its-*)(!useraccountcontrol:1.2.840.113556.1.4.803:=2))' $searcher.pagesize=500 $searcher.searchroot='ldap://ou=district offices,dc=mydomain,dc=com' $props=@("samaccountname","sn","givenname","mail","physicaldeliveryofficename","department","title","manager","distinguishedname") $searcher.findall() |      where-object{ 	    $_.properties['distinguishedname'][0] -notmatch ',ou=generic user accounts'         -and $_.properties["title"][0] -notmatch 'consultant|commissioner|security guard|osc|dcc.*temp'         -and $_.properties["samaccountname"][0] -ne 'tbjohn'     } 

notice uses less code , easier read.  put mail filter in search filter. (mail=*) - says include objects mail address not blank or null.   executes ont eh ad server , not on client powerhellll versiosn not behave differently.


¯\_(ツ)_/¯



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

some help on Event 540

WMI Repository 4GB limit - Win 2003 Ent Question

Event ID 1302 (error 1307) DFS replication service encountered an error while writing to the debug log file