问题
I'm creating a PS function with 2 parameters: $server & $database. I need the $database parameter to be auto-populated (dynamic validation set), depending on the first parameter ($server)
I got most of the code from here
However it is NOT working. What am I doing wrong here? Any insight is greatly appreciated. Thank you.
function Get-databases {
[CmdletBinding()]
Param(
# Any other parameters can go here
[Parameter(Mandatory)][string] $Server
)
DynamicParam {
# Set the dynamic parameters' name
$ParameterName = 'Database'
# Create the dictionary
$RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
# Create the collection of attributes
$AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
# Create and set the parameters' attributes
$ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
$ParameterAttribute.Mandatory = $true
$ParameterAttribute.Position = 1
# Add the attributes to the attributes collection
$AttributeCollection.Add($ParameterAttribute)
# Generate and set the ValidateSet
$arrSet = (Invoke-Sqlcmd -ServerInstance $server -query 'select name from sys.databases order by 1' -ConnectionTimeout 60 -QueryTimeout 99999).name
$ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet)
# Add the ValidateSet to the attributes collection
$AttributeCollection.Add($ValidateSetAttribute)
# Create and return the dynamic parameter
$RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $AttributeCollection)
$RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter)
return $RuntimeParameterDictionary
}
begin {
# Bind the parameter to a friendly variable
$db = $PsBoundParameters[$ParameterName]
}
process {
# Your code goes here
$db
}
}
回答1:
If you have Invoke-SqlCmd
in DynamicParam ValidateSet attribute, your tab completion is going to execute the whole Invoke-SqlCmd
for validation , this is very expensive w.r.t performance.
You can give some xyz
value to -DataBase without using Tab completion, you will see it validating the Input, but will take little time as it will execute the Invoke-SqlCmd
for the validation .
So I would advice not to go with DynamicParams or to Avoid Validation within DynamicParam, you can have an explicit validation in Begin
block.
回答2:
Here's the final code:
import-module sqlps
function Get-Database {
<#
.SYNOPSIS
Dynamic validationset of databases
#>
#Requires -Version 3.0
[CmdletBinding()]
Param (
[Parameter(Mandatory)][string]$server
)
DynamicParam {
$newparams = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
$paramattributes = New-Object System.Management.Automation.ParameterAttribute
$paramattributes.ParameterSetName = "__AllParameterSets"
$paramattributes.Mandatory = $false
$systemdbs = @("master", "msdb", "model", "SSIS", "distribution")
$srv = New-Object 'Microsoft.SqlServer.Management.SMO.Server' "$server"
$dblist = ($srv.Databases).name | Where-Object { $systemdbs -notcontains $_ }
$argumentlist = @()
foreach ($db in $dblist) {
$argumentlist += [Regex]::Escape($db)
}
$validationset = New-Object System.Management.Automation.ValidateSetAttribute -ArgumentList $argumentlist
$combinedattributes = New-Object -Type System.Collections.ObjectModel.Collection[System.Attribute]
$combinedattributes.Add($paramattributes)
$combinedattributes.Add($validationset)
$Databases = New-Object -Type System.Management.Automation.RuntimeDefinedParameter("Databases", [String[]], $combinedattributes)
$newparams.Add("Databases", $Databases)
return $newparams
}
process {
$UserDb = $psboundparameters.Databases
Write-Host "You picked: $UserDb"
}
}
Clear-Host
Get-Database -server 'YourServerName' -Databases 'DynamicallyPopulatedDatabases'
来源:https://stackoverflow.com/questions/46977686/getting-a-dynamic-database-parameter-on-a-sql-server