PowerCLI

 View Only
  • 1.  get basic stats for hosts and datastores

    Posted Jan 12, 2016 09:27 AM

    Hi all,

    I'm totally new to vcenter and power shell, so please be gentle on my stupid questions. I am desperate to compile a script to obtain the weekly stats for host memory, vcpu count and datastore space and export it to a csv file. So very basic numbers for each host and datastore. Below is what I've got so far based on LucD's generous blogs. It'd be great if any of you can help me realize what's wrong...:smileysilly:

    It took a lot of time for me to figure out what parameters I needed and I'm pretty sure that they are correct, but the syntax is killing me now. Basically I realize there's something wrong with my variables since nothing exported to the desired file, but I'm not sure how to define the variables and their attributes. And the error was "cannot overwrite variable host because it is read-only or constant", so as the other properties like "name", "mem", etc.

    Connect-VIServer -server xxx.com -user xxx -password xxxx

    $allhosts = @()

    $hosts = Get-VMHost

    $allds = @()

    $datastores = Get-DataStore

    foreach($vmHost in $hosts){

    $host = ""| Select "name","mem","vcpu"

    $host.name = $vmHost.Name

    $host.mem = Get-Stat -Entity ($vmHost) -start (Get-Date).AddDays(-1) -Finish (Get-Date) -MaxSamples 1 -Stat "mem.consumed.average" |  select -ExpandProperty Value

    $host.vcpu = Get-VM -Location ($vmHost) | Where-Object {$_.PowerState -eq "PoweredOn"} | Measure-Object -Property NumCpu -Sum |  select -ExpandProperty Sum

    $allhosts += $host

    }

    $allhosts | Export-csv -NoTypeInformation C:\host.csv

    foreach($dataStore in $datastores | Where-Object {$_.name -like "xxxx*"}){

    $dsName = $dataStore.Name

    $provspace = Get-Stat -Entity ($dataStore) -Start (Get-Date -Hour 9 -Minute 0 -Second 0) -Finish  (Get-Date -Hour 10 -Minute 0 -Second 0) -IntervalSecs 30000 -Instance '' -stat disk.provisioned.latest | select -ExpandProperty Value

    $freespace = $datastores | Select-Object Name, FreeSpaceGB

    }

    $allds | Select DataStorename, provspace, freespace | Export-csv -NoTypeInformation C:\datastore.csv

    Thank you in advance!!!

    -LX



  • 2.  RE: get basic stats for hosts and datastores
    Best Answer

    Posted Jan 12, 2016 10:19 AM

    Unfortunately you have picked a PowerShell reserved name for the name of the variable.

    The variable  $host is not allowed.

    Pick any other name, and the script should be ok



  • 3.  RE: get basic stats for hosts and datastores

    Posted Jan 12, 2016 01:41 PM

    Thank you so much LucD! I just changed the variable name and it worked!