Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.67 KB | None | 0 0
  1. function Get-VMInventory {
  2. <#
  3. .SYNOPSIS
  4. Retrieves detailed information about VMs from Hyper-V host or cluster
  5.  
  6. .DESCRIPTION
  7. Retrieves information about a VM, it's disk, path, current resources and network properties.
  8. Will query Hyper-V host or all nodes in a cluster if [$Cluster] is provided. Accepts [$Credential] parameter.
  9.  
  10. .PARAMETER ComputerName
  11. Hyper-V host or cluster name
  12.  
  13. .PARAMETER Cluster
  14. Set to true if cluster is to be queried
  15.  
  16. .PARAMETER Credential
  17. Optional Credential parameter
  18.  
  19. .EXAMPLE
  20. Get-VMInventory -ComputerName 'HVHost1' | Format-Table -AutoSize
  21.  
  22. Name ComputerName DynamicMemoryEnabled MemoryMinimum MemoryMaximum MemoryAssigned MemoryStatus ProcessorCount DisksCount DiskCurrentSize
  23. ---- ------------ -------------------- ------------- ------------- -------------- ------------ -------------- ---------- --------------
  24. Mgmt HVHost1 False 0,5 1024 0 4 1 23,99
  25. Router-VyOS HVHost1 False 0,25 1024 0 1 1 0,5
  26. S1_DC1 HVHost1 True 1 2 0 2 1 25,06
  27. S1_DC2 HVHost1 True 0,5 2 0 2 1 29,1
  28. WEC HVHost1 False 0,5 1024 0 6 2 49,08
  29.  
  30. .EXAMPLE
  31.  
  32. Get-VMInventory -Computer 'HVCluster1' -Cluster -Credential (Get-Credential) | Format-Table -AutoSize
  33.  
  34. Name ComputerName DynamicMemoryEnabled MemoryMinimum MemoryMaximum MemoryAssigned MemoryStatus ProcessorCount DisksCount DiskCurrentSize
  35. ---- ------------ -------------------- ------------- ------------- -------------- ------------ -------------- ---------- ---------------
  36. HVNode3-VyOS HVNode3 False 0,5 1024 1 1 1 0,91
  37. OT-PLCON0 HVNode1 False 0,5 1024 2 1 1 29,91
  38. OT-PLDHCP0 HVNode2 False 0,5 1024 2 1 1 19,94
  39. OT-PLPDC0 HVNode4 False 0,5 1024 2 1 1 21,04
  40. OT-PLSDC0 HVNode5 False 0,5 1024 2 1 1 23,88
  41.  
  42. .NOTES
  43. #>
  44.  
  45. [CmdletBinding()]
  46. Param(
  47. [Parameter(Mandatory = $true,
  48. ValueFromPipeline, ValueFromPipelineByPropertyName)]
  49. [String[]]
  50. $ComputerName,
  51.  
  52. [Parameter(Mandatory = $false,
  53. ValueFromPipelineByPropertyName)]
  54. [switch]
  55. $Cluster,
  56.  
  57. [Parameter(Mandatory = $false)]
  58. [System.Management.Automation.Credential()]
  59. $Credential = [System.Management.Automation.PSCredential]::Empty
  60.  
  61. )
  62.  
  63. begin {
  64.  
  65. $connProperties = @{
  66. ComputerName = $ComputerName
  67. }
  68. if ($Credential) {
  69. $connProperties.Credential = $Credential
  70. }
  71. if ($Cluster) {
  72. $Nodes = Invoke-Command @connProperties -ScriptBlock {
  73. Get-ClusterNode | where-object { $PSItem.State -eq 'Up' } |
  74. select-object -ExpandProperty Name
  75. }
  76. $connProperties.ComputerName = $Nodes
  77. }
  78. }
  79.  
  80. process {
  81. Invoke-Command @connProperties -ScriptBlock {
  82. $SelectObjectFilter = @(
  83. @{name = 'IPAddress'; e = { $PSItem.IPAddresses -notmatch ':' } },
  84. @{name = 'SwitchName'; e = { $PSItem.SwitchName } },
  85. @{name = 'MacAddress'; e = { $Psitem.MacAddress } }
  86. )
  87. Get-VM | ForEach-Object {
  88. Write-Verbose "ProcesiFng VM {$($PSItem.VMName)}"
  89. Write-Verbose "Getting VM {$($PSItem.VMName)} disk information"
  90. $disks = Get-VHD -VMId $PSItem.VMId -ComputerName $PSItem.ComputerName
  91. $diskCount = $disks | Measure-Object |
  92. Select-Object -ExpandProperty Count
  93. $diskCurrentSize = $disks | Measure-Object -Sum -Property FileSize |
  94. Select-Object -ExpandProperty Sum
  95. $diskMaximumSize = $disks | Measure-Object -Sum -Property Size |
  96. Select-Object -ExpandProperty Sum
  97. Write-Verbose "Getting VM {$($PSItem.VMName)} network information"
  98. $NetworkAdapters = @( Get-VMNetworkAdapter $PSItem |
  99. Select-Object $SelectObjectFilter)
  100. [pscustomobject]@{
  101. Name = $PSItem.Name
  102. ComputerName = $PSItem.ComputerName
  103. DynamicMemoryEnabled = $PSItem.DynamicMemoryEnabled
  104. MemoryMinimum = [System.Math]::Round($PSItem.MemoryMinimum / 1GB, 2)
  105. MemoryMaximum = [System.Math]::Round($PSItem.MemoryMaximum / 1GB, 2)
  106. MemoryAssigned = [System.Math]::Round($PSItem.MemoryAssigned / 1GB, 2)
  107. MemoryStatus = $PSItem.MemoryStatus
  108. ProcessorCount = $PSItem.ProcessorCount
  109. DisksCount = $diskCount
  110. DiskCurrentSize = [System.Math]::Round($diskCurrentSize / 1GB, 2)
  111. DiskMaximumSize = [System.Math]::Round($diskMaximumSize / 1GB, 2)
  112. State = $PSitem.State
  113. Uptime = $PSItem.Uptime
  114. Version = $PSItem.Version
  115. CreationTime = $PSItem.CreationTime
  116. Path = $PSItem.Path
  117. AutomaticStartAction = $PSItem.AutomaticStartAction
  118. AutomaticStartDelay = $PSItem.AutomaticStartDelay
  119. AutomaticStopAction = $PSItem.AutomaticStopAction
  120. IntegrationServicesState = $PSItem.IntegrationServicesState
  121. IntegrationServicesVersion = $PSitem.IntegrationServicesVersion
  122. NetworkAdapters = $NetworkAdapters
  123. }
  124. }
  125. }
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement