Increment a Collection

Welcome Forums General PowerShell Q&A Increment a Collection

This topic contains 3 replies, has 3 voices, and was last updated by

1 year, 9 months ago.

  • Author
  • #72880

    Points: 14
    Rank: Member

    This thing has 4 basic parts;
    1) Get list of servers from an AD OU
    2) Figure out which ones have a hypervisor installed
    3) If it does have a HV installed, then figure out how many VMs are running on it
    4) (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"
        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"
  • #72881

    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    If you're certain that $HVCount contains a number, then I don't see why it wouldn't work as-is. I suspect $HVCount doesn't contain what you think it does?

    In your ForEach loop, just use this by itself:

    $hvtotalcount += get-vm -comp $hvserver | measure | select -expand count


  • #72884

    Points: 14
    Rank: Member

    Thanks Don! That worked like a charm. I'll have to remember that for next time. Also, almost done with your book which I gotta say is awesome. It definitely has stepped up my administration game. Thanks again.

  • #72994

    Points: 31
    Rank: Member

    I think the reason your code doesn't work above is because you are using =+ It is not the correct increment feature, use += instead. Very subtle, would expect this in one of those code challenges soon.

The topic ‘Increment a Collection’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort