【问题标题】:How to get a template list from several clusters?如何从多个集群中获取模板列表?
【发布时间】:2019-01-08 14:37:38
【问题描述】:

我正在尝试从 6.0 vCenter 迁移到 6.5 vCenter,并且想要迁移所有模板。如何一次选择所有集群以检索完整的模板列表?

我的 6.0 vCenter 中有很多模板,需要导出一个列表,以便使用 Powercli 在新的 6.5 vCenter 中一次性迁移所有模板。我发现的唯一方法是使用 foreach 循环,我必须在其中提供一个集群名称。 我尝试使用“get-datacenter”而不是“get-cluster”,但结果更糟。

$toto = foreach ($vmhost in Get-Cluster 'my_cluster'|Get-VMHost) {
    Get-Template -Location $vmhost |
        select name, @{n='VMHOST';e={$vmhost.name}},
            @{n='VMTX';e={$_.extensiondata.config.files.VmPathName}}
} 

$toto | Export-Csv C:\scripts\Templates.csv

代码有效,但没有显示 vCenter 中的所有模板。 我怎样才能让它工作,以便我可以一次拥有所有集群中的所有模板,而不为每个模板使用循环?

【问题讨论】:

  • 在将Datacenter 实例传递给Get-Cluster 时,您能否详细说明“结果更糟”的含义?您是否尝试过将Datacenter 作为Get-Template-Location 参数传递,如the documentation 中的示例所示?
  • 我确实尝试过Get-Template -Location DatacenterName | Select-Object Name,DatastoreIdList,但问题是DatastoreIdList 返回了一个表。我没有尝试使用前面显示的foreach 行。是的,它奏效了...谢谢
  • 管道格式列表工作?

标签: powershell vmware powercli


【解决方案1】:

该功能通过扫描所有可用的集群和主机从单个数据中心目标中提取所有模板;将完整结果转储到 $array_list 变量中。

函数 Render_Template_List {

Clear-Host
$array_list = [System.Collections.ArrayList]@()
if (!($global:templates_bool)) {
    $host.ui.RawUI.WindowTitle = "Retrieving available templates for use with '$global:datacenter' datacenter...Please Wait!"
    if ($global:datacenter -eq 'none'){
        $ESX = get-datacenter | get-VMhost | %{$_.Extensiondata.MoRef}
    } else {
        $ESX = get-datacenter -name "$global:datacenter" | get-VMhost | %{$_.Extensiondata.MoRef}
    }
    If ($global:Template -eq 'none') {
        $Query = Get-Template | where {$ESX -contains $_.Extensiondata.Runtime.Host} | Sort-Object               
    } else {
        $Query = Get-Template -name "$global:Template" | where {$ESX -contains $_.Extensiondata.Runtime.Host} | Sort-Object        
    }
    $global:templates_bool = $true
    $global:templates_query = $query

} 
$seperator = "{,}"
$query = 0
$arr = 1
foreach ($template in $global:templates_query )  {
    if ($arr -eq 1) {
        foreach ($array in $global:templates_array) {
            If (!($array -like "*#*")) {
                $query = $query + 1
                $val_template = $array.split($seperator)[2]
                $val_template = $val_template.replace("`"","") 
                if ("$val_template" -eq "$template") {
                    $val_datacenter = $array.split($seperator)[1]
                    $val_datacenter = $val_datacenter.replace("`"","")                    
                    if ("$val_datacenter" -eq "$global:datacenter")  {
                        $array_list.Add("$val_template") | Out-Null
                    }
                }
            }
        } 
    }     
    if ($query -eq 0) {
        $array_list.Add("$template") | Out-Null
        $arr = 0
    }
}     

return $array_list

}

【讨论】:

    猜你喜欢
    • 2013-07-11
    • 2022-07-11
    • 2019-04-04
    • 2011-08-08
    • 2019-11-21
    • 1970-01-01
    • 1970-01-01
    • 2021-08-25
    • 1970-01-01
    相关资源
    最近更新 更多