Hi All,
I'm trying to generate the cluster report for multiple vCenter, but it generating output for only one vcenter from the vCenter lists C:\Temp\VC.txt.
Looking to modify the script and after generating report for each vcenter it should disconnect it and connect new vcenter. Any pointers will be much appreciated.
### load credentails for logging to vcenter
Get-Module -Name VMware* -ListAvailable | Import-Module
$userbane = "admin"
$encrypted = Get-Content D:\Scripts\scriptsencrypted_paswd_admin.txt | ConvertTo-SecureString
$Cred = New-Object System.Management.Automation.PsCredential($userbane, $encrypted)
$vCenters = (Get-Content "C:\Temp\VC.txt")
foreach($vcenter in $vcenters){
Connect-ViServer $vcenter -Credential $Cred}
foreach($cluster in Get-Cluster){
$esx = $cluster | Get-VMHost
}
$cluster | Select @{N="DCname";E={(Get-Datacenter -Cluster $cluster).Name}},
@{N="Clustername";E={$cluster.Name}},
@{N="Total Physical Memory (GB)";E={($esx | Measure-Object -Property MemoryTotalGB -Sum).Sum}},
@{N="Configured Memory GB";E={($esx | Measure-Object -Property MemoryUsageGB -Sum).Sum}},
@{N="Available Memory (GB)";E={($esx | Measure-Object -InputObject {$_.MemoryTotalGB - $_.MemoryUsageGB} -Sum).Sum}},
@{N="Num of CPUs)";E={($esx | Measure-Object -Property NumCpu -Sum).Sum}},
@{N="Total CPU (Mhz)";E={($esx | Measure-Object -Property CpuTotalMhz -Sum).Sum}},
@{N="Configured CPU (Mhz)";E={($esx | Measure-Object -Property CpuUsageMhz -Sum).Sum}},
@{N="Number of VMs";E={($esx | Get-VM).count}},
@{N="Number of ESXi Hosts";E={$esx.count}} | Export-Csv -path c:\Temp\DSClusterStatus.csv -NoTypeInformation -UseCulture
Send-MailMessage -From 'mail@domain.com ' -To 'mail@domain.com ' -Subject 'Remote Site vCenter Report' -Body " Report. Sending now." -
Attachments C:\Temp\DSClusterStatus.csv -Priority High -DeliveryNotificationOption OnSuccess, OnFailure -SmtpServer 'smtp.aa.domain.com'
Thanks
V
Try like this
$encrypted = Get-Content D:\Scripts\scriptsencrypted_paswd_admin.txt | ConvertTo-SecureString
$Cred = New-Object System.Management.Automation.PsCredential($userbane, $encrypted)
$vCenters = (Get-Content "C:\Temp\VC.txt")
$report = @()
foreach ($vcenter in $vcenters) {
Connect-VIServer $vcenter -Credential $Cred
foreach ($cluster in Get-Cluster -Server $vcenter) {
$esx = $cluster | Get-VMHost
$report += $cluster | Select-Object @{N = "DCname"; E = { (Get-Datacenter -Cluster $cluster).Name } },
@{N = "Clustername"; E = { $cluster.Name } },
@{N = "Total Physical Memory (GB)"; E = { ($esx | Measure-Object -Property MemoryTotalGB -Sum).Sum } },
@{N = "Configured Memory GB"; E = { ($esx | Measure-Object -Property MemoryUsageGB -Sum).Sum } },
@{N = "Available Memory (GB)"; E = { ($esx | Measure-Object -InputObject { $_.MemoryTotalGB - $_.MemoryUsageGB } -Sum).Sum } },
@{N = "Num of CPUs)"; E = { ($esx | Measure-Object -Property NumCpu -Sum).Sum } },
@{N = "Total CPU (Mhz)"; E = { ($esx | Measure-Object -Property CpuTotalMhz -Sum).Sum } },
@{N = "Configured CPU (Mhz)"; E = { ($esx | Measure-Object -Property CpuUsageMhz -Sum).Sum } },
@{N = "Number of VMs"; E = { ($esx | Get-VM).count } },
@{N = "Number of ESXi Hosts"; E = { $esx.count } }
}
Disconnect-VIServer -Server $vcenter -Confirm:$false
}
$report | Export-Csv -path c:\Temp\DSClusterStatus.csv -NoTypeInformation -UseCulture
$sMail = @{
From = 'mail@domain.com'
To = 'mail@domain.com'
Subject = 'Remote Site vCenter Report'
Body = "Report. Sending now."
Attachments = 'C:\Temp\DSClusterStatus.csv'
Priority = 'High'
DeliveryNotificationOption = 'OnSuccess', 'OnFailure'
SmtpServer = 'smtp.aa.domain.com'
}
Send-MailMessage @sMail
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You didn't mention if you want a cluster report per cluster or one report for all clusters.
In any case, the code blocks have some issues.
This produces 1 email per cluster.
$encrypted = Get-Content D:\Scripts\scriptsencrypted_paswd_admin.txt | ConvertTo-SecureString
$Cred = New-Object System.Management.Automation.PsCredential($userbane, $encrypted)
$vCenters = (Get-Content "C:\Temp\VC.txt")
foreach ($vcenter in $vcenters) {
Connect-VIServer $vcenter -Credential $Cred
foreach ($cluster in Get-Cluster) {
$esx = $cluster | Get-VMHost
$cluster | Select-Object @{N = "DCname"; E = { (Get-Datacenter -Cluster $cluster).Name } },
@{N = "Clustername"; E = { $cluster.Name } },
@{N = "Total Physical Memory (GB)"; E = { ($esx | Measure-Object -Property MemoryTotalGB -Sum).Sum } },
@{N = "Configured Memory GB"; E = { ($esx | Measure-Object -Property MemoryUsageGB -Sum).Sum } },
@{N = "Available Memory (GB)"; E = { ($esx | Measure-Object -InputObject { $_.MemoryTotalGB - $_.MemoryUsageGB } -Sum).Sum } },
{N = "Num of CPUs)"; E = { ($esx | Measure-Object -Property NumCpu -Sum).Sum } },
@{N = "Total CPU (Mhz)"; E = { ($esx | Measure-Object -Property CpuTotalMhz -Sum).Sum } },
@{N = "Configured CPU (Mhz)"; E = { ($esx | Measure-Object -Property CpuUsageMhz -Sum).Sum } },
@{N = "Number of VMs"; E = { ($esx | Get-VM).count } },
@{N = "Number of ESXi Hosts"; E = { $esx.count } } | Export-Csv -path c:\Temp\DSClusterStatus.csv -NoTypeInformation -UseCulture
$sMail = @{
From = 'mail@domain.com'
To = 'mail@domain.com'
Subject = 'Remote Site vCenter Report'
Body = "Report. Sending now."
Attachments = 'C:\Temp\DSClusterStatus.csv'
Priority = 'High'
DeliveryNotificationOption = 'OnSuccess', 'OnFailure'
SmtpServer = 'smtp.aa.domain.com'
}
Send-MailMessage @sMail
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
Thanks for your prompt response. Yes, I'm looking report for all the clusters in each vcenter.
Does each vCenter will disconnect after running the script ? Since, i need to run on a daily basis through Task scheduler.
Thanks
V
This does the disconnect and sends an individual report for all clusters in a vCenter.
$encrypted = Get-Content D:\Scripts\scriptsencrypted_paswd_admin.txt | ConvertTo-SecureString
$Cred = New-Object System.Management.Automation.PsCredential($userbane, $encrypted)
$vCenters = (Get-Content "C:\Temp\VC.txt")
foreach ($vcenter in $vcenters) {
$report = @()
Connect-VIServer $vcenter -Credential $Cred
foreach ($cluster in Get-Cluster -Server $vcenter) {
$esx = $cluster | Get-VMHost
$report += $cluster | Select-Object @{N = "DCname"; E = { (Get-Datacenter -Cluster $cluster).Name } },
@{N = "Clustername"; E = { $cluster.Name } },
@{N = "Total Physical Memory (GB)"; E = { ($esx | Measure-Object -Property MemoryTotalGB -Sum).Sum } },
@{N = "Configured Memory GB"; E = { ($esx | Measure-Object -Property MemoryUsageGB -Sum).Sum } },
@{N = "Available Memory (GB)"; E = { ($esx | Measure-Object -InputObject { $_.MemoryTotalGB - $_.MemoryUsageGB } -Sum).Sum } },
{N = "Num of CPUs)"; E = { ($esx | Measure-Object -Property NumCpu -Sum).Sum } },
@{N = "Total CPU (Mhz)"; E = { ($esx | Measure-Object -Property CpuTotalMhz -Sum).Sum } },
@{N = "Configured CPU (Mhz)"; E = { ($esx | Measure-Object -Property CpuUsageMhz -Sum).Sum } },
@{N = "Number of VMs"; E = { ($esx | Get-VM).count } },
@{N = "Number of ESXi Hosts"; E = { $esx.count } }
}
$report | Export-Csv -path c:\Temp\DSClusterStatus.csv -NoTypeInformation -UseCulture
$sMail = @{
From = 'mail@domain.com'
To = 'mail@domain.com'
Subject = 'Remote Site vCenter Report'
Body = "Report. Sending now."
Attachments = 'C:\Temp\DSClusterStatus.csv'
Priority = 'High'
DeliveryNotificationOption = 'OnSuccess', 'OnFailure'
SmtpServer = 'smtp.aa.domain.com'
}
Send-MailMessage @sMail
Disconnect-VIServer -Server $vcenter -Confirm:$false
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
I tried the updated code, but it throws an error
Also, still generates report for one vc only using the first code.
Thanks
V
Then you probably made a mistake with the copy.
For me, that script is working.
Attach the version you are using.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
You are right. It was a copy paste issue. One challenge here for me to filter each vc report because all the vc report name is same . Can all the vc report can be consolidated into single attachment (report) instead of sending report for each vc one at a time which is causing mail spam. Please if possible let me know.
Thanks
V
Try like this
$encrypted = Get-Content D:\Scripts\scriptsencrypted_paswd_admin.txt | ConvertTo-SecureString
$Cred = New-Object System.Management.Automation.PsCredential($userbane, $encrypted)
$vCenters = (Get-Content "C:\Temp\VC.txt")
$report = @()
foreach ($vcenter in $vcenters) {
Connect-VIServer $vcenter -Credential $Cred
foreach ($cluster in Get-Cluster -Server $vcenter) {
$esx = $cluster | Get-VMHost
$report += $cluster | Select-Object @{N = "DCname"; E = { (Get-Datacenter -Cluster $cluster).Name } },
@{N = "Clustername"; E = { $cluster.Name } },
@{N = "Total Physical Memory (GB)"; E = { ($esx | Measure-Object -Property MemoryTotalGB -Sum).Sum } },
@{N = "Configured Memory GB"; E = { ($esx | Measure-Object -Property MemoryUsageGB -Sum).Sum } },
@{N = "Available Memory (GB)"; E = { ($esx | Measure-Object -InputObject { $_.MemoryTotalGB - $_.MemoryUsageGB } -Sum).Sum } },
@{N = "Num of CPUs)"; E = { ($esx | Measure-Object -Property NumCpu -Sum).Sum } },
@{N = "Total CPU (Mhz)"; E = { ($esx | Measure-Object -Property CpuTotalMhz -Sum).Sum } },
@{N = "Configured CPU (Mhz)"; E = { ($esx | Measure-Object -Property CpuUsageMhz -Sum).Sum } },
@{N = "Number of VMs"; E = { ($esx | Get-VM).count } },
@{N = "Number of ESXi Hosts"; E = { $esx.count } }
}
Disconnect-VIServer -Server $vcenter -Confirm:$false
}
$report | Export-Csv -path c:\Temp\DSClusterStatus.csv -NoTypeInformation -UseCulture
$sMail = @{
From = 'mail@domain.com'
To = 'mail@domain.com'
Subject = 'Remote Site vCenter Report'
Body = "Report. Sending now."
Attachments = 'C:\Temp\DSClusterStatus.csv'
Priority = 'High'
DeliveryNotificationOption = 'OnSuccess', 'OnFailure'
SmtpServer = 'smtp.aa.domain.com'
}
Send-MailMessage @sMail
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
It worked smoothly. Thank you for your help . One last help i need from you. Why does the column "F" appear as command. see below.
Thanks
V
Because I lost the @ character at the start of the calculated property.
I corrected the code above
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference