OpsaC - Operating as PowerShell code
published: January 4, 2024 author: Tinu tags: PowerShell categories: VMware
For the login to the vCenter Server see vSphere REST API
Clone a virtual machine as OVF-Template in a Content Library.
# Create an empty reference object
$libraryObject = New-Object PSObject
#region Get the specified VM by name
$vmName = 'TPL-RHEL8'
$Properties = @{
Uri = "https://$($vCenterServer)/api/vcenter/vm?names=$($vmName)"
Method = 'Get'
Headers = $ApiSessionHeaders
UseBasicParsing = $true
ContentType = 'application/json'
ErrorAction = 'Stop'
}
$vm = Invoke-RestMethod @Properties
if(!$vm){
Write-Warning "Could not find any virtual machine with name $vmName"
break
}
$libraryObject | Add-Member -MemberType NoteProperty -Name 'runtime' -Value $(Get-Date -f 'yyyy-MM-dd HH:mm:ss.fff') -Force
$libraryObject | Add-Member -MemberType NoteProperty -Name 'vmId' -Value $($vm.vm) -Force
$Properties = @{
Uri = "https://$($vCenterServer)/api/vcenter/vm/$($libraryObject.vmId)"
Method = 'Get'
Headers = $ApiSessionHeaders
UseBasicParsing = $true
ContentType = 'application/json'
ErrorAction = 'Stop'
}
$vm = Invoke-RestMethod @Properties
$libraryObject | Add-Member -MemberType NoteProperty -Name 'runtime' -Value $(Get-Date -f 'yyyy-MM-dd HH:mm:ss.fff') -Force
$libraryObject | Add-Member -MemberType NoteProperty -Name 'vmName' -Value $($vm.name)
#endregion
Get the id of the target Content Library:
#region Get the specified Content Library by name
$librarySpec = @{
"name" = "ContentLibrary"
"type" = "LOCAL"
}
$Properties = @{
Uri = "https://$($vCenterServer)/api/content/library?action=find"
Method = 'Post'
Headers = $ApiSessionHeaders
Body = $librarySpec | ConvertTo-Json
UseBasicParsing = $true
ContentType = 'application/json'
ErrorAction = 'Stop'
}
$libraryId = Invoke-RestMethod @Properties
if(!$libraryId){
Write-Warning "Could not find any Content Library with name $($librarySpec.name)"
break
}
$libraryObject | Add-Member -MemberType NoteProperty -Name 'runtime' -Value $(Get-Date -f 'yyyy-MM-dd HH:mm:ss.fff') -Force
$libraryObject | Add-Member -MemberType NoteProperty -Name 'libraryId' -Value $($libraryId)
$libraryObject | Add-Member -MemberType NoteProperty -Name 'libraryName' -Value $($librarySpec.name)
#endregion
Clone the virtual machine as OVF-Template in the Content Library:
#region Create Library Item
$libraryItemSpec = @{
"create_spec"= @{
"name" = "$($libraryObject.vmName)_OVF"
"description" = "Description to use in the OVF descriptor stored in the library item. If unset, the server will use source’s current annotation."
}
"source"= @{
"id" = "$($libraryObject.vmId)"
"type" = "VirtualMachine"
}
"target"= @{
"library_id" = "$($libraryObject.libraryId)"
}
}
$Properties = @{
Uri = "https://$($vCenterServer)/api/vcenter/ovf/library-item"
Method = 'Post'
Headers = $ApiSessionHeaders
Body = $libraryItemSpec | ConvertTo-Json
UseBasicParsing = $true
ContentType = 'application/json'
ErrorAction = 'Stop'
}
$libraryItem = Invoke-RestMethod @Properties
$libraryObject | Add-Member -MemberType NoteProperty -Name 'runtime' -Value $(Get-Date -f 'yyyy-MM-dd HH:mm:ss.fff') -Force
if($libraryItem.succeeded){
$libraryObject | Add-Member -MemberType NoteProperty -Name 'libraryItemId' -Value $($libraryItem.'ovf_library_item_id')
}else{
Write-Warning "$($libraryItem.error.errors.error.'error_type'), $($libraryItem.error.errors.error.messages.default_message)"
}
$libraryObject | Add-Member -MemberType NoteProperty -Name 'libraryItemSucceeded' -Value $($libraryItem.succeeded) -Force
$libraryObject | Add-Member -MemberType NoteProperty -Name 'libraryItemError' -Value $($libraryItem.error) -Force
#endregion
List the reference object:
$libraryObject | Format-List
List the Content Library Item:
$Properties = @{
Uri = "https://$($vCenterServer)/api/content/library/item/$($libraryObject.libraryItemId)"
Method = 'Get'
Headers = $ApiSessionHeaders
UseBasicParsing = $true
ContentType = 'application/json'
ErrorAction = 'Stop'
}
$myItem = Invoke-RestMethod @Properties
$myItem | Format-List
API Reference on VMware Developper Documentation.