【问题标题】:Powershell: How to incrementally scale up variables in a loop?Powershell:如何在循环中逐步扩大变量?
【发布时间】:2019-02-16 22:51:52
【问题描述】:

我是脚本/PowerShell/PowerCLI 新手。我的任务是弄清楚如何最好地扩展我们现有的一些脚本。

我们的脚本从最终用户那里获取 YAML 输入,并根据他们的规范构建 VMware ESXi 集群。我们正在尝试扩展脚本,以便我们可以根据用户在 YAML 中指定的集群类型应用不同的配置。我们希望最终用户能够扩展它以根据需要创建尽可能多的集群。同时根据他们输入的集群类型应用不同的配置。我们还希望将来能够轻松地将 Cluster"X"Type 扩展为我们最终定义的其他类型。

YAML 输入示例:

Cluster1: <Name>
Cluster1Type: <Basic, DR, or Replicate>
Cluster2: <Name>
Cluster2Type: <Basic, DR, or Replicate>

我知道我可以通过硬编码很长的 if 和语句的相当不干净的方式来做到这一点。比如:

If ($Cluster1Type -eq 'DR') {<Code to execute on $Cluster1>}
ElseIf ($Cluster1Type -eq 'Replicate') {<Code to execute on $Cluster1>}
Else {<Code to execute on $Cluster1>}

If ($Cluster2Type -eq 'DR') {<Code to execute on $Cluster2>}
ElseIf ($Cluster2Type -eq 'Replicate') {<Code to execute on $Cluster2>}
Else {<Code to execute on $Cluster2>}

我知道必须有更好的方法来解决这个问题。如果我没记错的话,vSphere 6.5 每个 vCenter 最多可以有 64 个集群,当然,每次我们需要检查最终用户分配给特定集群名称的集群类型时,都不想硬编码 64 个 if else 语句。我一直在寻找一个干净的解决方案,但我的经验不足使得我很难自己找到答案。

我还认为可以为集群名称使用变量数组,然后提示执行我们的 PowerShell 脚本的用户为他们输入到数组中的每个集群名称输入集群类型。我仍然认为可能有比这更好的方法吗?可能是一种在增量方法中对每个 ClusterX 和 ClusterXType 变量运行循环的方法?

【问题讨论】:

  • 切换命令:ss64.com/ps/switch.html
  • 用字典之类的名称/值集合来处理这种情况会不会更好
  • 使用第 3 方 yaml 工具读取所有行。然后做一个foreach集群,写一个集群函数来做需要做的事情,在里面使用switch命令或你上面的if语句。 dbadailystuff.com/yaml-in-powershell

标签: powershell yaml powercli


【解决方案1】:

你是在说这样的话吗? 这是假设用户一次只能输入一种集群类型。

# Specify the number of cluster nodes to create
$ClusterCount = Read-Host -Prompt 'Enter the number of guests to create'

# Enter a cluster type to create
$ClusterType = Read-Host -Prompt 'Enter the type - Basic, DR, Replicate'

1..$ClusterCount | 
ForEach{
    "Working cluster type $ClusterType on new node name Cluster$PSITEM"
    <#
    If ($ClusterType -eq 'DR') {"Code to execute on Cluster$PSItem"}
    ElseIf ($ClusterType -eq 'Replicate') {"Code to execute on Cluster$PSItem"}
    Else {<Code to execute on $Cluster1>}
    #>
}

# Results

Enter the number of guests to create: 3
Enter the type - Basic, DR, Replicate: Basic
Working cluster type Basic on new node name Cluster1
Working cluster type Basic on new node name Cluster2
Working cluster type Basic on new node name Cluster3

【讨论】:

  • 这对我们正在努力实现的目标肯定很有帮助。我们需要集群名称列表是静态的,因为它在我们脚本的许多其他部分(Cluster1、1 Cluster2 等)中被调用。我确实认为您建议如何为 if else 语句迭代集群名称是好的,并且可能会对我有所帮助。谢谢,我会进一步调查那条路线。
【解决方案2】:

您可以使用 New-Variable 命令创建一个变量,该变量使用另一个变量作为名称

$iteration = 1
New-Variable -Name Cluster$iteration

这会创建一个名为$Cluster1的变量

Get-Variable C*

Name          Value
----          ----
Cluster1

【讨论】:

  • 我正在尝试根据用户以可以扩大的方式输入到 $Cluster"X"Type 变量中的内容,将一组不同的命令应用于 $Cluster"X"到任意数量的成对变量。我不确定上述答案是否适用于该场景,或者我只是误解了这个回复?上面的先前评论提到了可能有帮助的 switch 语句。我需要进行一些研究才能弄清楚如何在这种情况下实现它。
【解决方案3】:

相反,我们最终在 YAML 中创建了一个对象数组。然后将 YAML 导入我们的脚本并通过 Clusters.Name / Clusters.Type 调用每个脚本。感谢大家的帮助,让我学习了完成这项任务或类似任务的各种方法。

集群: - 姓名:XXXXXX 类型:XXXXX

【讨论】:

    猜你喜欢
    • 2022-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-09
    相关资源
    最近更新 更多