четверг, 8 июня 2023 г.

vCloud - Export Template to OVF

 #ScriptDescription : =Export from Template to OVF=

cls

Write-Host "Export from vCloud Template to OVF with help of vCenter" -BackgroundColor DarkRed -foreground Yellow

Write-Host "(Check lease and renew if needed)" -BackgroundColor Black

$TemplateName = ""

$DestVAppName = ""

$SubStringLength = "10"

Import-Module VMware.PowerCLI | Out-Null

# Import-Module VMware.PowerCLI 3>$null

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -confirm:$false | Out-Null

$ErrorActionPreference = 'SilentlyContinue'

$WarningActionPreference = 'SilentlyContinue'

$ErrorPreference = 'SilentlyContinue'

$WarningPreference = 'SilentlyContinue'


##== Defining general parameters ==##

[Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') > $NULL

$TemplateName = [Microsoft.VisualBasic.Interaction]::InputBox("Put Template name `n(otherwise the script will exit...)", "Template Name")

IF(-not$TemplateName){Write-Host "You did not provide Template name, the script is exiting..." -BackgroundColor Black; Break}

if  ($TemplateName.Length -gt $SubStringLength)

    # {$ShortTemplateName = $TemplateName.SubString(0,25); $DestVAppName = $ShortTemplateName+"(OVF)"}

    {$DestVAppName = ($TemplateName.SubString(0,$SubStringLength))+"(OVF)"}

else{$DestVAppName = $TemplateName+"(OVF)"}

Write-Host "Destination VApp name is '$DestVAppName'" -BackgroundColor Green

$UpperFolder = "D:\OVF"

$DestFolder = -join($UpperFolder+"\"+$DestVAppName+"\")

MD $DestFolder

$TestPath = Test-Path -Path $DestFolder

If($TestPath){Write-Host "Path '$DestFolder' exists!" -BackgroundColor Red} Else{ "Path isn't created. The script will exit..."; Break } 

##== Defining vCloud/vCenter connection parameters ==##

$vCloud = 'vCloud'

$vCenter = 'VCRD'

$UserName = $env:UserName

$SecurePass = Read-Host -AsSecureString "Please enter password for $UserName and wait..."

$ClearPass = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecurePass))

##== Connecting to vCenter/vCloud ==##

Write-Host

Write-Host "Connecting to vCenter/vCloud" -BackgroundColor DarkRed

Disconnect-VIServer * -Confirm:$false -ErrorAction SilentlyContinue

Connect-VIServer -Server $vCenter -User $UserName -Password $ClearPass -ErrorAction SilentlyContinue | Out-Null

Write-Host You are connected to vCenter -BackgroundColor Green

Disconnect-CIServer * -Confirm:$false | Out-Null

Connect-CIServer -Server $vCloud -User $UserName -Password $ClearPass -ErrorAction SilentlyContinue | Out-Null

Write-Host "You are connected to vCloud" -BackgroundColor Green

$global:DefaultVIServers | select Name, User, Version

$global:DefaultCIServers | select Name, User, Version, Org


##== Real Work - Converting Template to vApp ==##

##==Checking Template existence

$VAppTemplateOBJ = Get-CIVAppTemplate $TemplateName

If(!$VAppTemplateOBJ){Write-Host "Template '$TemplateName' does not exists!" -BackgroundColor DarkRed; Break}

$VAppTemplateOBJ | FT Name, Status, Owner, OrgVdc, Catalog, StorageUsedGB -AutoSize

$OrgVdcOBJ = $VAppTemplateOBJ.OrgVdc

$OrgVdcName = $OrgVdcOBJ.Name

Write-Host "Defined Template name: '$TemplateName'"  -BackgroundColor Green

Write-Host "OrgVdc Name: '$OrgVdcName'" -BackgroundColor Green

Write-Host "Destination VApp name is '$DestVAppName'" -BackgroundColor Green

Write-Host "Destination Path: '$DestFolder'" -BackgroundColor Green

Write-Host

Write-Host "We are going to create a new vApp '$DestVAppName'..." -BackgroundColor Black -ForegroundColor Yellow

$NewVApp = New-CIVApp -Name $DestVAppName -OrgVdc $OrgVdcName -VAppTemplate $VAppTemplateOBJ

Write-Host "You have created a new vApp '$NewVApp'" -BackgroundColor Black -ForegroundColor Yellow

$NewVApp

Write-Host "Getting vCloud vApp properties" -BackgroundColor Green

$vAppObj = Get-CIVApp -Name $DestVAppName

$vAppObj | FT Name, Status, Org, Owner, Shared, SizeGB -AutoSize

#== Stopping the vApp (It is not nessessary because a new vApp is not running, but to be sure)

Stop-CIVApp $vAppObj -Confirm:$false

##== Converting vApp VM names to vCenter names ==##

$vAppVMObj = $vAppObj | Get-CIVM

$vAppVMsCount = $vAppVMObj.Count

Write-Host "vApp '$vAppObj' has $vAppVMsCount VMs :" -BackgroundColor Green

$vAppVMObj | FT Name, Status, CpuCount, MemoryGB, Description, GuestOSFullName, VApp -AutoSize

Write-Host "Select VMs for exporting to OVF :" -BackgroundColor Green

$vAppVMObjChosen = $vAppVMObj | OGV -PassThru

Write-Host "You selected $vAppVMObjChosen VMs"

# Action

ii $DestFolder

Write-Host "Wait for several hours until the process finish....." -ForegroundColor Green -Background DarkRed

ForEach ($CloudVM in $vAppVMObjChosen)

{

Get-Date

$vCenterVMName = (Get-View –RelatedObject $CloudVM.ExtensionData).Name

Write-Host "'$CloudVM' = '$vCenterVMName'" -BackgroundColor Green

Write-Host "We are exporting VMs to the '$DestFolder'" -BackgroundColor Green

$VMPath = -join($DestFolder+$vCenterVMName)

Export-VApp -Destination $DestFolder -VM $vCenterVMName -Format OVF

}

ii $DestFolder

Write-Host "Now I am going to remove a new vApp '$NewVApp'" -BackgroundColor Green

#Remove-CIVApp $vAppVMObj

Get-Date

Комментариев нет:

Отправить комментарий