VMware Cloud Community
TaupoJohn
Contributor
Contributor

powercli 5.0.1 - how do you change a vapp's owner?

Hi,

Is anyone using powercli to deploy vCD vapps? I've got vapps deploying fine, but they are set with "system" as the owner. I want to change the owner of these vapps, so that  astudent can login and only see his (or her) vapps. This should be easy - but I just can't find it.

To keep things simple, after the vapp is deployed I'd do something like this:

$vapp = Get-CIVApp "student1 vsphere 5.1 - Install"

$user = Get-CIUser -name student1

Using extensiondata, I tried

$vapp.owner.name=$user

but it sdays the owner property is read only!

I even dug deeper with

$myvapp.owner.name | get-member   and found a "replace" methof

I tried

$vapp.owner.name=$vapp.owner.name.Replace('system', $user)

but it still says it's read only.

Do I have to create a new object, and somehow replace the existing owner? That's reaching the limits of my brain capacity 😉

I've googled this - but I don't see a lot of people actually writing to properties of objects. I can use methods (hence I tried the replace method) but there must be a way to change properties of a vapp.

This should be straightforward, but it's got me baffled.

Any clues would be greatly appreciated.

rgds, John B
0 Kudos
16 Replies
Troy_Clavell
Immortal
Immortal

thread moved to VMware vSphere™ PowerCLI Community for better exposure.

0 Kudos
TaupoJohn
Contributor
Contributor

changed title to reflect that I'm working with vCloud Director vapps.

rgds, John B
0 Kudos
dmilov
VMware Employee
VMware Employee

Hi,

Here is how to change the vApp owner:

$vAppView = $vApp.ExtensionData

$newOwner = Get-CIUser -Name <newOwnerUserName>

$newOnwerView = $newOwner.ExtensionData

$vAppView.Owner.User.Name= $newOnwerView.Name

$vAppView.Owner.User.Href = $newOnwerView.Href

$vAppView.Owner.UpdateServerData()

$vAppView.Owner.UpdateViewData()

In the code above replace <newOwnerUserName> with the desired owner user name. The way the code is working is:

1. get the vApp view object

2. get the desired user view object.

3. Update vApp's owner user name and href

4. Update the server data for the $vAppView Owner object.

Regards,

Dimitar Milov

PowerCLI team

TaupoJohn
Contributor
Contributor

Dimitar,

Thanks so much for this! I found an alternative method to try - to change the sharing of a vapp

