Scripts/Mdbc.ArgumentCompleters.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
<#
.Synopsis Argument completers for Mdbc commands. .Description The script adds completers for: Connect-Mdbc -DatabaseName .. -CollectionName .. Add-MdbcData New-MdbcData Export-MdbcData -Property .., .. Get-MdbcDatabase Get-MdbcCollection Remove-MdbcDatabase Remove-MdbcCollection Rename-MdbcCollection [-Name] .. How to use: * PowerShell v5 native Invoke Mdbc.ArgumentCompleters.ps1, e.g. in a profile. * TabExpansion2.ps1 https://www.powershellgallery.com/packages/TabExpansion2 Put Mdbc.ArgumentCompleters.ps1 to the path. Or invoke after TabExpansion2.ps1, e.g. in a profile. * TabExpansionPlusPlus https://github.com/lzybkr/TabExpansionPlusPlus Put Mdbc.ArgumentCompleters.ps1 to TabExpansionPlusPlus directory. Or invoke after importing the module, e.g. in a profile. NOTE: Not all completers work in TabExpansionPlusPlus. #> Register-ArgumentCompleter -CommandName Connect-Mdbc -ParameterName DatabaseName -ScriptBlock { param($commandName, $parameterName, $wordToComplete, $commandAst, $boundParameters) if (!($ConnectionString = $boundParameters['ConnectionString'])) { $ConnectionString = '.' } @(Connect-Mdbc $ConnectionString *) -like "$wordToComplete*" | .{process{ New-Object System.Management.Automation.CompletionResult $_, $_, 'ParameterValue', $_ }} } Register-ArgumentCompleter -CommandName Get-MdbcDatabase, Remove-MdbcDatabase -ParameterName Name -ScriptBlock { param($commandName, $parameterName, $wordToComplete, $commandAst, $boundParameters) if (!($myClient = $boundParameters['Client'])) { $myClient = $Client } @( foreach($_ in Get-MdbcDatabase -Client $myClient) { $_.DatabaseNamespace.DatabaseName } ) -like "$wordToComplete*" | .{process{ New-Object System.Management.Automation.CompletionResult $_, $_, 'ParameterValue', $_ }} } Register-ArgumentCompleter -CommandName Connect-Mdbc -ParameterName CollectionName -ScriptBlock { param($commandName, $parameterName, $wordToComplete, $commandAst, $boundParameters) if (!($ConnectionString = $boundParameters['ConnectionString'])) { $ConnectionString = '.' } if (!($DatabaseName = $boundParameters['DatabaseName'])) { $DatabaseName = 'test' } @(Connect-Mdbc $ConnectionString $DatabaseName *) -like "$wordToComplete*" | .{process{ New-Object System.Management.Automation.CompletionResult $_, $_, 'ParameterValue', $_ }} } Register-ArgumentCompleter -CommandName Get-MdbcCollection, Remove-MdbcCollection, Rename-MdbcCollection -ParameterName Name -ScriptBlock { param($commandName, $parameterName, $wordToComplete, $commandAst, $boundParameters) if (!($myDatabase = $boundParameters['Database'])) { $myDatabase = $Database } @( foreach($_ in Get-MdbcCollection -Database $myDatabase) { $_.CollectionNamespace.CollectionName } ) -like "$wordToComplete*" | .{process{ New-Object System.Management.Automation.CompletionResult $_, $_, 'ParameterValue', $_ }} } Register-ArgumentCompleter -CommandName Add-MdbcData, New-MdbcData, Export-MdbcData -ParameterName Property -ScriptBlock { $private:commandName, $private:parameterName, $private:wordToComplete, $private:commandAst, $private:boundParameters = $args $private:data = $boundParameters['InputObject'] if (!$data) { $private:ast = $commandAst.Parent if ($ast -isnot [System.Management.Automation.Language.PipelineAst] -or $ast.PipelineElements.Count -ne 2) { return } try { $data = & ([scriptblock]::Create($ast.PipelineElements[0])) } catch { return } } $keys = [System.Collections.Generic.HashSet[object]]@() $pattern = "$wordToComplete*" foreach($_ in $data) { if ($_ -is [System.Collections.IDictionary]) { foreach($_ in $_.Keys -like $pattern) { $null = $keys.Add($_) } } elseif ($_) { foreach($_ in $_.PSObject.Properties.Match($pattern)) { $null = $keys.Add($_.Name) } } } $keys | Sort-Object -CaseSensitive | .{process{ New-Object System.Management.Automation.CompletionResult $_, $_, 'ParameterValue', $_ }} } |