This thing has 4 basic parts;
- Get list of servers from an AD OU
- Figure out which ones have a hypervisor installed
- If it does have a HV installed, then figure out how many VMs are running on it
- (The part I’m having trouble with) Calculate the grand total of the number of VMs
I’m still kind of fresh to this stuff. I’m not super familiar with objects/things like that. But basically every time my script sees that a server has X number of VMs I want to add that X value to a $collection which is totaled (sum calculated) at the end. This way I can see that my file server OU has 20 VMs or my DR OU has 35 VMs etc etc. I’m hoping this will expedite my FY18 Microsoft licensing estimation so I can get our SA re-quoted.
#4 is my problem. I cant seem to figure out a way to get all of the $.count collected and summed. Any help would be greatly appreciated!
#$HVE -- HyperVisor Enabled [array]$Servers = Get-ADComputer -SearchBase "OU=File Servers,DC=contoso,DC=com" -Filter * -Credential contoso\admin | select -ExpandProperty name $GoodCollection = New-Object System.Collections.Generic.List[System.Object] $BadCollection = New-Object System.Collections.Generic.List[System.Object] $HVTotalCount = New-Object System.Collections.ArrayList ##OK I DON"T KNOW WHAT IM DOING HERE## $HVTotalCount = 0 $i = foreach ($server in $Servers) { Write-Host "Checking if HyperV is enabled on: $server" $HVE = gcim Win32_ComputerSystem -ComputerName $server -ErrorAction SilentlyContinue if ($HVE.HypervisorPresent -eq $True -and $HVE.Model -notlike "Virtual Machine") { write-host "$server has HyperV Enabled...Proceeding" $GoodCollection.Add($server) } else{ $BadCollection.Add($server) } } Write-Host "NOW CALCULATING # OF VMS FOR EACH HYPERVISOR" -ForegroundColor Red -BackgroundColor black $i2 = foreach ($hvServer in $GoodCollection){ $hvCounter = get-vm -ComputerName $hvServer $HVcount = $hvCounter.count $HVTotalCount =+ $HVcount ##I NEED TO GET ALL OF THE "$hvCounter.count" TOTALED## Write-host "$hvServer HAS $HVcount VMs" } Write-Host "TOTAL DETECTED VMS: $HVTotalCount" ##RIGHT NOW THIS JUST COMES BACK AS 0##