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)
}


March 2nd, 2012 at 8:07 pm
Reblogged this on The Windows & VMware Guru.
June 8th, 2012 at 6:02 am
Great job! I was looking for a script to do exactly that! big thank you I adapted the script in my environment but I get some errors with “UploadFile” (Operation has timed out or aborted request: the resquest was Canceled) and I do not understand what causes these errors
July 3rd, 2012 at 12:18 pm
It seems to be a size problem. Can’t upload file bigger than 1Go :’(
July 3rd, 2012 at 4:37 pm
Interesting, that sounds almost like a PowerCLI bug. Would you want some code to upload media that bypasses PowerCLI?
July 4th, 2012 at 1:41 pm
another solution ?! Oh yes I want :$
September 5th, 2012 at 2:18 am
do you have any update for this “bug” ? I can’t find another way to upload my ISOs :’(