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
I took that from your script, that should be
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
What do you already have?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
$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
How we can we export it to CSV file ?
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
LucD Apologies, i was only trying to help. Will leave you to it :smileycheck:
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
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
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
I have copied the code in powershell and executed it from powercli . I am not getting output
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
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
Are you deleting parts of the replies?
I'm pretty sure I just posted this reply.
Try like this
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
What about pulling cluster name and Guest OS type ?
What about my question?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
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
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
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}},
I took that from your script, that should be
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference