sanjaygpt171
Enthusiast
Enthusiast

Script to pull datacenter, clusters, current power state, OS version and custom attributes of VM

Jump to solution

Hi All,

Could you please help me with script to pull below information from VCenter.

list of all VMs, the datacenter, clusters, current power state, OS version, with the following custom attributes:

1)BU

2)Team

3)Use Case

4)Managed By

5)Owned By

6)Department

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

I took that from your script, that should be

    @{N='powerstate';E={$_.Guest.State}},


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

View solution in original post

0 Kudos
18 Replies
LucD
Leadership
Leadership

What do you already have?


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

0 Kudos
sanjaygpt171
Enthusiast
Enthusiast

$report = @()

foreach($vmName in (Get-VM))

{

$vm = Get-VM -Name $vmName

  # Get-HardDisk -VM $vm |

   ForEach-Object {

   #$HardDisk = $_

   $row = "" | Select Hostname, VM, GuestName, powerstate, Guest,  Department, Use_Case, Team, Business_unit, Managed_By, Owned_By

  $row.Hostname = $vm.VMHost.Name

   $row.VM = $VM.Name

   $row.GuestName = $vm.Guest.HostName

    $row.powerstate = $vm.Guest.powerstate

    $row.Guest = $vm.Guest.powerstate

   $row.Department = (Get-Annotation -CustomAttribute 'Department' -Entity $vm).Value

  

   $row.Use_Case = (Get-Annotation -CustomAttribute 'Use Case' -Entity $vm).Value

   $row.Team = (Get-Annotation -CustomAttribute 'Team' -Entity $vm).Value

   $row.Business_unit = (Get-Annotation -CustomAttribute 'Business Unit' -Entity $vm).Value

   $row.Managed_By = (Get-Annotation -CustomAttribute 'Managed By' -Entity $vm).Value

   $row.Owned_By = (Get-Annotation -CustomAttribute 'Owned By' -Entity $vm).Value

   $report += $row

  }

}

$report | Export-Csv -Path C:\users\sanjay\usecase.csv -NoTypeInformation -UseCultur

0 Kudos
sanjaygpt171
Enthusiast
Enthusiast

How we can we export it to CSV file ?

0 Kudos
sanjaygpt171
Enthusiast
Enthusiast

I have executed command on single VM , it has taking almost 20 mins and it's still running. Also, i am not getting output on CSV file

0 Kudos
T180985
Expert
Expert

LucD​ Apologies, i was only trying to help. Will leave you to it :smileycheck:

Please mark helpful or correct if my answer resolved your issue. How to post effectively on VMTN https://communities.vmware.com/people/daphnissov/blog/2018/12/05/how-to-ask-for-help-on-tech-forums
0 Kudos
sanjaygpt171
Enthusiast
Enthusiast

LucD​ Can you help us in fixing this  , i am not getting any value for custom attributes >

Get-VM   | Select name, Powerstate,   @{N=“Datacenter“;E={$_ | Get-Datacenter}},   @{N=“Cluster“;E={$_ | Get-Cluster}},   @{N=“OS“;E={$_.ExtensionData.Guest.GuestFullName}},,   @{N=“Team“;E={($_ | Get-CustomAttribute -Name "Team").value}}   | Export-Csv -Path C:\users\sanja\y\usecase.csv  -NoTypeInformation -UseCultur

pastedImage_0.png

0 Kudos
LucD
Leadership
Leadership

That was not my intention T180985

I just wanted to explain that Format-Table has different rules than Export-Csv.


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

0 Kudos
LucD
Leadership
Leadership

We had a similar issue in your other thread Re: PowerCLI scrip to get a list of VMs, Attributes (Team and use case) and used disk space

Can you show a screenshot of how you execute this?


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

0 Kudos
sanjaygpt171
Enthusiast
Enthusiast

I have copied the code in powershell and executed it from powercli . I am not getting output Smiley Sad

pastedImage_3.png

pastedImage_7.png

0 Kudos
LucD
Leadership
Leadership

Did you save the file?
The asterisk in the tab indicates the file was not saved


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

0 Kudos
sanjaygpt171
Enthusiast
Enthusiast

LucD​ I have used below script and it's working, Could you please let me know, if i want to add cluster name and guest OS information, what i need to add in script ?

$report = @()

foreach($vmName in (Get-Content -Path C:\TEMP\vmliste.txt))

## foreach($vmName in (Get-VM))

{

$vm = Get-VM -Name $vmName

  # Get-HardDisk -VM $vm |

   ForEach-Object {

   #$HardDisk = $_

   $row = "" | Select Hostname, VM, GuestName, powerstate, Guest,  Department, Use_Case, Team, Business_unit, Managed_By, Owned_By

  $row.Hostname = $vm.VMHost.Name

   $row.VM = $VM.Name

   $row.GuestName = $vm.Guest.HostName

    $row.powerstate = $vm.powerstate

    $row.Guest = $vm.VMGuest

   $row.Department = (Get-Annotation -CustomAttribute 'Department' -Entity $vm).Value

  

   $row.Use_Case = (Get-Annotation -CustomAttribute 'Use Case' -Entity $vm).Value

   $row.Team = (Get-Annotation -CustomAttribute 'Team' -Entity $vm).Value

   $row.Business_unit = (Get-Annotation -CustomAttribute 'Business Unit' -Entity $vm).Value

   $row.Managed_By = (Get-Annotation -CustomAttribute 'Managed By' -Entity $vm).Value

   $row.Owned_By = (Get-Annotation -CustomAttribute 'Owned By' -Entity $vm).Value

   $report += $row

  }

}

