Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. # Define custom properties
  2.  
  3. # Percentage of configured virtual machine “physical” memory
  4. # active ÷ virtual machine configured size
  5. # 10 samples range is 7 days
  6. $MemoryUsageAveragePercentProp = @{
  7. Name = 'MemoryUsageAveragePercent'
  8. Expression = {
  9. $value = $_ | Get-Stat -Stat "mem.usage.average" -Start (Get-Date).AddDays(-7) -Finish (Get-Date) -MaxSamples 10 | Measure-Object -Property Value -Average
  10. [Math]::Round($value.Average, 0)
  11. }
  12. }
  13.  
  14. $GuestDiskCapacityGBProp = @{
  15. Name = 'GuestDiskCapacityGB'
  16. Expression = {
  17. [Math]::Round(($_.Guest.Disks.CapacityGB), 0)
  18. }
  19. }
  20.  
  21. $GuestDiskFreeGBProp = @{
  22. Name = 'GuestDiskFreeGB'
  23. Expression = {
  24. [Math]::Round(($_.Guest.Disks.FreeSpaceGB), 0)
  25. }
  26. }
  27.  
  28. $GuestFreeSpacePercentProp = @{
  29. Name = 'GuestFreeSpacePercent'
  30. Expression = {
  31. [Math]::Round(((($_.Guest.Disks.FreeSpaceGB) / ($_.Guest.Disks.CapacityGB)) * 100), 0)
  32. }
  33. }
  34.  
  35. # $_.Guest.Disks.Path
  36. $GuestDiskPathProp = @{
  37. Name = 'GuestDiskPath'
  38. Expression = {
  39. $_.Guest.Disks[0].Path
  40. }
  41. }
  42.  
  43. $GuestToolsVersionProp = @{
  44. Name = 'GuestToolsVersion'
  45. Expression = {
  46. $_.Guest.ToolsVersion
  47. }
  48. }
  49.  
  50. $GuestToolsRunningStatusProp = @{
  51. Name = 'GuestToolsRunningStatus'
  52. Expression = {
  53. $_.ExtensionData.Guest.ToolsRunningStatus
  54. }
  55. }
  56.  
  57. $GuestToolsStatusProp = @{
  58. Name = 'GuestToolsStatus'
  59. Expression = {
  60. $_.ExtensionData.Guest.ToolsStatus
  61. }
  62. }
  63.  
  64. $GuestOSFullNameProp = @{
  65. Name = 'GuestOSFullName'
  66. Expression = {
  67. $_.Guest.OSFullName
  68. }
  69. }
  70.  
  71. # Get VMs
  72. $collection = Get-VM
  73.  
  74. $collection | ForEach-Object -Begin {
  75.  
  76. Clear-Host
  77. # Set the $i counter variable to zero.
  78. $i = 0
  79.  
  80. # Set the $out collection to empty
  81. $out = @()
  82.  
  83. } -Process {
  84.  
  85. if ($_.Guest.Disks.Count -eq 1 -and $_.PowerState -eq 'PoweredOn') {
  86.  
  87. $out += $_ | Select-Object -Property Name,
  88. NumCpu,
  89. MemoryGB,
  90. $MemoryUsageAveragePercentProp,
  91. $GuestDiskCapacityGBProp,
  92. $GuestDiskFreeGBProp,
  93. $GuestFreeSpacePercentProp,
  94. $GuestDiskPathProp,
  95. $GuestToolsVersionProp,
  96. $GuestToolsStatusProp,
  97. $GuestToolsRunningStatusProp,
  98. $GuestOSFullNameProp
  99. }
  100.  
  101. # Increment the $i counter variable which is used to create the progress bar.
  102. $i++
  103. # Use Write-Progress to output a progress bar.
  104. # The Activity and Status parameters create the first and second lines of the progress bar heading, respectively.
  105. Write-Progress -Activity "Getting VM details" -Status "Progress:" -PercentComplete ($i / $collection.count * 100)
  106. } -End {
  107. # Export $out collection to CSV file.
  108. $out | Export-Csv -Path "vm_details.csv" -NoClobber -NoTypeInformation
  109. Write-Host "Done."
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement