Ran into something goofy/unexpected on vCloud Director 5.5.2 with PowerCLI 5.8.0.
The logic (in this particular scenario anyway), just checks if the vApp exists; if it doesn't it returns null, then it creates the vApp then does another request for the object. If the vApp does exist, it just skips the creation and moves on to use the object somewhere else.
When I run a command to get an object (typically a vApp Template), it'll return null, even though the object exists. The same thing happens when we request a vDC. Is this something broken with our vCloud Director install?
So for example, the following code...
#check if the vApp exists, if not, create it
$vApp=Get-CIVApp -Name $vAppName -OrgVdc $vDC -ErrorAction:SilentlyContinue
if($vApp -eq $null){
#create the new vApp
New-CIVApp -Name $vAppName -OrgVdc $vDC -VAppTemplate $vAppTemplate # -ErrorAction:Stop
#get the newly created vApp and store it. If it doesn't exist, fail the script
$vApp=Get-CIVApp -Name $vAppName -OrgVdc $vDC -ErrorAction:Stop
}
...results in the following errors:
New-CIVApp : 1/9/2015 7:09:59 PM New-CIVApp DUPLICATE_NAME: The VCD entity new_vApp already exists.
At C:\Users\me\Desktop\Programming_SP15.ps1:149 char:4
+ New-CIVApp -Name $vAppName -OrgVdc $vDC -VAppTemplate $vAppTemplate # -ErrorA ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [New-CIVApp], CIServerException
+ FullyQualifiedErrorId : VMware.VimAutomation.Cloud.Impl.VAppServiceImpl.NewVAppFromTemplate.Error,VMware.VimAutomation.Cloud.Commands.Cmdlets.NewCIVApp
Get-CIVApp : 1/9/2015 7:09:59 PM Get-CIVApp CIVApp with name 'new_vApp' was not found using the specified filter(s).
At C:\Users\me\Desktop\Programming_SP15.ps1:152 char:10
+ $vApp=Get-CIVApp -Name $vAppName -OrgVdc $vDC -ErrorAction:Stop
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (:) [Get-CIVApp], VimException
+ FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.Cloud.Commands.Cmdlets.GetCIVApp
Forgot the golden rule... When in doubt try a different system. Worked flawlessly. So we isolated this particular issue to just occurring on a single system, and it isn't a problem with our vCD API responding incorrectly.
Out of interest. what get return if you add $vApp to a single line on it's own below the if statement.
Also if ($vApp) { should work the same as if ($vApp -eq $null) {