VMware Cloud Community
SCC
Contributor
Contributor

New-VIPermission bug - again

You will see very quickly that I am a total newbie at PowerShell and PowerCLI so be kind.

I am trying to deal with the problem identified in New-VIPermission where I can find the principal in the gui and add to a role, but New-VIPermission returns a principal not found error message for the same AD account used in the gui.

I found this code:

function New-VIAccount($principal) {
$flags = `
  [System.Reflection.BindingFlags]::NonPublic    -bor
  [System.Reflection.BindingFlags]::Public       -bor
  [System.Reflection.BindingFlags]::DeclaredOnly -bor
  [System.Reflection.BindingFlags]::Instance
$method = $defaultviserver.GetType().GetMethods($flags) |
  where { $_.Name -eq "VMware.VimAutomation.Types.VIObjectCore.get_Client" }
$client = $method.Invoke($global:DefaultVIServer, $null)
Write-Output `
  (New-Object VMware.VimAutomation.Client20.PermissionManagement.VCUserAccountImpl `
   -ArgumentList $principal, "", $client)
}

posted as a workaround to this problem but when I call the function with

$account=New-VIAccount "somedoman\someaccountinthedomain"

I get this:

You cannot call a method on a null-valued expression.
At C:\Users\db172\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:
82 char:26
+     $client = $method.Invoke <<<< ($global:DefaultVIServer, $null)
    + CategoryInfo          : InvalidOperation: (Invoke:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

New-Object : Constructor not found. Cannot find an appropriate constructor for
type VMware.VimAutomation.Client20.PermissionManagement.VCUserAccountImpl.
At C:\Users\db172\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:84 char:14
+         (New-Object <<<<  VMware.VimAutomation.Client20.PermissionManagement.
VCUserAccountImpl `
    + CategoryInfo          : ObjectNotFound: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : CannotFindAppropriateCtor,Microsoft.PowerShell.Commands.NewObjectCommand

So I cannot even get the workaround function to work properly to further test against New-VIPermission.

For what it is worth, I put the above function in my powershell profile.

Any enlightened sole that can point me in the right direction?

Thanks

Don

Reply
0 Kudos
7 Replies
LucD
Leadership
Leadership

I suspect the types used in that function were renamed in the current PowerCLI build.

Which PowerCLI version are you using ?

Get-PowerCLIVersion


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
SCC
Contributor
Contributor

PowerCLI Version
----------------
   VMware vSphere PowerCLI 5.0 build 435427
---------------
Snapin Versions
---------------
   VMWare AutoDeploy PowerCLI 5.0 build 575
   VMWare ImageBuilder PowerCLI 5.0 build 575
   VMware License PowerCLI 5.0 build 395016
   VMWare vSphere PowerCLI 5.0 build 435427

If it matters, we are using vCenter 4.1 with updates through Aug-2011

Reply
0 Kudos
SCC
Contributor
Contributor

Based on this version, can you see any function/type name changes?

Don

Reply
0 Kudos
LucD
Leadership
Leadership

Yes, I know the typename that has changed, but I'm afraid I don't know about the parameters that the constructor requires.

The function should now be

function New-VIAccount($principal) {
$flags = `
  [System.Reflection.BindingFlags]::NonPublic    -bor
  [System.Reflection.BindingFlags]::Public       -bor
  [
System.Reflection.BindingFlags]::DeclaredOnly -bor
  [System.Reflection.BindingFlags]::Instance
$method = $defaultviserver.GetType().GetMethods($flags) |   where { $_.Name -eq "VMware.VimAutomation.ViCore.Interop.V1.VIObjectCoreInterop.get_Client" } $client = $method.Invoke($global:DefaultVIServer, $null) Write-Output `
  (
New-Object VMware.VimAutomation.Client20.PermissionManagement.VCUserAccountImpl `
   -ArgumentList ????) }

Notice the ???? in the ArgumentList parameter, that's where I'm stuck.

Looking at the getConstructor method, I can see 3 variations, but I'm afraid I don't know what each parameter expects.

System.Reflection.ConstructorInfo GetConstructor(System.Reflection.BindingFlags bindingAttr,
System.Reflection.Binder binder, System.Reflection.CallingConventions callConvention,
type[] types,
System.Reflection.ParameterModifier[] modifiers), System.Reflection.ConstructorInfo GetConstructor(System.Reflection.BindingFlags bindingAttr,
System.Reflection.Binder binder, type[] types,
System.Reflection.ParameterModifier[] modifiers), System.Reflection.ConstructorInfo GetConstructor(type[] types)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

SCC
Contributor
Contributor

OK Thanks for looking LucD. I appreciate you taking the time. I am still in a pickel in how to do this (via a script) if anyone else has any ideas?

Thanks

Don

Reply
0 Kudos
LucD
Leadership
Leadership

Perhaps someone from the PowerCLI Dev Team could tell us what the exact parameters for the Constructor should be ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ykalchev
VMware Employee
VMware Employee

I can suggest to increate result count of returned accounts from the AD domin or use the script posted in this thread: http://communities.vmware.com/message/1840112#1840112

Using reflection and creating VIAccount is not a good idea - there is a great chance to be broken each time we release a new version of PowerCLI.

Yasen Kalchev, vSM Dev Team
Reply
0 Kudos