Automation

 View Only
  • 1.  Unable to get the Available values when usage is zero

    Posted 30 days ago

    Hi,

    I am using the below script to get the usage, this works fine, when I have VM with CPU, Memory and HDD used, but when there are no VMs, CPU, Memory or HDD used, I am unable to get the Available values correctly. Please help!!

    $vmInfo = Foreach ($vc in $global:DefaultVIServers)
    {
    $report = @()
    $folder = "Admin"
    $allocated = "" | Select "Cluster","Folder Name","Resource_Info","CPU","Memory(GB)","Capacity(GB)","Total VM's","PoweredOn VM's","PoweredOff VM's"
    $allocated.'Resource_Info' = 'Allocated'
    $allocated.CPU = 20
    $allocated.'Memory(GB)' = 40
    $allocated.'Capacity(GB)' = 100
    $report += $allocated
    $report += Get-Folder $folder | Get-VM | Group-Object -Property {$_.Folder.Id} | Select @{N="Cluster";E={Get-Cluster -VM $_.Group}},
      @{N="Folder Name";E={$folder = $_.Group[0].Folder; $path = $folder.Name; $path}},
      @{N='Resource_Info';E={'Used'}},
      @{N="CPU";E={$_.Group | Measure-Object -Property NumCpu -Sum | Select -ExpandProperty Sum}},
      @{N="Memory(GB)";E={[math]::Round(($_.Group | Measure-Object -Property MemoryGB -Sum).Sum,0)}},
      @{N="Capacity(GB)";E={[math]::Round(($_.Group | Get-HardDisk | Measure-Object -Property CapacityGB -Sum).Sum,0)}},
      #@{N="Capacity(GB)";E={"{0:f1}" -f ($_.Group | Get-HardDisk | Measure-Object -Property CapacityGB -Sum | Select -ExpandProperty Sum)}},
      @{N="Total VM's";E={$_.Group.Count}},
      @{N="PoweredOn VM's";E={($_.Group | Where-Object {$_.PowerState -eq "PoweredOn"}).Count}},
      @{N="PoweredOff VM's";E={($_.Group | Where-Object {$_.PowerState -eq "PoweredOff"}).Count}}
      $available = "" | Select "Cluster","Folder Name","Resource_Info","CPU","Memory(GB)","Capacity(GB)","Total VM's","PoweredOn VM's","PoweredOff VM's"
      $available.'Resource_Info' = 'Available'
      $available.CPU = $report[0].CPU - ($report[1..($report.Count -1)].CPU | Measure-Object -Sum).Sum
      $available.'Memory(GB)' = $report[0].'Memory(GB)' - ($report[1..($report.Count -1)].'Memory(GB)' | Measure-Object -Sum).Sum
      $available.'Capacity(GB)' = $report[0].'Capacity(GB)' - ($report[1..($report.Count -1)].'Capacity(GB)' | Measure-Object -Sum).Sum
      $report += $available
      $report | Export-Excel -Path $reportlocation

    $vmInfo

    Currently I am getting as below when there are no resources used

    But I need as below, when there are no resources used



  • 2.  RE: Unable to get the Available values when usage is zero
    Best Answer

    Posted 30 days ago

    Try something like this

    $folderName = 'Admin'
    $vmInfo = @()
    
    $obj = New-Object -TypeName PSObject -Property ([ordered]@{
        'Cluster' = ''
        'Folder Name' = ''
        'Resource_Info' = ''
        'CPU' = 0
        'Memory(GB)' = 0
        'Capacity(GB)' = 0
        'Total VM''s' = 0
        'PoweredOn VM''s' = 0
        'PoweredOff VM''s' = 0
    })
    
    Foreach ($vc in $global:DefaultVIServers) {
        $report = @()
    
        $allocated = $obj.PSObject.Copy()
        $allocated.'Resource_Info' = 'Allocated'
        $allocated.'CPU' = 20
        $allocated.'Memory(GB)' = 40
        $allocated.'Capacity(GB)' = 100
        $report += $allocated
    
        Get-Folder -Name $folderName -PipelineVariable folder |
        ForEach-Object -Process {
            $used = $obj.PSObject.Copy()
            $used.Resource_Info = 'Used'
            $used.'Folder Name' = $folder.Name
    
            $vms = Get-VM -Location $folder
            if($vms){
                $used.Cluster = (Get-Cluster -VM $vms[0]).Name
                $used.CPU = $vms | Measure-Object -Property NumCpu -Sum | Select-Object -ExpandProperty Sum
                $used.'Memory(GB)' = [math]::Round(($vms | Measure-Object -Property MemoryGB -Sum).Sum, 0)
                $used.'Capacity(GB)' = [math]::Round(($vms | Get-HardDisk | Measure-Object -Property CapacityGB -Sum).Sum,0)
                $used.'Total VM''s' = $vms.Count
                $used.'PoweredOff VM''s' = ($vms | Where-Object { $_.PowerState -eq "PoweredOn" }).Count
                $used.'PoweredOff VM''s' = ($vms | Where-Object { $_.PowerState -eq "PoweredOff" }).Count
            }
            else{
                $used.Cluster = 'na'
            }
            $report += $used
        }
    
        $available = $obj.PSObject.Copy()
        $available.Resource_Info = 'Available'
        $available.CPU = $report[0].CPU - ($report[1..($report.Count - 1)].CPU | Measure-Object -Sum).Sum
        $available.'Memory(GB)' = $report[0].'Memory(GB)' - ($report[1..($report.Count - 1)].'Memory(GB)' | Measure-Object -Sum).Sum
        $available.'Capacity(GB)' = $report[0].'Capacity(GB)' - ($report[1..($report.Count - 1)].'Capacity(GB)' | Measure-Object -Sum).Sum
        $available = "" | Select-Object "Cluster", "Folder Name", "Resource_Info", "CPU", "Memory(GB)", "Capacity(GB)", "Total VM's", "PoweredOn VM's", "PoweredOff VM's"
        $available.'Resource_Info' = 'Available'
        $available.CPU = $report[0].CPU - ($report[1..($report.Count - 1)].CPU | Measure-Object -Sum).Sum
        $available.'Memory(GB)' = $report[0].'Memory(GB)' - ($report[1..($report.Count - 1)].'Memory(GB)' | Measure-Object -Sum).Sum
        $available.'Capacity(GB)' = $report[0].'Capacity(GB)' - ($report[1..($report.Count - 1)].'Capacity(GB)' | Measure-Object -Sum).Sum
        $report += $available
    
        $vmInfo += $report
        $report | Export-Excel -Path $reportlocation
    }
    $vmInfo
    


    ------------------------------


    Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference


    ------------------------------