GeekAfterFive

Infrastructure as Code

home

Upload Media to vCloud with PowerCLI

24 Feb 2012

English: a small compact disk / Memorex CD-RW ...   It's a double post Friday! This one won't be quite as long. ;)   Have a bunch of ISOs you need to upload to your vCloud Director Catalog? This is the script for you! Once in a while I get a crazy "can you do this" from my friend and Bluelock product development dude, Ben Miller. The detailed reasons why are still very top secret, but I can at least share the code to upload media and add it to the catalog!!! Enjoy! -Jake  
$isoFolder = "C:\MyISOs"
$vdcName = "MyVDC"
$catalogName = "MyCatalog"

# create / delete a cloud media object
Get-ChildItem $isoFolder | %{

$media = New-Object VMware.VimAutomation.Cloud.Views.Media
$media.name = $_.name
$media.ImageType = 'iso'
$media.size = $_.length

$media.Files = New-Object VMware.VimAutomation.Cloud.Views.FilesList
$media.Files.File = @(new-object VMware.VimAutomation.Cloud.Views.File)
$media.Files.File[0] = new-object VMware.VimAutomation.Cloud.Views.File
$media.Files.file[0].type = 'iso'
$media.Files.file[0].name = $_.name

$vdc = Get-OrgVdc $vdcName
$vdc.ExtensionData.CreateMedia($media)

$filehref = (Get-Media $media.name | Get-CIView).files.file[0].link[0].href
$webclient = New-Object system.net.webclient
$webclient.Headers.Add('x-vcloud-authorization',$global:DefaultCIServers[0].sessionid)
$webclient.Uploadfile($filehref, 'PUT', $_.fullname)
}

#Add non-catalog media to catalog
$mediaList = get-media | where {!$_.catalog}
$catalog = get-catalog $catalogName

foreach ($noCatMedia in $mediaList)
{
 $resEntity = $vdc.extensiondata.resourceEntities.resourceEntity | where {$_.name -eq $noCatMedia.name}
 $catitem = New-Object VMware.VimAutomation.Cloud.Views.CatalogItem
 $catitem.Entity = $resEntity.href
 $catitem.name = $resEntity.name
 $catitem.description = $media.description
 $catalog.extensiondata.createcatalogitem($catitem)
}
 
comments powered by Disqus