What I'm trying to do is set the filter criteria in the script below. Instead of having:
$strFilter = "(&(&(objectCategory=person)(objectClass=user)(|(something)(something)(something)(something))))"
I would like to set the filter in a variable to allow for ease of adding criteria. Is this possible?
$strFilter = "(&(&(objectCategory=person)(objectClass=user)(|($filterFor))))"
$filterFor= @("samaccountname=eq_*",
"samaccountname=m_*",
"samaccountname=ocs_*",
"samaccountname=rm_*",
"samaccountname=svc_*")
Function GetFiltered{
#---------- All user accounts not a normal user (e.g. service accounts, admin accounts)
#$strFilter = "(&(&(objectCategory=person)(objectClass=user)(|(samaccountname=m_*)(samaccountname=ocs_*))))"
$strFilter="(&(&(objectCategory=person)(objectClass=user)(|($filterFor))))"
#===============================================================================================================#
# This section will open a window with the AD Structure listed. Select the OU you want to target and the filter selected will be targeted there
#===============================================================================================================
$GetADOU=Get-ADOrganizationalUnit-Filter*-properties* | Select canonicalname,DistinguishedName | sort-object canonicalname | Out-GridView-Title"Pick an OU"-passthru
$ADOU=$GetADOU.distinguishedname
#===============================================================================================================#
# This section allows for searching AD
#===============================================================================================================#
$objDomain=New-ObjectSystem.DirectoryServices.DirectoryEntry("LDAP://$ADOU")
$objSearcher=New-ObjectSystem.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot=$objDomain
$objSearcher.PageSize= 1000 #--if more than 1000 objects returned, you need to use the PageSize parameter set to 1000
$objSearcher.Filter=$strFilter
$objSearcher.SearchScope="Subtree"
#================================================================================================================#
#list the attributes you're wanting
#================================================================================================================#
$colProplist="name"
# create empty array
$objArray= @()
foreach ($iin$colPropList){$objSearcher.PropertiesToLoad.Add($i)}
$colResults=$objSearcher.FindAll()
foreach ($objResultin$colResults)
{$objItem=$objResult.Properties
$objItem.name
$ItemCol=""|select name
$ItemCol.name= [string]$objItem.name
$objArray+=$ItemCol
}
$objArray | export-csv C:\scripts\reports\getinfo-test.csv-NoTypeInformation
} # put a name for the file and the path above.
"samaccountname=m_*",
"samaccountname=ocs_*",
"samaccountname=rm_*",
"samaccountname=svc_*")
Function GetFiltered{
#---------- All user accounts not a normal user (e.g. service accounts, admin accounts)
#$strFilter = "(&(&(objectCategory=person)(objectClass=user)(|(samaccountname=m_*)(samaccountname=ocs_*))))"
$strFilter="(&(&(objectCategory=person)(objectClass=user)(|($filterFor))))"
#===============================================================================================================#
# This section will open a window with the AD Structure listed. Select the OU you want to target and the filter selected will be targeted there
#===============================================================================================================
$GetADOU=Get-ADOrganizationalUnit-Filter*-properties* | Select canonicalname,DistinguishedName | sort-object canonicalname | Out-GridView-Title"Pick an OU"-passthru
$ADOU=$GetADOU.distinguishedname
#===============================================================================================================#
# This section allows for searching AD
#===============================================================================================================#
$objDomain=New-ObjectSystem.DirectoryServices.DirectoryEntry("LDAP://$ADOU")
$objSearcher=New-ObjectSystem.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot=$objDomain
$objSearcher.PageSize= 1000 #--if more than 1000 objects returned, you need to use the PageSize parameter set to 1000
$objSearcher.Filter=$strFilter
$objSearcher.SearchScope="Subtree"
#================================================================================================================#
#list the attributes you're wanting
#================================================================================================================#
$colProplist="name"
# create empty array
$objArray= @()
foreach ($iin$colPropList){$objSearcher.PropertiesToLoad.Add($i)}
$colResults=$objSearcher.FindAll()
foreach ($objResultin$colResults)
{$objItem=$objResult.Properties
$objItem.name
$ItemCol=""|select name
$ItemCol.name= [string]$objItem.name
$objArray+=$ItemCol
}
$objArray | export-csv C:\scripts\reports\getinfo-test.csv-NoTypeInformation
} # put a name for the file and the path above.