-
-
Notifications
You must be signed in to change notification settings - Fork 179
Adds AboutFiltering topic to the ActiveDirectory module #343
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
Merged
vexx32
merged 16 commits into
vexx32:master
from
indented-automation:AddsActiveDirectoryModule
Jan 14, 2020
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
608d9ff
Building AD koans
indented-automation 0c9949d
Defining format
indented-automation 2b2f0d6
Adds AD filter koans
indented-automation 65320e3
Merge branch 'master' into AddsActiveDirectoryModule
indented-automation f95f0db
Fixes path. Adds index entry.
indented-automation 56ad3fb
Fixes typo
indented-automation cef7308
Merge branch 'master' into AddsActiveDirectoryModule
indented-automation a308b00
Update PSKoans/Koans/Modules/ActiveDirectory/Introduction/AboutFilter…
indented-automation dd300d7
Update PSKoans/Koans/Modules/ActiveDirectory/Introduction/AboutFilter…
indented-automation e0e19d2
Update PSKoans/Koans/Modules/ActiveDirectory/Introduction/AboutFilter…
indented-automation 421ed79
Update PSKoans/Koans/Modules/ActiveDirectory/Introduction/AboutFilter…
indented-automation f542e08
Update PSKoans/Koans/Modules/ActiveDirectory/Introduction/AboutFilter…
indented-automation f6cdf2f
Merge branch 'master' into AddsActiveDirectoryModule
indented-automation 3de13cd
Fixes typo
indented-automation 51257c3
Adds Requires directive
indented-automation 6a221fc
Update PSKoans/Koans/Modules/ActiveDirectory/Introduction/AboutFilter…
indented-automation File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
PSKoans/Koans/Modules/ActiveDirectory/Introduction/AboutFiltering.Koans.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| #Requires -Modules ActiveDirectory | ||
| using module PSKoans | ||
| [Koan(Position = 101, Module = 'ActiveDirectory')] | ||
| param() | ||
| <# | ||
| About Filtering | ||
|
|
||
| The ActiveDirectory module offers support for two different styles of filtering via two different parameters. | ||
|
|
||
| The Filter parameter supports a PowerShell-style expression as the filter. The expression is converted into | ||
| an LDAP filter by the ActiveDirectory module before being sent to the AD web services gateway. | ||
|
|
||
| It is possible, although not easy, to run the filter converter independently. The function in the following | ||
| gist does so: | ||
|
|
||
| https://gist.github.com/indented-automation/66e07bc76fdb6cf0be6743ed0b24575c | ||
|
|
||
| PowerShell-style filters have a number of quirks and occasionally do not behave as they should. Any failures | ||
| are assumed to be problems caused when converting the filter. | ||
| #> | ||
| Describe 'About Filtering' { | ||
|
|
||
| Context 'PowerShell-style filtering' { | ||
| <# | ||
| The Filter parameter expects a string. Several of the examples use a ScriptBlock to write the filter. | ||
|
|
||
| Get-ADUser -Filter { name -eq "Dave" } | ||
|
|
||
| The filter above can also be written using single quotes, a literal or non-expanding string. The two filters | ||
| are identical except in appearance | ||
|
|
||
| Get-ADUser -Filter 'name -eq "Dave"' | ||
|
|
||
| Using a script block can be misleading. It implies support for more complex expressions which is not present. | ||
| #> | ||
|
|
||
| It 'often uses script blocks in examples' { | ||
| # Fill in your username to show that both filters return the same result. | ||
|
|
||
| Get-ADUser -Filter { samAccountName -eq '____' } | | ||
| Select-Object -ExpandProperty SamAccountName | | ||
| Should -Be $env:USERNAME | ||
|
|
||
| Get-ADUser -Filter 'samAccountName -eq "____"' | | ||
| Select-Object -ExpandProperty SamAccountName | | ||
| Should -Be $env:USERNAME | ||
| } | ||
|
|
||
| It 'expands variables in literal strings' { | ||
| <# | ||
| Variable expansion in filters is a feature of the ActiveDirectory module. | ||
|
|
||
| Sub-expressions are not supported. Values from providers, such as the Environment provider, for example | ||
| $env:USERNAME, are not supported. | ||
|
|
||
| The variable below is in single quotes. PowerShell will not expand this variable, the ActiveDirectory | ||
| module will. | ||
| #> | ||
|
|
||
| $username = '____' | ||
| Get-ADUser -Filter 'samAccountName -eq $username' | | ||
| Select-Object -ExpandProperty SamAccountName | | ||
| Should -Be $env:USERNAME | ||
| } | ||
|
|
||
| It 'is often useful to let the Active Directory module expand a variable' { | ||
| <# | ||
| The AD module uses a mixture of hard-coded data and information from the AD schema to figure out the | ||
| format an attribute expects in a filter. | ||
|
|
||
| For example, if a filter is written based on a SID the ActiveDirectory module will happily convert the | ||
| SID into an appropriate format. The author of the query no longer needs to care about exactly | ||
| how the filter should be written. | ||
|
|
||
| The same approach can be used for dates, GUIDs, byte arrays, and so on. | ||
| #> | ||
|
|
||
| $username = '____' | ||
| # The statement below will only work on Windows | ||
| $sid = [System.Security.Principal.WindowsIdentity]::GetCurrent().User | ||
| Get-ADUser -Filter 'objectSID -eq $sid' | | ||
| Select-Object -ExpandProperty SamAccountName | | ||
| Should -Be $username | ||
|
|
||
| <# | ||
| The SID will be converted into an appropriate format for an LDAP filter. Similar to the example below: | ||
|
|
||
| (objectSid=\01\05\00\00\00\00\00\05\15\00\00\00\11\22\33\44\55\66\77\88\99\AA\BB\CC\DD\EE\FF\00) | ||
| #> | ||
| } | ||
|
|
||
| It 'can expand properties of objects from variables' { | ||
| <# | ||
| Filters do not support sub-expressions but the Filter parameter does support property expansion. | ||
|
|
||
| The catch is that the ActiveDirectory module can only expand properties of .NET types. It cannot read | ||
| NoteProperty values from PSCustomObject, it cannot read NoteProperty members added by Add-Member. | ||
|
|
||
| Get-Item will return a System.IO.DirectoryInfo object if the path is a directory. The DirectoryInfo | ||
| object has a Name property which can be expanded. | ||
| #> | ||
|
|
||
| $homeDirectory = Get-Item -Path '____' | ||
| Get-ADUser -Filter 'samAccountName -eq $homeDirectory.Name' | | ||
| Select-Object -ExpandProperty SamAccountName | | ||
| Should -Be $env:USERNAME | ||
| } | ||
|
|
||
| It 'can sometimes be better to let PowerShell expand variables' { | ||
| <# | ||
| In some cases it can be better to treat the filter as a string and let PowerShell expand any | ||
| variable values. | ||
|
|
||
| If PowerShell is expanding values, quotes must be added around the value in the filter. | ||
| #> | ||
|
|
||
| $username = '____' | ||
| Get-ADUser -Filter "samAccountName -eq '$username'" | | ||
| Select-Object -ExpandProperty SamAccountName | | ||
| Should -Be $env:USERNAME | ||
| } | ||
|
|
||
| It 'has aliases for certain attributes' { | ||
| <# | ||
| The ActiveDirectory module provides aliases for a large number of attributes. In some cases this is | ||
| just convenient. In others, it is used to hide complexity. | ||
|
|
||
| Enabled is a commonly used alias which masks a query against the userAccountControl attribute. | ||
| #> | ||
|
|
||
| $username = '____' | ||
| Get-ADUser -Filter 'samAccountName -eq $username -and Enabled -eq $true' | | ||
| Select-Object -ExpandProperty SamAccountName | | ||
| Should -Be $env:USERNAME | ||
|
|
||
| <# | ||
| Whether or not an account is enabled is described by a single flag in the userAccountControl attribute. | ||
|
|
||
| The query used above can also be written as shown below. | ||
| #> | ||
|
|
||
|
|
||
| Get-ADUser -Filter 'samAccountName -eq $username -and -not userAccountControl -band 2' | | ||
| Select-Object -ExpandProperty SamAccountName | | ||
| Should -Be $env:USERNAME | ||
|
|
||
| <# | ||
| In both cases, the filter is converted to the following LDAP filter. | ||
|
|
||
| (&(samAccountName=<Username>)(!(userAccountControl:1.2.840.113556.1.4.803:=2))) | ||
|
|
||
| Other common aliases include LastLogonDate, an alias for lastLogonTimestamp. | ||
|
|
||
| The possible aliases can be read from the ActiveDirectory module although they are not easily | ||
| available. The function in in the gist below does so: | ||
|
|
||
| https://gist.github.com/indented-automation/629a00167a74e9cb3329eabd6662bc0c | ||
| #> | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.