首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >发布构建PowerCLI批量OVF导出工具

发布构建PowerCLI批量OVF导出工具
EN

Stack Overflow用户
提问于 2015-06-12 16:40:38
回答 1查看 2.1K关注 0票数 3

背景

我正在使用PowerCLI创建一个脚本,使用VMware的ovftool执行大规模OVF导出。该脚本通过执行以下功能来工作:

  • 通过PowerCLI参数,获取要导出的VM的vCenter地址、命名方案以及输出OVF的位置。
  • 连接到vCenter实例
  • 将所有遵循指定命名方案的VM放入列表中。
  • 循环遍历列表,并使用ovftool和构建的VM路径将每个VM导出到ovf。

脚本: VMs-to-OVF.ps1

代码语言:javascript
复制
# Take in vCenter address, naming scheme of VMs to be exported, and where the OVFs should be exported

param (

[string]$vcenter = $(throw "

    vCenter address required.`n

    Example:`n

    .\VMs-to-OVF.ps1 -vcenter <192.168.1.200>`n

    .\VMs-to-OVF.ps1 -vcenter <vcenter.test.com>"),

[string]$vmNamingScheme = $(throw "

    VM naming scheme required.`n

    Example:`n

    .\VMs-to-OVF.ps1 -vcenter <vcenterIP/DNS> -vmPath </DATACENTER/vm/`n

    test/> -vmNamingScheme <test-vm-1>`n

    .\VMs-to-OVF.ps1 -vcenter <vcenterIP/DNS> -vmPath </DATACENTER/vm/`n

    test/> -vmNamingScheme <test-vm-*>`n"),

[string]$exportLocation = $(throw "

    Export location required.`n

    Example:`n

    .\VMs-to-OVF.ps1 -vcenter <vcenterIP/DNS> -vmPath </DATACENTER/vm/`n

    test/> -vmNamingScheme <test-vm-1> -exportLocation 192.168.1.100:\`n

    .\VMs-to-OVF.ps1 -vcenter <vcenterIP/DNS> -vmPath </DATACENTER/vm/`n

    test/> -vmNamingScheme <test-vm-*> -exportLocation X:\`n")

)

# Connect to vCenter

Connect-VIServer -Server $vcenter

# $VMs is an array of VM names that will be exported
# $vmNamingScheme gives the VM naming pattern we are looking for

$VMs = $(get-vm -name $vmNamingScheme | select name | format-list | out-string)
$VMs = $VMs.replace("Name : ","")
$VMs = $VMs.replace(" ","")
$VMs = $VMs.split("`n")
$VMs = $VMs|?{$_ -ne $VMs[1]}

# This loop iterates through the $VMs array and performs an OVF export, to the location
# specified in $exportLocation, for each VM name in the array
# $vmPath is comprised of the path to the VM and $VM holds the actual VM name

foreach ($VM in $VMs){
    if ($VMs -ne $null){ 

    # ***THIS SCRIPT ASSUMES THE get-vmfolderpath SCRIPT IS IN THE SAME DIRECTORY AS ITSELF***
    # get the folder path of the VM using the get-vmfolderpath script (this will align with
    # the path in vSphere Folders and Templates view)

    $vmPath = "get-vm -name $VM | .\get-vmfolderpath.ps1"
    &$vmPath

    # ***THIS SCRIPT ASSUMES THE DEFAULT INSTALL PATH FOR THE ovftool PROGRAM
    # run the ovftool command with the proper arguments

    &'C:\Program Files\VMware\VMware OVF Tool\ovftool.exe' vi://$vcenter$vmPath$VM $exportLocation
    }
}

附带脚本: get-vmfolderpath

代码语言:javascript
复制
param(
    [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
    [string]$folderid,
    [switch]$moref
)

$folderparent=get-view $folderid

if ($folderparent.name -ne 'vm'){
    if($moref){
        $path=$folderparent.moref.toString()+'\'+$path
    }
    else{
        $path=$folderparent.name+'\'+$path
    }
    if ($folderparent.parent){
        if($moref){
            get-vmfolderpath $folderparent.parent.tostring() -moref
        }
        else{
            get-vmfolderpath($folderparent.parent.tostring())
        }
    }
}
else {
    if ($moref){
        return (get-view $folderparent.parent).moref.tostring()+"\"+$folderparent.moref.tostring()+"\"+$path
    }
    else {
        return (get-view $folderparent.parent).name.toString()+'\'+$folderparent.name.toString()+'\'+$path
    }
}

错误

如果您将文本复制并粘贴到PowerCLI控制台中,而不是直接从脚本运行时,在for循环的每一次迭代中构建的ovftool命令都会工作。在脚本中运行自定义ovftool命令时会产生以下错误:

代码语言:javascript
复制
The term 'get-vm -name VM-NAME | .\get-vmfolderpath.ps1' is not
recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify
that the path is correct and try again.At
C:\Users\username\Desktop\VMs-to-OVF.ps1:85 char:4
+         &$vmPath
+          ~~~~~~~
    + CategoryInfo          : ObjectNotFound: (get-vm -name CA...mfolderpath.p
   s1:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

我检查过的东西:

  • 直接在-name控制台中运行的"get-vm vm_name vm_name.get-vmfolderpath.ps1.ps1“的输出返回正确的VM路径。
  • 所有变量输出期望值。
  • 如果脚本中生成的确切的ovftool命令在PowerCLI控制台中运行,那么它将正确地导出VM。
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-02 18:08:02

刚刚结束了这个循环。我找到了解决问题的办法。我的猜测是,当您操作Get返回的VM列表时,会丢失一些东西。在$VM到OVFs脚本中,如果将所有"$VM =“行替换为下面的一行,则保留该信息。

$VMs =get -name $vmNamingScheme

在for循环中,使用".Name“属性将每个VM对象传递给get-vmfolderpath脚本。

$vmPath = get-vm -name $VM.Name区.\get-vmfolderpath.ps1 .ps1

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30808178

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档