VMware Cloud Community
AGFlora
Enthusiast
Enthusiast
Jump to solution

How to list perms for 1 datacenter by name

Tried modifying LucD's code but to no avail:

$report = @()

$inherited = $true

$authMgr = Get-View AuthorizationManager

$roles = @{}

foreach($role in $authMgr.RoleList){

$roles[http://$role.RoleId|http://$role.RoleId] = $role.Name

}

Get-DataCenter -Name "DC_name" {

$exist = Get-VIObjectByVIView -MORef $_.MoRef -ErrorAction SilentlyContinue

if($exist -ne $null){

$current = Get-View $_.Parent

$path = $_.Name

do {

$parent = $current

if($parent.Name -ne "vm"){$path = $parent.Name + "\" + $path}

$current = Get-View $current.Parent

} while ($current.Parent -ne $null)

$permissions = $authMgr.RetrieveEntityPermissions($_.MoRef, $inherited)

if($permissions.Length -ne 0){

foreach($perm in $permissions){

$row = "" | select Name, Path, Principal, Group, Role, DefinedIn

$row.Name = $_.Name

$row.Path = $path

$row.Principal = $perm.Principal

$row.Group = $perm.Group

$row.Role = $roles[http://$perm.RoleId|http://$perm.RoleId]

$row.DefinedIn = (Get-View -Id $perm.Entity).Name

$report += $row

}

}

else{

$row = "" | select Name, Path, Princiapl, Group

$row.Name = $_.Name

$row.Path = $path

$report += $row

}

}

}

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Works for me.

Where do you run this from ? (PowerCLI prompt, PowerGui, Vesi...)


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

View solution in original post

Reply
0 Kudos
17 Replies
LucD
Leadership
Leadership
Jump to solution

Try the attached script.

I suspect you want to report the permissions for a datacenter and all its child objects ?


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

Reply
0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

Yes. I get the following error when I run the script:

The argument cannot be null or empty.

At :line:52 char:31

+ Get-DataCenter -Name "DC_name" | % <<<< {

PG script editor

Reply
0 Kudos
harkamal
Expert
Expert
Jump to solution

Omit

-Name "DC_name"

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

There was a Get-View missing after the Get-Datacenter cmdlet.

Try this script.


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

Reply
0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

still getting the same error

Reply
0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

nope...still getting the same error.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Could you give me the complete error message (with the line number) and when using the script from my last attached file ?


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

Reply
0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

The argument cannot be null or empty.

At :line:52 char:52

+ Get-DataCenter -Name "DC_Name" | Get-View | % <<<< {

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You do have a datacenter that is called "DC_Name" ?

Does this return anything ?

Get-Datacenter -Name "DC_Name"


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

Reply
0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

yes to both questions...

ustomFields : {AutoWM CR, BarCode, Site Wide AutoWM CR}

Id : Datacenter-datacenter-134

Name : DC_Name

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Judging from the line number in the error message there are other lines before the line that contains the Get-Datacenter cmdlet.

Is it possible to attach the complete script ?


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

Reply
0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

See attached.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Works for me.

Where do you run this from ? (PowerCLI prompt, PowerGui, Vesi...)


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

Reply
0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

powergui...I'm going to try it from the ps prompt

Reply
0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

OK...here's the results after running the script from the ps prompt:

I got some output to the csv file but there was a slightly differrent error this time.

Get-View ; the argument cannot be null or empty.

At C:\windows\system32\WindowsPowerShell\v1.0\test.ps1:59 char:23

$current = Get-View <<<< $current.parent

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You must be encountering a node with a parent that is equal to $null.

Which is strange since the loop (do while) is stopping on that condition.

You could perhaps insert a Write-Host $path just before the statement.

That would allow you to see where it goes wrong.


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

AGFlora
Enthusiast
Enthusiast
Jump to solution

The attached script works better with no errors....

Your advice to "You could perhaps insert a Write-Host $path just before the statement.

That would allow you to see where it goes wrong." will come in handy for troubleshooting.

Thanks!

Reply
0 Kudos