HI all,
So i wrote this quick script to generate a report of VMs info and more specifically, who owns them.
The issue is that I am getting some rows with nothing but the Owner, and all the other colemns are empty.
Connect-Ciserver -Server VCDserver -User Username -Password Password
$report = @()
$Vapp = "*"
foreach($Vapp in Get-CiVapp -Name $vapp){
$vm = $vapp | get-civm
$row = "" | Select "Name", "VApp", "Org", "Status", "Operating System", "CpuCount", "Memory (GB)", Owner
$row."Name" = $vm.Name
$row."Vapp" = $vm.vapp
$row."Org" = $vm.Org
$row."Status" = $vm.Status
$row."Operating System" = $vm.GuestOSFullName
$row."CpuCount" = $vm.CpuCount
$row."Memory (GB)" = ($VM | Measure-Object -Property MemoryMB -Sum).Sum/1024
$row.Owner = $vapp.Owner
$report += $row
}
$report | export-csv "C:\VcloudVMsNEW.csv" -NoTypeInformation -UseCulture
Any ideas?
Hi, jkb5054,
It seems to me that there is an error in your script - the $vm is an array of vms, shouldn't you be iterating through it to generate a row for each vm?
The following just modifies your script:
foreach($Vapp in Get-CiVapp -Name $vapp){
$vm = $vapp | get-civm
$vm | % {
$row = "" | Select "Name", "VApp", "Org", "Status", "Operating System", "CpuCount", "Memory (GB)", Owner
$row."Name" = $_.Name
$row."Vapp" = $Vapp #we know the vapp already
$row."Org" = $_.Org
$row."Status" = $_.Status
$row."Operating System" = $_.GuestOSFullName
$row."CpuCount" = $_.CpuCount
$row."Memory (GB)" = ($_ | Measure-Object -Property MemoryMB -Sum).Sum/1024
$row.Owner = $vapp.Owner
$report += $row
}
}
Hope this helps,
Ogniana
Message was edited by: orainova b/c of typos
Could this be a permission problem ?
What role does the user you are connected with have ?
Are the vApps that don't show all the properties perhaps shared to a limited set of usesr ?
Can you see all the properties in the web interface ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The user I am connecting with is System.
There shouldn't be a permission issue.
I am thinking it has to do with the relationship that a Vapp and a VM have. The main goal is to get the owner of the VM. But the owner field is only accessable through the use of get-civapp.
Do you know another way to obtain this information?
Thanks in advance!
Hi, jkb5054,
It seems to me that there is an error in your script - the $vm is an array of vms, shouldn't you be iterating through it to generate a row for each vm?
The following just modifies your script:
foreach($Vapp in Get-CiVapp -Name $vapp){
$vm = $vapp | get-civm
$vm | % {
$row = "" | Select "Name", "VApp", "Org", "Status", "Operating System", "CpuCount", "Memory (GB)", Owner
$row."Name" = $_.Name
$row."Vapp" = $Vapp #we know the vapp already
$row."Org" = $_.Org
$row."Status" = $_.Status
$row."Operating System" = $_.GuestOSFullName
$row."CpuCount" = $_.CpuCount
$row."Memory (GB)" = ($_ | Measure-Object -Property MemoryMB -Sum).Sum/1024
$row.Owner = $vapp.Owner
$report += $row
}
}
Hope this helps,
Ogniana
Message was edited by: orainova b/c of typos
Thanks Orainova! Works great now.
Some users of the vcloud have made a Vapp, and not assigned any VM's to it.
i.e the report shows up with no info within that row other than the vapp name and the owner.
Is there anyway to have it not show those columns?
To suppress the vApps that have no VMs assigned, you could do something like this
foreach($Vapp in Get-CiVapp -Name $vapp){ $vm = $vapp | get-civm if($vm) { $vm | %{ $row = "" | Select "Name", "VApp", "Org", "Status", "Operating System", "CpuCount", "Memory (GB)", Owner
$row."Name" = $_.Name $row."Vapp" = $Vapp #we know the vapp already
$row."Org" = $_.Org $row."Status" = $_.Status $row."Operating System" = $_.GuestOSFullName $row."CpuCount" = $_.CpuCount $row."Memory (GB)" = ($_ | Measure-Object -Property MemoryMB -Sum).Sum/1024
$row.Owner = $vapp.Owner $report += $row
} } }
When there are no VMs ($vm will equal $null), the code block will not be executed.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
It only returned the vapp name and the owner.
$vapp = "*"
foreach($Vapp in Get-CiVapp -Name $vapp){
$vm = $vapp | get-civm
if($vm) {
$row = "" | Select "Name", "VApp", "Org", "Status", "Operating System", "CpuCount", "Memory (GB)", Owner
$row."Name" = $_.Name
$row."Vapp" = $Vapp #we know the vapp already
$row."Org" = $_.Org
$row."Status" = $_.Status
$row."Operating System" = $_.GuestOSFullName
$row."CpuCount" = $_.CpuCount
$row."Memory (GB)" = ($_ | Measure-Object -Property MemoryMB -Sum).Sum/1024
$row.Owner = $vapp.Owner
$report += $row
}
}
Oops, I forgot the inner loop.
The code above is corrected.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Works great! Thanks
Is it possibly to output to html AND a csv file within this same script?
Sure, add these 2 lines to the end of script
$report | Export-Csv "C:\report.csv" -NoTypeInformation -UseCulture
$report | ConvertTo-HTML | Out-File "C:\report.html"
You can be very fancy with the HTML output by using style sheets.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
great! and the style sheets made it really nice.
How about sorting the data?
I tried putting the sort function in the second loop, but it seemed to fail on me.
I was trying to sort by $vm.Org.
Just a Sort-Object before you create the output
$report = $report | Sort-Object -Property Org
$report | Export-Csv "C:\report.csv" -NoTypeInformation -UseCulture
$report | ConvertTo-HTML -he | Out-File "C:\report.html"
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Here is the finished product (for now).
Thanks for all the help LucD!!!!!
$CIservername = "Vcloudservername"
$UserID = "userID"
$password = "password"
Connect-Ciserver -Server $CIservername -User $UserID -Password $Password
$smtpServer = "exchange.server.com"
$ReportFrom = "yourname@domain"
$report = @()
$date = Get-Date
$vapp = "*"
foreach($Vapp in Get-CiVapp -Name $vapp){
$vm = $vapp | get-civm
if($vm) {
$vm | %{
$row = "" | Select "Name", "VApp", "Org", "Status", "Operating System", "CpuCount", "Memory (GB)", Owner
$row."Name" = $_.Name
$row."Vapp" = $Vapp #we know the vapp already
$row."Org" = $_.Org
$row."Status" = $_.Status
$row."Operating System" = $_.GuestOSFullName
$row."CpuCount" = $_.CpuCount
$row."Memory (GB)" = ($_ | Measure-Object -Property MemoryMB -Sum).Sum/1024
$row.Owner = $vapp.Owner
$report += $row
}
}
}
#Html style sheet
$a = "<style>"
$a = $a + "BODY{background-color:lavender;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 1px;padding: 2px;border-style: solid;border-color: black;background-color:whitesmoke}"
$a = $a + "TD{border-width: 1px;padding: 2px;border-style: solid;border-color: black;background-color:slategrey}"
$a = $a + "</style>"
#sort by Org property
$report = $report | Sort-Object -Property Org
$report | export-csv "C:\VcloudVMs.csv" -NoTypeInformation -UseCulture
#html output
$report | ConvertTo-HTML -head $a -body "<H1>Vcloud Inventory Report</H1>
<p>
<a href='http://webserver.com/VcloudVMs.csv'>Download as CSV</a><br>
<font size='2' face='arial' color='red'>Last Updated: $date </font>
</p>" | Out-File "C:\VcloudInventroy.html"
Invoke-Expression C:\VcloudInventroy.html
$billiejean = "C:\VcloudVMs.csv"
$msg = new-object Net.Mail.MailMessage
$att = new-object Net.Mail.Attachment($billiejean)
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = $ReportFrom
$msg.To.Add("person.to.recieve@domain.com")
$msg.Bcc.Add("maybe.bcc.yourself?@domain.com")
$msg.Subject = "Vcloud VM report for $date"
$msg.Body = "Report Attached or you may view it at http://webserver.com/VcloudInventroy.html"
$msg.Attachments.Add($att)
$smtp.Send($msg)
$msg.Dispose()
Disconnect-CIServer -Server $CIserver -confirm:$False
I have recently discovered that this only pulls the first VM in each Vapp. So if a Vapp has more than one VM is does not show all of them.
I have tried entering another loop within ( foreach $vm in $vapp) but it seems to come out with the same result.
Any thoughts?
cls
$vapp = "*"
foreach($Vapp in Get-CiVapp -Name $vapp){
$vm = $vapp | get-civm
if($vm) {
$vm | %{
$row = "" | Select "Name", "VApp", "Org", "Status", "Operating System", "CpuCount", "Memory (GB)", Owner
$row."Name" = $_.Name
$row."Vapp" = $Vapp #we know the vapp already
$row."Org" = $_.Org
$row."Status" = $_.Status
$row."Operating System" = $_.GuestOSFullName
$row."CpuCount" = $_.CpuCount
$row."Memory (GB)" = ($_ | Measure-Object -Property MemoryMB -Sum).Sum/1024
$row.Owner = $vapp.Owner
$report += $row