help .\Get-CNUser -Examples
<#
.SYNOPSIS
Search for Users using privided Canonical Names (CNAME) of User or OU.
Guy Lidbetter
THIS CODE IS MADE AVAILABLE AS IS, WITHOUT WARRANTY OF ANY KIND. THE ENTIRE
RISK OF THE USE OR THE RESULTS FROM THE USE OF THIS CODE REMAINS WITH THE USER.
Version 1.0 21st September 2015
.DESCRIPTION
This script uses a provided Canonical Name (CNAME) to locate accounts using the ActiveDirectory
module cmndlet Get-ADUser.
IMPORTANT NOTE: This script requires the ActiveDirectory Module as installed with RSAT tools if not run on a domain controller.
.PARAMETER CNAME
The CNAME to be searched with Get-ADUser
.PARAMETER isOU
CName is for an OU. Set to $True to enable. If enabled script will call all users in the OU and subfolders.
.PARAMETER SearchSubs
Search Subfolders of OU as well. Set to $False to disable. Enabled by default. If disabled, search will return only users in selected OU.
.PARAMETER Props
Specify the properties to query from AD. Default is all available. Specific properties can be requested through comma seperated list. See Example 3.
.PARAMETER Export
Export the results to a CSV File. Set to $True to enable. If enabled script will export only to CSV with no screen output. This is better for querying a lot of properties.
.PARAMETER Filepath
Filepath to Export CSV to. Default is "C:\Temp\User Export.csv"
.EXAMPLE
Get an individual user.
.\Get-CNUser.ps1 -CNAME "Domain.com/Headoffice/Users/User name"
.EXAMPLE
Get all users in Sales Department OU and subfolders.
.\Get-CNUser.ps1 -CNAME "Domain.com/Offices/Sales Department/Users" -isOU $True -SearchSubs $True
.EXAMPLE
Get an individual with specific properties.
.\Get-CNUser.ps1 -CNAME "Domain.com/Users/User name" -Props Name, Displayname, employeeID, Samaccountname, Mail
.EXAMPLE
Get all users in root Finance Department OU only and Export Results to CSV.
.\Get-CNUser.ps1 -CNAME "Domain.com/Headoffice/Departments/Finance/Users" -isOU $True -SearchSubs $False -Export $True -Filepath "C:\Rootfolder\Subfolder\results.csv"
#>
# Define Parameters
param(
[parameter(Position=0,Mandatory=$true,ValueFromPipeline=$false,HelpMessage='Canonical Name')][string]$CNAME,
[parameter(Mandatory=$false,ValueFromPipeline=$false,HelpMessage='Is this an OU ($True/$False)')][bool]$isOU=$False,
[parameter(Mandatory=$false,ValueFromPipeline=$false,HelpMessage='Search Subfolders of OU ($True/$False)')][bool]$SearchSubs=$True,
[parameter(Mandatory=$false,ValueFromPipeline=$false,HelpMessage='Export Report to CSV')][bool]$Export=$False,
[parameter(Mandatory=$false,ValueFromPipeline=$false,HelpMessage='FilePath to Exported Report')][String]$Filepath="C:\Temp\User Export.csv",
[parameter(Mandatory=$false,ValueFromPipeline=$false,HelpMessage='Required Properties (Comma Seperated)')][string[]]$Props="*"
)
# Import Modules
Import-Module ActiveDirectory
# Define Functions
Function readParams {
IF ($isOU) {
getOU
}
ELSE {
getUser
}
}
Function getOU {
$Working = $CNAME.split('/')
$DNWorking = $Working[0]
$DNWorking = $DNWorking.split('.')
$OUWorking = $Working[1..500]
[array]::Reverse($OUWorking)
$OUWorking = $OUWorking | foreach {"OU=$_"}
$DNWorking = $DNWorking | foreach {"DC=$_"}
[String]$DName = ""
$OUWorking | foreach {$DName = $DName + $_ + ","}
$DNWorking | foreach {$DName = $DName + $_ + ","}
$DName = $DName.Trim(",")
IF ($Export) {
GetADUserExport
}
ELSE {
GetADUserDisp
}
}
Function getUser {
$Working = $CNAME.split('/')
$DNWorking = $Working[0]
$DNWorking = $DNWorking.split('.')
$OUWorking = $Working[1..500]
[array]::Reverse($OUWorking)
$CNWorking = $OUWorking[0]
$OUWorking = $OUWorking[1..500]
$CNWorking = $CnWorking | foreach {"CN=$_"}
$OUWorking = $OUWorking | foreach {"OU=$_"}
$DNWorking = $DNWorking | foreach {"DC=$_"}
[String]$DName = ""
$CNWorking | foreach {$DName = $DName + $_ + ","}
$OUWorking | foreach {$DName = $DName + $_ + ","}
$DNWorking | foreach {$DName = $DName + $_ + ","}
$DName = $DName.Trim(",")
IF ($Export) {
GetADUserExport
}
ELSE {
GetADUserDisp
}
}
Function getADUserDisp {
IF ($isOU) {
IF ($SearchSubs) {
Get-ADUser -Filter * -SearchBase $DName -SearchScope 2 -Properties $Props | Select $Props
}
ELSE {
Get-ADUser -Filter * -SearchBase $DName -SearchScope 1 -Properties $Props | Select $Props
}
}
Else {
Get-ADUser $Dname -Properties $Props | Select $Props
}
}
Function getADUserExport {
IF ($isOU) {
IF ($SearchSubs) {
Get-ADUser -Filter * -SearchBase $DName -SearchScope 2 -Properties $Props | Select $Props | Export-CSV $FilePath -NoTypeInformation
}
ELSE {
Get-ADUser -Filter * -SearchBase $DName -SearchScope 1 -Properties $Props | Select $Props | Export-CSV $FilePath -NoTypeInformation
}
}
Else {
Get-ADUser $Dname -Properties $Props | Select $Props | Export-CSV $FilePath -NoTypeInformation
}
}
readParams
.\Get-CNUser.ps1 -CNAME "Domain.com/Headoffice/Departments/Finance/Users" -isOU $True -SearchSubs $True -Export $True -Filepath "C:\Rootfolder\Subfolder\results.csv" -Props Name, Displayname, employeeID, Samaccountname, Mail
You also will not be able filter on CanonicalName either because it is a Constructed Attribue and it is not part of the default properties. So i am not exactly sure whatyou want to do with the CanonicalName.
I have also checked Get-adobject and it also does not accept CanonicalName and a searchable attribute.
Will.