(http://geekafterfive.com/2012/06/26/sharing-vapps-in-vcloud-with-powercli/) - but I thought changing the owner would be easier.

As you can tell, there are big holes in my knowledge of how to do this - but your method looks perfect.

I will try this tomorrow.

PS - I fixed a small typo ($newOnwerView)

$vAppView = $vApp.ExtensionData

$newOwner = Get-CIUser -Name <newOwnerUserName>

$newOwnerView = $newOwner.ExtensionData

$vAppView.Owner.User.Name= $newOwnerView.Name

$vAppView.Owner.User.Href = $newOwnerView.Href

$vAppView.Owner.UpdateServerData()

$vAppView.Owner.UpdateViewData()

rgds, John B
0 Kudos
dmilov
VMware Employee
VMware Employee

Don't worry, we are here to help.

So if you have any other questions, post here. Smiley Happy

0 Kudos
TaupoJohn
Contributor
Contributor

OK - I definitely will Smiley Happy

rgds, John B
0 Kudos
TaupoJohn
Contributor
Contributor

One more thing - I guess I have to define $vapp as my vapp object?

$vapp = $config1 (the vapp I've just deployed)

rgds, John B
0 Kudos
dmilov
VMware Employee
VMware Employee

Yes, $vApp should be vApp object.

In your code if $config1 is vApp object returned by PowerCLI cmdlet it should work.

How do you deploy your vApp?

0 Kudos
TaupoJohn
Contributor
Contributor

I'm using velementals a "unofficial vcd cmdlet"

http://velemental.com/2012/05/05/unofficial-vmware-vcd-cmdlets/

Function Instantiate-VApp {
    #
    # -hashNetworks is a mapping between what the VMs in the vApp template have listed and what the target networks for the org are (case sensitive)
    #    listed as a index # per unique network listed
    #   -hashNetworks @{0=@{sourceNet="ALL_PURPOSE";targetNet="BBS_external01"}} -- represents a single network specified
    #   -hashNetworks @{0=@{sourceNet="ALL_PURPOSE";targetNet="BBS_external01"};1=@{sourceNet="Internal-NoUplink";targetNet="BBS_internal01"}} -- represents 2 networks specified
    #
    # EXAMPLES
    #
    # Instantiate-VApp -name "test" -catalogname HOLDEV_HOL22_CAT01 -VAppTemplateName HOL22-Test-v1 -orgvdcname OvDC_HOLDEV_HOL22_C01 -mode Clone
    # Instantiate-VApp -name "test" -catalogname HOLDEV_HOL22_CAT01 -VAppTemplateName HOL22-Test-v1 -orgvdcname OvDC_HOLDEV_HOL22_C01 -mode Clone -poweron:$false -deploy:$false
   
    Param (
        $Name=$(throw "need -name"),
        $CatalogName=$(throw "need -catalogname"),
        $VAppTemplateName=$(throw "need -VAppTemplateName"),
        $orgVdcName=$(throw "need -orgVdcName"),
        $hashNetworks = @{},
        $LinkedClone=$true,
        $AllEULAsAccepted=$true,
        $IsSourceDelete=$false,
        $Deploy=$true,
        $PowerOn=$true,
        $mode="Clone"
    )
    Process {
        $paramsVAppT = New-Object VMware.VimAutomation.Cloud.Views.InstantiateVAppTemplateParams
        $paramsVAppT.AllEULAsAccepted = $AllEULAsAccepted
        $paramsVAppT.LinkedClone = $LinkedClone
        $paramsVAppT.Source = (Get-Catalog $CatalogName | Get-CIVAppTemplate $VAppTemplateName).ExtensionData.Href
        $paramsVAppT.IsSourceDelete = $IsSourceDelete
        $paramsVAppT.Deploy = $Deploy
        $paramsVAppT.PowerOn = $PowerOn
        $paramsVAppT.Name = $name
        $paramsInst = New-Object vmware.vimautomation.cloud.views.instantiationparams
        #temporarily deprecated everything but Clone
        $paramsInst.Section = (Get-Catalog $CatalogName | Get-CIVAppTemplate $VAppTemplateName).Section
        $paramsVAppT.InstantiationParams = $paramsInst
        (Get-OrgVdc $orgVdcName | Get-CIView).InstantiateVappTemplate($paramsVappT)
    }
}

It works great.

I just need to change the owner, so that when a student logs in to vcd, they can only see their own vapps that have been deployed for them.

rgds,

John B

rgds, John B
0 Kudos
dmilov
VMware Employee
VMware Employee

OK,

As I see in your script you are using InstantiateVappTemplate method of the Org Vdc view, this method returns vApp View, so if you use the script from my post above you have $vAppView returned by this line of code:

(Get-OrgVdc $orgVdcName | Get-CIView).InstantiateVappTemplate($paramsVappT)

So make the following:

$vAppView = (Get-OrgVdc $orgVdcName | Get-CIView).InstantiateVappTemplate($paramsVappT)

and ignore the 1st line of my script.

0 Kudos
TaupoJohn
Contributor
Contributor

That's fantastic - thanks! I'm not worried about getting the vapp I just deployed because "get-civapp -name" will get it (I jst gave it a name when I instantiated it)

Your way is better and will be more seamless Smiley Wink

I'm not working today, but will be on this first thing tomorrow Smiley Wink

==================

John Braner

Sent from my BlackBerry® wireless device

rgds, John B
0 Kudos
TaupoJohn
Contributor
Contributor

Dimitar,

FYI - this line doesn't work

$vAppView = (Get-OrgVdc $orgVdcName | Get-CIView).InstantiateVappTemplate($paramsVappT)

Error:

Get-OrgVdc : Cannot validate argument on parameter 'Name'. The argument is null or empty. Supply an argument that is no

t null or empty and then try the command again.

At C:\scripts\deploy-selfpaced-1.ps1:306 char:24

+ $vAppView = (Get-OrgVdc <<<<  $orgVdcName | Get-CIView).InstantiateVappTemplate($paramsVappT)

    + CategoryInfo          : InvalidData: (:) [Get-OrgVdc], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.Cloud.Commands.Cmdlets.GetOrgVdc

I just used $vAppView = $vApp.ExtensionData

And this works great:

(deploy the vapp with Instantiate-VApp...)

$vApp = get-civapp -name $config1

$vAppView = $vApp.ExtensionData

$newOwner = Get-CIUser -Name $student

$newOwnerView = $newOwner.ExtensionData

$vAppView.Owner.User.Name= $newOwnerView.Name

$vAppView.Owner.User.Href = $newOwnerView.Href

$vAppView.Owner.UpdateServerData()

$vAppView.Owner.UpdateViewData()

So thank you very much for this! 😉

rgds, John B
0 Kudos
TaupoJohn
Contributor
Contributor

Hi Dimitar,

One more question. This script is working fine when used on it's own - but I'm getting an error message when I add it to my main script. Mind you - it reports an error, but everything is actually working fine. I'd just like to get rid of the error message.

The error message is:

VMware.VimAutomation.Cloud.Views.VApp is not deployed correctly!

but the vapp has actually deployed fine - and the owner has been changed correctly.

to recap, I deploy the vapp with:

$deployedvapp1 = Instantiate-VApp -name $config1 -catalogname "class deployments" -VAppTemplateName $cat1 -orgvdcname "vs51 org vdc" -mode Clone

then run:

$vApp = get-civapp -name $config1 (this returns the correct deployed vapp)
$vAppView = $vApp.ExtensionData


$newOwner = Get-CIUser -Name $student
$newOwnerView = $newOwner.ExtensionData


$vAppView.Owner.User.Name= $newOwnerView.Name
$vAppView.Owner.User.Href = $newOwnerView.Href


$vAppView.Owner.UpdateServerData()
$vAppView.Owner.UpdateViewData()

I've tried

$ErrorActionPreference = "silentlycontinue"

to get rid of the error, and I've also tried pausing the script for awhile in case the deployment was not quite finished

but the error still occurs.

If I then go in and just run the "change owner" part of the code - it runs fine.

I'm puzzled. I could just leave the error - because everything is deploying fine - but it would be better to get rid of it.

Do you have any ideas?

Thanks.

PS $vAppView shows

OvfDescriptorUploaded :
Owner                 : VMware.VimAutomation.Cloud.Views.Owner
InMaintenanceMode     : False
Children              : VMware.VimAutomation.Cloud.Views.VAppChildren
Deployed              : True
VAppParent            :
Section               : {VMware.VimAutomation.Cloud.Views.LeaseSettingsSection, VMware.VimAutomation.Cloud.Views.OvfSta
                        rtupSection, VMware.VimAutomation.Cloud.Views.OvfNetworkSection, VMware.VimAutomation.Cloud.Vie
                        ws.NetworkConfigSection}
Status                : 4
Files                 :
Name                  : student1 vSphere 5.1 - Exercises
Id                    : urn:vcloud:vapp:d1ca7469-9bc7-4eb8-94ff-8a6dbb98e18d
Description           :
Tasks                 :
Client                : VMware.VimAutomation.Cloud.Views.CloudClient
Href                  : https://192.168.45.23/api/vApp/vapp-d1ca7469-9bc7-4eb8-94ff-8a6dbb98e18d
Type                  : application/vnd.vmware.vcloud.vApp+xml
Link                  : {, , , ...}
AnyAttr               : {xsi:schemaLocation}
VCloudExtension       :

It is definitely deployed correctly...

rgds, John B
0 Kudos
dmilov
VMware Employee
VMware Employee

Hi John,

looks as if the error comes from the Instantiate-VApp command, but it is not PowerCLI command. So if the vApp is deployed correctly on the server I suppose it is an  incorrect error in the command itself not on the server.

You can try to call Instantiate-VApp with

-ErrorAction SilentlyContinue

to check whether the error will be hidden.

No other ideas

Regards,

Dimitar Milov

0 Kudos
TaupoJohn
Contributor
Contributor

OK thanks. The error is coming from

$vAppView.Owner.UpdateViewData()

but it won't suppress.

I'm pretty sure this worked yesterday - but not 100% certain.

Oh well, I'll try emailing the guy at vElemental.com, and will try a few things to see if I can get rid of it.

rgds, John B
0 Kudos
TaupoJohn
Contributor
Contributor

Sorry Dimitar,

It's a different line throwing the error! I can fix this.

Sorry for bothering you 😉

rgds, John B
0 Kudos