Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Define custom properties
- # Percentage of configured virtual machine “physical” memory
- # active ÷ virtual machine configured size
- # 10 samples range is 7 days
- $MemoryUsageAveragePercentProp = @{
- Name = 'MemoryUsageAveragePercent'
- Expression = {
- $value = $_ | Get-Stat -Stat "mem.usage.average" -Start (Get-Date).AddDays(-7) -Finish (Get-Date) -MaxSamples 10 | Measure-Object -Property Value -Average
- [Math]::Round($value.Average, 0)
- }
- }
- $GuestDiskCapacityGBProp = @{
- Name = 'GuestDiskCapacityGB'
- Expression = {
- [Math]::Round(($_.Guest.Disks.CapacityGB), 0)
- }
- }
- $GuestDiskFreeGBProp = @{
- Name = 'GuestDiskFreeGB'
- Expression = {
- [Math]::Round(($_.Guest.Disks.FreeSpaceGB), 0)
- }
- }
- $GuestFreeSpacePercentProp = @{
- Name = 'GuestFreeSpacePercent'
- Expression = {
- [Math]::Round(((($_.Guest.Disks.FreeSpaceGB) / ($_.Guest.Disks.CapacityGB)) * 100), 0)
- }
- }
- # $_.Guest.Disks.Path
- $GuestDiskPathProp = @{
- Name = 'GuestDiskPath'
- Expression = {
- $_.Guest.Disks[0].Path
- }
- }
- $GuestToolsVersionProp = @{
- Name = 'GuestToolsVersion'
- Expression = {
- $_.Guest.ToolsVersion
- }
- }
- $GuestToolsRunningStatusProp = @{
- Name = 'GuestToolsRunningStatus'
- Expression = {
- $_.ExtensionData.Guest.ToolsRunningStatus
- }
- }
- $GuestToolsStatusProp = @{
- Name = 'GuestToolsStatus'
- Expression = {
- $_.ExtensionData.Guest.ToolsStatus
- }
- }
- $GuestOSFullNameProp = @{
- Name = 'GuestOSFullName'
- Expression = {
- $_.Guest.OSFullName
- }
- }
- # Get VMs
- $collection = Get-VM
- $collection | ForEach-Object -Begin {
- Clear-Host
- # Set the $i counter variable to zero.
- $i = 0
- # Set the $out collection to empty
- $out = @()
- } -Process {
- if ($_.Guest.Disks.Count -eq 1 -and $_.PowerState -eq 'PoweredOn') {
- $out += $_ | Select-Object -Property Name,
- NumCpu,
- MemoryGB,
- $MemoryUsageAveragePercentProp,
- $GuestDiskCapacityGBProp,
- $GuestDiskFreeGBProp,
- $GuestFreeSpacePercentProp,
- $GuestDiskPathProp,
- $GuestToolsVersionProp,
- $GuestToolsStatusProp,
- $GuestToolsRunningStatusProp,
- $GuestOSFullNameProp
- }
- # Increment the $i counter variable which is used to create the progress bar.
- $i++
- # Use Write-Progress to output a progress bar.
- # The Activity and Status parameters create the first and second lines of the progress bar heading, respectively.
- Write-Progress -Activity "Getting VM details" -Status "Progress:" -PercentComplete ($i / $collection.count * 100)
- } -End {
- # Export $out collection to CSV file.
- $out | Export-Csv -Path "vm_details.csv" -NoClobber -NoTypeInformation
- Write-Host "Done."
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement