Created
September 21, 2018 05:22
-
-
Save gjonespf/32118ba7992101fe973a1a93fe109fbc to your computer and use it in GitHub Desktop.
Query HyperV Hosts for disk space
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Get-HyperVServersInDomain { | |
| [cmdletbinding()] | |
| param( | |
| ) | |
| try { | |
| Import-Module ActiveDirectory -ErrorAction Stop | |
| } | |
| catch { | |
| Write-Warning "Failed to import Active Directory module. Exiting" | |
| return | |
| } | |
| try { | |
| $Hypervs = Get-ADObject -Filter 'ObjectClass -eq "serviceConnectionPoint" -and Name -eq "Microsoft Hyper-V"' -ErrorAction Stop | |
| } | |
| catch { | |
| Write-Error "Failed to query active directory. More details : $_" | |
| } | |
| foreach ($Hyperv in $Hypervs) { | |
| $temp = $Hyperv.DistinguishedName.split(",") | |
| $HypervDN = $temp[1..$temp.Count] -join "," | |
| $Comp = Get-ADComputer -Id $HypervDN -Prop * | |
| $OutputObj = New-Object PSObject -Prop ( | |
| @{ | |
| HyperVName = $Comp.Name | |
| OSVersion = $($comp.operatingSystem) | |
| }) | |
| $OutputObj | |
| } | |
| } | |
| function Get-FriendlySize { | |
| param($Bytes) | |
| $sizes='Bytes,KB,MB,GB,TB,PB,EB,ZB' -split ',' | |
| for($i=0; ($Bytes -ge 1kb) -and | |
| ($i -lt $sizes.Count); $i++) {$Bytes/=1kb} | |
| $N=2; if($i -eq 0) {$N=0} | |
| "{0:N$($N)} {1}" -f $Bytes, $sizes[$i] | |
| } | |
| $vmhostdetails = Get-HyperVServersInDomain | |
| $vmhosts = $vmhostdetails | Select -ExpandProperty HyperVName | |
| $vmhostdrives = foreach($vmhost in $vmhosts ) { | |
| # $disk = Get-WmiObject Win32_LogicalDisk -ComputerName remotecomputer -Filter "DeviceID='C:'" | Select-Object Size,FreeSpace | |
| Invoke-Command -ComputerName $vmhost -ArgumentList $vmhost { | |
| # Arguments | |
| $vmhost = $args[0] | |
| # Query | |
| Get-PSDrive | ?{ $_.Free -and $_.DisplayRoot -notmatch "\\" } | | |
| Select @{ name="hostname"; expression={ $vmhost }}, | |
| @{ name="DriveName"; expression= { $_.Name }}, | |
| @{ name="UsedSpace"; expression= { $_.Used }}, | |
| @{ name="FreeSpace"; expression= { $_.Free }} | |
| } | |
| } | |
| $vmhostdrives | Select *, | |
| @{ name="UsedFriendly"; expression= { Get-FriendlySize -Bytes $_.UsedSpace }}, | |
| @{ name="FreeFriendly"; expression= { Get-FriendlySize -Bytes $_.FreeSpace }} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment