PowerShell - 有没有办法创建一种包含多个验证集参数的 'alias'?
PowerShell - Is there a way to create a sort of 'alias' that contains multiple parameter of a validateset?
下午好,
我最近创建了一个 PowerShell 脚本,用于在 运行将它们安装到 vCenters/ESXi 主机之前自动化和测试 PowerCLI 脚本。
目前,我使用验证集让用户选择 server/cluster 他们想要执行脚本:
param(
[string]$script = "help",
[ValidateSet("localhost", "Server1", "Server2")]
[string]$server,
[ValidateSet("DC0_C0", "DC0_C1", "DC0_C2", "Customer1")]
[string[]]$cluster,
[switch]$help
)
问题在于,有时当您有大客户时,他们可能有多个集群,只需键入“customer1”并将脚本 运行 放在 cluster1/2 上会更方便,并且4 而不是手动键入所有集群:
./viexec vminfo localhost Cluster1,Cluster3,Cluster10
./viexec vminfo localhost Customer1 #And have the script run automatically on all the right clusters.
我已经尝试过使用 if 检查变量 $cluster 的值,如果它等于“Customer1”,则用适当的簇替换他的值,但我觉得这个解决方案并不优雅。而且很难 configure/maintain,因为用户需要修改代码,所以如果可以从外部配置 file/user 输入创建这些参数就更好了。
我还想知道是否可以从 file/csv 中检索验证集的参数以避免用户自定义主脚本。ps1,而是简单地 replace/write他们的服务器和集群在填充 validateset 参数的 .CSV 中。
希望清楚..
此致,
亚历山德罗
这是一个概念证明,它 实现了一个 Invoke-VI
函数 ,它展示了所需的制表符完成/验证行为,由 customer- JSON 文件 中定义的到集群的映射(有关基于 CSV 的解决方案,请参阅 )。
注:
JSON 被选为 CSV 的更灵活的替代方案,因为后者需要每个客户固定数量的集群(列)。最终,像 YAML 或 INI 文件这样的东西对最终用户来说会更方便,但 PowerShell 目前缺乏对这些格式的内置支持;参见 GitHub proposal #3607 and GitHub proposal #9035; however, third-party modules are available in the PowerShell Gallery, such as powershell-yaml and PSIni。
实现了 函数 而不是脚本,因为在使用所需的制表符完成和验证功能声明参数之前需要准备步骤。这意味着,您需要 dot-source it (. ./viexec.ps1
) and then invoke the Invoke-VI
function. Alternatively, define the function as part of an (auto-loading) module.
而不是 运行 您的 *.ps1
脚本 直接
首先,创建一个示例 JSON 文件,其中包含 2 个客户及其关联的集群:
# Create a sample JSON file that maps customer names to clusters.
# This will serve as the basis for tab-completion / argument validation.
@'
{
"Customer1": [ "DC0_C0", "DC0_C1", "DC0_C2" ],
"Customer2": [ "DC1_C0", "DC1_C1" ]
}
'@ > CustomerToClusters.json
基于它的代码:
# Parse the JSON file, assumed to be located in the same dir.
# as this script.
$customerToClusterMapping = ConvertFrom-Json (Get-Content -Raw $PSScriptRoot/CustomerToClusters.json)
# Build the list of customer names and cluster names across all customers.
[string[]] $customerNames = $customerToClusterMapping.psobject.Properties.Name
[string[]] $allClusters = $customerToClusterMapping.psobject.Properties.Value
function Invoke-VI {
param(
# Tab-complete cluster names as well as customer names.
[ArgumentCompleter({ param($cmd, $param, $wordToComplete) ($customerNames + $allClusters) -like "$wordToComplete*" })]
# Ensure that only known customer / cluster names were specified.
# NOTE: If validation fails, the (default) error message is unhelpful.
# Unfortunately, this cannot be helped in *Windows PowerShell*, but in
# PowerShell (Core) 7+, you can add an `ErrorMessage` property:
# [ValidateScript({ $_ -in ($customerNames + $allClusters) }, ErrorMessage = 'Unknown customer/cluster name: {0}')]
[ValidateScript({ $_ -in ($customerNames + $allClusters) })]
[string[]] $Cluster
)
# Resolve the specified cluster arguments and remove duplicates from the
# resulting list of cluster-only names.
$resolvedClusters = $Cluster | ForEach-Object {
# If a customer name was specified, eturn the list of clusters for the specified customer.
if ($_ -in $customerNames) { $customerToClusterMapping.$_ }
else { $_ }
} | Select-Object -Unique
"Specified or implied clusters: $resolvedClusters"
}
示例调用,在对上面的代码进行点源之后:
PS> Invoke-VI Customer1 # Customer1 was tab-completed.
Specified or implied clusters: DC0_C0 DC0_C1 DC0_C2
请注意客户名称是如何解析到关联的集群的。
这是 的变体,其中:
根据要求使用 CSV 文件作为数据源。
显示了一个更简单的制表符完成和验证组合解决方案,它基于传递一个 class 来实现 System.Management.Automation.IValidateSetValuesGenerator
interface to the ValidateSet
attribute (gratefully adapted from this answer), but note that it requires PowerShell (Core) 7+.
首先,创建一个包含 2 个客户及其关联集群的示例 CSV 文件:
# Create a sample CSV file that maps customer names to clusters.
# This will serve as the basis for tab-completion / argument validation.
# IMPORTANT: Be sure that you have enough headers (colum names) to cover
# the maximum number of columns values.
@'
Customer,Cluster1,Cluster2,Cluster3,Cluster4,Cluster5
Customer1,DC0_C0,DC0_C1,DC0_C2
Customer2,DC1_C0,DC1_C1
'@ > CustomersToClusters.csv
基于它构建的代码:
# Parse the CSV file, assumed to be located in the same dir.
# as this script.
$csvRows = Import-Csv $PSScriptRoot/CustomersToClusters.csv
# Build a hashtable of customer names mapped to their associated clusters.
$colCount = $csvRows[0].psobject.Properties.Name.Count
$htCustomersToClusters = [ordered] @{}
foreach ($row in $csvRows) {
$htCustomersToClusters[$row.Customer] = $row.psobject.Properties.Value[1..($colCount-1)] -ne $null
}
# Build an array of all customer and cluster names.
[string[]] $allCustomersAndClusters = $htCustomersToClusters.Keys + $htCustomersToClusters.GetEnumerator().ForEach({ $_.Value })
# Define the custom class that implements the System.Management.Automation.IValidateSetValuesGenerator
# interface, to be passed to the [ValidateSet()] attribute.
class CustomerAndClusters : System.Management.Automation.IValidateSetValuesGenerator {
[string[]] GetValidValues() { return $script:allCustomersAndClusters }
}
function Invoke-VI {
param(
# Provide tab-completion and validation based on the values
# returned by a [CustomersAndClusters] instance's .GetValidValues() call.
[ValidateSet([CustomerAndClusters])]
[string[]] $Cluster
)
# Resolve the specified cluster arguments and remove duplicates from the
# resulting list of cluster-only names.
$resolvedClusters = $Cluster | ForEach-Object {
# If a customer name was specified, resolve it to the list of associated clusters.
if ($customerClusters = $htCustomersToClusters[$_]) { $customerClusters }
else { $_ }
} | Select-Object -Unique
"Specified or implied clusters: $resolvedClusters"
}
下午好,
我最近创建了一个 PowerShell 脚本,用于在 运行将它们安装到 vCenters/ESXi 主机之前自动化和测试 PowerCLI 脚本。
目前,我使用验证集让用户选择 server/cluster 他们想要执行脚本:
param(
[string]$script = "help",
[ValidateSet("localhost", "Server1", "Server2")]
[string]$server,
[ValidateSet("DC0_C0", "DC0_C1", "DC0_C2", "Customer1")]
[string[]]$cluster,
[switch]$help
)
问题在于,有时当您有大客户时,他们可能有多个集群,只需键入“customer1”并将脚本 运行 放在 cluster1/2 上会更方便,并且4 而不是手动键入所有集群:
./viexec vminfo localhost Cluster1,Cluster3,Cluster10
./viexec vminfo localhost Customer1 #And have the script run automatically on all the right clusters.
我已经尝试过使用 if 检查变量 $cluster 的值,如果它等于“Customer1”,则用适当的簇替换他的值,但我觉得这个解决方案并不优雅。而且很难 configure/maintain,因为用户需要修改代码,所以如果可以从外部配置 file/user 输入创建这些参数就更好了。
我还想知道是否可以从 file/csv 中检索验证集的参数以避免用户自定义主脚本。ps1,而是简单地 replace/write他们的服务器和集群在填充 validateset 参数的 .CSV 中。
希望清楚..
此致, 亚历山德罗
这是一个概念证明,它 实现了一个 Invoke-VI
函数 ,它展示了所需的制表符完成/验证行为,由 customer- JSON 文件 中定义的到集群的映射(有关基于 CSV 的解决方案,请参阅
注:
JSON 被选为 CSV 的更灵活的替代方案,因为后者需要每个客户固定数量的集群(列)。最终,像 YAML 或 INI 文件这样的东西对最终用户来说会更方便,但 PowerShell 目前缺乏对这些格式的内置支持;参见 GitHub proposal #3607 and GitHub proposal #9035; however, third-party modules are available in the PowerShell Gallery, such as powershell-yaml and PSIni。
实现了 函数 而不是脚本,因为在使用所需的制表符完成和验证功能声明参数之前需要准备步骤。这意味着,您需要 dot-source it (
而不是 运行 您的. ./viexec.ps1
) and then invoke theInvoke-VI
function. Alternatively, define the function as part of an (auto-loading) module.*.ps1
脚本 直接
首先,创建一个示例 JSON 文件,其中包含 2 个客户及其关联的集群:
# Create a sample JSON file that maps customer names to clusters.
# This will serve as the basis for tab-completion / argument validation.
@'
{
"Customer1": [ "DC0_C0", "DC0_C1", "DC0_C2" ],
"Customer2": [ "DC1_C0", "DC1_C1" ]
}
'@ > CustomerToClusters.json
基于它的代码:
# Parse the JSON file, assumed to be located in the same dir.
# as this script.
$customerToClusterMapping = ConvertFrom-Json (Get-Content -Raw $PSScriptRoot/CustomerToClusters.json)
# Build the list of customer names and cluster names across all customers.
[string[]] $customerNames = $customerToClusterMapping.psobject.Properties.Name
[string[]] $allClusters = $customerToClusterMapping.psobject.Properties.Value
function Invoke-VI {
param(
# Tab-complete cluster names as well as customer names.
[ArgumentCompleter({ param($cmd, $param, $wordToComplete) ($customerNames + $allClusters) -like "$wordToComplete*" })]
# Ensure that only known customer / cluster names were specified.
# NOTE: If validation fails, the (default) error message is unhelpful.
# Unfortunately, this cannot be helped in *Windows PowerShell*, but in
# PowerShell (Core) 7+, you can add an `ErrorMessage` property:
# [ValidateScript({ $_ -in ($customerNames + $allClusters) }, ErrorMessage = 'Unknown customer/cluster name: {0}')]
[ValidateScript({ $_ -in ($customerNames + $allClusters) })]
[string[]] $Cluster
)
# Resolve the specified cluster arguments and remove duplicates from the
# resulting list of cluster-only names.
$resolvedClusters = $Cluster | ForEach-Object {
# If a customer name was specified, eturn the list of clusters for the specified customer.
if ($_ -in $customerNames) { $customerToClusterMapping.$_ }
else { $_ }
} | Select-Object -Unique
"Specified or implied clusters: $resolvedClusters"
}
示例调用,在对上面的代码进行点源之后:
PS> Invoke-VI Customer1 # Customer1 was tab-completed.
Specified or implied clusters: DC0_C0 DC0_C1 DC0_C2
请注意客户名称是如何解析到关联的集群的。
这是
根据要求使用 CSV 文件作为数据源。
显示了一个更简单的制表符完成和验证组合解决方案,它基于传递一个 class 来实现
System.Management.Automation.IValidateSetValuesGenerator
interface to theValidateSet
attribute (gratefully adapted from this answer), but note that it requires PowerShell (Core) 7+.
首先,创建一个包含 2 个客户及其关联集群的示例 CSV 文件:
# Create a sample CSV file that maps customer names to clusters.
# This will serve as the basis for tab-completion / argument validation.
# IMPORTANT: Be sure that you have enough headers (colum names) to cover
# the maximum number of columns values.
@'
Customer,Cluster1,Cluster2,Cluster3,Cluster4,Cluster5
Customer1,DC0_C0,DC0_C1,DC0_C2
Customer2,DC1_C0,DC1_C1
'@ > CustomersToClusters.csv
基于它构建的代码:
# Parse the CSV file, assumed to be located in the same dir.
# as this script.
$csvRows = Import-Csv $PSScriptRoot/CustomersToClusters.csv
# Build a hashtable of customer names mapped to their associated clusters.
$colCount = $csvRows[0].psobject.Properties.Name.Count
$htCustomersToClusters = [ordered] @{}
foreach ($row in $csvRows) {
$htCustomersToClusters[$row.Customer] = $row.psobject.Properties.Value[1..($colCount-1)] -ne $null
}
# Build an array of all customer and cluster names.
[string[]] $allCustomersAndClusters = $htCustomersToClusters.Keys + $htCustomersToClusters.GetEnumerator().ForEach({ $_.Value })
# Define the custom class that implements the System.Management.Automation.IValidateSetValuesGenerator
# interface, to be passed to the [ValidateSet()] attribute.
class CustomerAndClusters : System.Management.Automation.IValidateSetValuesGenerator {
[string[]] GetValidValues() { return $script:allCustomersAndClusters }
}
function Invoke-VI {
param(
# Provide tab-completion and validation based on the values
# returned by a [CustomersAndClusters] instance's .GetValidValues() call.
[ValidateSet([CustomerAndClusters])]
[string[]] $Cluster
)
# Resolve the specified cluster arguments and remove duplicates from the
# resulting list of cluster-only names.
$resolvedClusters = $Cluster | ForEach-Object {
# If a customer name was specified, resolve it to the list of associated clusters.
if ($customerClusters = $htCustomersToClusters[$_]) { $customerClusters }
else { $_ }
} | Select-Object -Unique
"Specified or implied clusters: $resolvedClusters"
}