Skip to content

Instantly share code, notes, and snippets.

@bielawb
Created October 31, 2016 23:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bielawb/6bee4f9347e5ad353a0b5f0992d1cd9d to your computer and use it in GitHub Desktop.
Save bielawb/6bee4f9347e5ad353a0b5f0992d1cd9d to your computer and use it in GitHub Desktop.
Snippet with complex completer
[ArgumentCompleter({
param(
[string]$commandName,
[string]$parameterName,
[string]$wordToComplete,
[System.Management.Automation.Language.CommandAst]$commandAst,
[System.Collections.IDictionary]$fakeBoundParameters
)
$types = @{
And = '1.2.840.113556.1.4.803', 'LDAP_MATCHING_RULE_BIT_AND'
Or = '1.2.840.113556.1.4.804', 'LDAP_MATCHING_RULE_BIT_OR'
Chain = '1.2.840.113556.1.4.1941', 'LDAP_MATCHING_RULE_IN_CHAIN'
}
switch -Regex ($wordToComplete) {
'^(groupType|userAccountControl):$' {
foreach ($key in 'And', 'Or') {
$line = "$_$($types.$key[0]):="
$attribute = $Matches[1]
[System.Management.Automation.CompletionResult]::new(
$line,
$line,
[System.Management.Automation.CompletionResultType]::ParameterValue,
"Matching rule $($types.$key[1]) for attribute $attribute"
)
}
}
'^(member|memberof):$' {
$line = "$_$($types.Chain[0]):="
$attribute = $Matches[1]
[System.Management.Automation.CompletionResult]::new(
$line,
$line,
[System.Management.Automation.CompletionResultType]::ParameterValue,
"Matching rule $($types.Chain[1]) for attribute $attribute"
)
}
'^(?!.*=)' {
$searcher = [ADSISearcher]::new(
[ADSI]'LDAP://CN=Schema,CN=Configuration,DC=monad,DC=net',
"(&(objectClass=attributeSchema)(lDAPDisplayName=$wordToComplete*))",
@(
'lDAPDisplayName'
'name'
)
)
$searcher.FindAll() | ForEach-Object {
$attribute = -join $_.Properties['lDAPDisplayName'][0]
$name = -join $_.Properties['name'][0]
[System.Management.Automation.CompletionResult]::new(
$attribute,
$attribute,
[System.Management.Automation.CompletionResultType]::ParameterValue,
"Attribute $attribute ($name)"
)
}
}
}
})]
[string[]]$Filter = '(name=*)',
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment