Skip to content

implement LDAPFilter (fix #62) #118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 71 additions & 28 deletions src/public/Get-ADSIComputer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ function Get-ADSIComputer
System.DirectoryService.AccountManagement.IdentityType
https://msdn.microsoft.com/en-us/library/bb356425(v=vs.110).aspx

.PARAMETER LDAPFilter
A custom LDAP Filter string to search for computer objects.
May not be used together with -Identity.

.PARAMETER LDAPPath
The directory path to search inside when using an LDAPFilter.

.PARAMETER Credential
Specifies alternative credential
By default it will use the current user windows credentials.
Expand Down Expand Up @@ -56,8 +63,14 @@ function Get-ADSIComputer
https://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.computerprincipal(v=vs.110).aspx
#>
[CmdletBinding(DefaultParameterSetName = "All")]
param ([Parameter(Mandatory = $true, Position = 0, ParameterSetName = "Identity")]
param (
[Parameter(Mandatory = $true, Position = 0, ParameterSetName = "Identity")]
[string]$Identity,

[Parameter(ParameterSetName = "LDAPFilter")]
[string]$LDAPFilter,
[Parameter(ParameterSetName = "LDAPFilter")]
[string]$LDAPPath,

[Alias("RunAs")]
[System.Management.Automation.PSCredential]
Expand All @@ -69,40 +82,70 @@ function Get-ADSIComputer
begin
{
$FunctionName = (Get-Variable -Name MyInvocation -Scope 0 -ValueOnly).Mycommand
Add-Type -AssemblyName System.DirectoryServices.AccountManagement

# Create Context splatting
$ContextSplatting = @{ ContextType = "Domain" }

if ($PSBoundParameters['Credential'])
{
Write-Verbose "[$FunctionName] Found Credential Parameter"
$ContextSplatting.Credential = $Credential
}
if ($PSBoundParameters['DomainName'])
{
Write-Verbose "[$FunctionName] Found DomainName Parameter"
$ContextSplatting.DomainName = $DomainName
switch ($PsCmdlet.ParameterSetName) {
'LDAPFilter' {
# Create Context splatting
$ContextSplatting = @{}

if ($PSBoundParameters['Credential'])
{
Write-Verbose "[$FunctionName] Found Credential Parameter"
$ContextSplatting.Credential = $Credential
}
if ($PSBoundParameters['LDAPPath'])
{
Write-Verbose "[$FunctionName] Found LADPPath Parameter"
$ContextSplatting.Path = $LDAPPath
}

$SearchRoot = New-ADSIDirectoryEntry @ContextSplatting
}
default {
Add-Type -AssemblyName System.DirectoryServices.AccountManagement

# Create Context splatting
$ContextSplatting = @{ ContextType = "Domain" }

if ($PSBoundParameters['Credential'])
{
Write-Verbose "[$FunctionName] Found Credential Parameter"
$ContextSplatting.Credential = $Credential
}
if ($PSBoundParameters['DomainName'])
{
Write-Verbose "[$FunctionName] Found DomainName Parameter"
$ContextSplatting.DomainName = $DomainName
}

$Context = New-ADSIPrincipalContext @ContextSplatting
}
}

$Context = New-ADSIPrincipalContext @ContextSplatting

}
process
{
try
{
if ($Identity)
{
[System.DirectoryServices.AccountManagement.ComputerPrincipal]::FindByIdentity($Context, $Identity)
}
else
{
$ComputerPrincipal = New-object -TypeName System.DirectoryServices.AccountManagement.ComputerPrincipal -ArgumentList $Context
$Searcher = new-object -TypeName System.DirectoryServices.AccountManagement.PrincipalSearcher
$Searcher.QueryFilter = $ComputerPrincipal

$Searcher.FindAll()
switch ($PsCmdlet.ParameterSetName) {
'Identity' {
[System.DirectoryServices.AccountManagement.ComputerPrincipal]::FindByIdentity($Context, $Identity)
}
'LDAPFilter' {
if ($LDAPFilter) {
$LDAPFilter = "(&(objectClass=computer)$LDAPFilter)"
$Searcher = New-Object -TypeName System.DirectoryServices.DirectorySearcher -ArgumentList $SearchRoot, $LDAPFilter
} else {
$Searcher = New-Object -TypeName System.DirectoryServices.DirectorySearcher -ArgumentList $SearchRoot, 'objectClass=computer'
}
$Searcher.FindAll() | Select-Object -ExpandProperty Properties
}
default {
$ComputerPrincipal = New-object -TypeName System.DirectoryServices.AccountManagement.ComputerPrincipal -ArgumentList $Context
$Searcher = new-object -TypeName System.DirectoryServices.AccountManagement.PrincipalSearcher
$Searcher.QueryFilter = $ComputerPrincipal

$Searcher.FindAll()
}
}
}
catch
Expand Down