$report | Export-Csv -Path C:\users\sguptaadmin\power.csv -NoTypeInformation -UseCultur

0 Kudos
LucD
Leadership
Leadership

Are you deleting parts of the replies?
I'm pretty sure I just posted this reply.

Try like this

Get-VM -Name (Get-Content -Path C:\Temp\vmliste.txt) -PipelineVariable vm |

Select @{N='Hostname';E={$_.VMHost.Name}},

    @{N='VM';E={$_.Name}},

    @{N='GuestName';E={$_.Guest.HostName}},

    @{N='powerstate';E={$_.Guest.powerstate}},

    @{N='Guest';E={$_.Guest.powerstate}},

    @{N='Department';E={(Get-Annotation -CustomAttribute 'Department' -Entity $_).Value}},

    @{N='Use_Case';E={(Get-Annotation -CustomAttribute 'Use Case' -Entity $_).Value}},

    @{N='Team';E={(Get-Annotation -CustomAttribute 'Team' -Entity $_).Value}},

    @{N='Business_unit';E={(Get-Annotation -CustomAttribute 'Business Unit' -Entity $_).Value}},

    @{N='Managed_By';E={(Get-Annotation -CustomAttribute 'Managed By' -Entity $_).Value}},

    @{N='Owned_By';E={(Get-Annotation -CustomAttribute 'Owned By' -Entity $_).Value}} |

Export-Csv -Path C:\users\sanjay\usecase.csv -NoTypeInformation -UseCultur


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

0 Kudos
sanjaygpt171
Enthusiast
Enthusiast

What about pulling cluster name and Guest OS type ?

0 Kudos
LucD
Leadership
Leadership

What about my question?


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

0 Kudos
sanjaygpt171
Enthusiast
Enthusiast

Previously, i was getting some other error, which i already fixed and landed up with new issue in below script, that's why i deleted the older my comments not yours.

I just want to pull clustername and guestOS type field and add it to below script and rest all is working fine

$report = @()

foreach($vmName in (Get-Content -Path C:\TEMP\vmliste.txt))

## foreach($vmName in (Get-VM))

{

$vm = Get-VM -Name $vmName

   ForEach-Object {

   $row = "" | Select Hostname, VM, GuestName, powerstate, Guest,  Department, Use_Case, Team, Business_unit, Managed_By, Owned_By

  $row.Hostname = $vm.VMHost.Name

   $row.VM = $VM.Name

   $row.GuestName = $vm.Guest.HostName

    $row.powerstate = $vm.powerstate

    $row.Guest = $vm.OSFullName

   $row.Department = (Get-Annotation -CustomAttribute 'Department' -Entity $vm).Value

  

   $row.Use_Case = (Get-Annotation -CustomAttribute 'Use Case' -Entity $vm).Value

   $row.Team = (Get-Annotation -CustomAttribute 'Team' -Entity $vm).Value

   $row.Business_unit = (Get-Annotation -CustomAttribute 'Business Unit' -Entity $vm).Value

   $row.Managed_By = (Get-Annotation -CustomAttribute 'Managed By' -Entity $vm).Value

   $row.Owned_By = (Get-Annotation -CustomAttribute 'Owned By' -Entity $vm).Value

   $report += $row

  }

}

$report | Export-Csv -Path C:\users\a\power.csv -NoTypeInformation -UseCultur

0 Kudos
LucD
Leadership
Leadership

When you delete your entries, you also delete all replies on those entries.
That is not very nice for others who have replied.

Try like this

Get-VM -Name (Get-Content -Path C:\Temp\vmliste.txt) -PipelineVariable vm |

Select @{N='Hostname';E={$_.VMHost.Name}},

    @{N='VM';E={$_.Name}},

    @{N='GuestOS';E={$_.Guest.OSFullName}},

    @{N='Cluster';E={(Get-Cluster -VM $_).Name}},

    @{N='GuestName';E={$_.Guest.HostName}},

    @{N='powerstate';E={$_.Guest.State}},

    @{N='Guest';E={$_.Guest.powerstate}},

    @{N='Department';E={(Get-Annotation -CustomAttribute 'Department' -Entity $_).Value}},

    @{N='Use_Case';E={(Get-Annotation -CustomAttribute 'Use Case' -Entity $_).Value}},

    @{N='Team';E={(Get-Annotation -CustomAttribute 'Team' -Entity $_).Value}},

    @{N='Business_unit';E={(Get-Annotation -CustomAttribute 'Business Unit' -Entity $_).Value}},

    @{N='Managed_By';E={(Get-Annotation -CustomAttribute 'Managed By' -Entity $_).Value}},

    @{N='Owned_By';E={(Get-Annotation -CustomAttribute 'Owned By' -Entity $_).Value}} |

Export-Csv -Path C:\users\sanjay\usecase.csv -NoTypeInformation -UseCultur


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

0 Kudos
sanjaygpt171
Enthusiast
Enthusiast

Thanks LucD​ , all is working fine except powerstate of VM . Below 2 code is not pulling any data ?

@{N='powerstate';E={$_.Guest.PowerState}},

    @{N='Guest';E={$_.Guest.powerstate}},

0 Kudos
LucD
Leadership
Leadership

I took that from your script, that should be

    @{N='powerstate';E={$_.Guest.State}},


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

View solution in original post

0 Kudos