1

下午好,

我最近创建了一个 PowerShell 脚本来自动化和测试 PowerCLI 脚本,然后再在 vCenter/ESXi 主机上运行它们。

目前,我使用验证集让用户选择他们想要在哪个服务器/集群上执行他们的脚本:

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”,那么它将用适当的集群替换他的值,但我觉得这个解决方案并不优雅。而且很难配置/维护,因为用户需要修改代码,所以如果这些参数可以从外部配置文件/用户输入创建就更好了。

我还想知道是否可以从文件/csv 中检索 validateset 的参数,以避免用户自定义主 script.ps1,而是简单地将他们的服务器和集群替换/写入填充 validateset 参数的 .CSV .

希望清楚..

问候,亚历山德罗

4

2 回答 2

1

这是一个概念证明,它实现了一个Invoke-VI功能,该功能展示了所需的选项卡完成/验证行为,由JSON 文件中定义的客户到集群映射驱动(参见基于CSV的解决方案的另一个答案)。

笔记:

  • JSON 被选为 CSV 的更灵活替代方案,因为后者需要每个客户固定数量的集群(列)。最终,像 YAML 或 INI 文件这样的东西对最终用户来说会更方便,但 PowerShell 目前缺乏对这些格式的内置支持;参见GitHub 提案 #3607GitHub 提案 #9035但是, PowerShell 库中提供了第三方模块,例如powershell-yamlPSIni

  • 实现的是函数而不是脚本,因为在使用所需的制表符完成和验证功能声明参数之前需要准备步骤。这意味着,您不需要直接*.ps1运行脚本,而是需要对其进行点源( ),然后调用该函数。或者,将函数定义为(自动加载)模块的一部分。. ./viexec.ps1Invoke-VI

首先,创建一个包含 2 个客户及其关联集群的示例 JSON 文件:

# 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"

}  

示例 call,在对上面的代码进行点源之后:

PS> Invoke-VI Customer1    # Customer1 was tab-completed.
Specified or implied clusters: DC0_C0 DC0_C1 DC0_C2

请注意如何将客户名称解析为与之关联的集群。

于 2021-04-30T18:22:14.783 回答
0

这是基于 JSON 的答案的变体,其中:

首先,创建一个包含 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"

}
于 2021-05-02T18:03:59.250 回答