Automation

 View Only
  • 1.  Adding the total datastore size and remaining space

    Posted Nov 22, 2011 08:43 PM

    Hi,

    What would be the easist way to add the total datastore size and remaining datastore size to the following.
    I do emphasise, esasiest, since I'm as green as can be to powercli, powershell for that matter. I'm trying to learn in babysteps :smileyhappy:

    Get-VM $VM | %{ $_.Name; ($_ | get-datastore | select Name).Name}

    Right now I receive thereport as follows, I receive from the above---

    datastorename (add size info here on DS?)

    vmname

    datastorename (add size info here on DS?)

    vmname

    ...

    ...

    ...

    Thanks!!



  • 2.  RE: Adding the total datastore size and remaining space

    Posted Nov 22, 2011 09:59 PM

    Something like this would do the trick

    $VM="MyVM"
    Get-VM $VM | %{     $VmObj = $_
       
    $VmObj.DatastoreIdList | %{Get-View -Id $_} |
        Select
    @{N="VM";E={$VmObj.Name}},Name,
            @{N
    ="Capacity";E={$_.Summary.Capacity}},         @{N="FreeSpace";E={$_.Summary.FreeSpace}} }

    In the object returned by Get-VM there is a list of Managed Object references (MoRefs) to all the datastores used by the VM.

    By feeding that MoRef into Get-View you get the Datastore object, which contains the required information.

    Another way, which uses only PowerCLI objects, is like this

    $VM="MyVM" 
    $VmObj
    = Get-VM $VM
    Get-Datastore -VM $VmObj | Select @{N="VM";E={$VmObj.Name}},Name, @{N="CapacityMB";E={$_.CapacityMB}},         @{N="FreeSpaceMB";E={$_.FreeSpaceMB}}

    This is perhaps easier to read, we get all the datastores that a VM uses and select the required properties.



  • 3.  RE: Adding the total datastore size and remaining space

    Posted Nov 22, 2011 11:04 PM

    As always, perfect...

    For my learning experience:

    can you please tell me what the following means?

    $_


    N="VM";E={$VmObj.Name}

    I assume, N=name, E=? (referncing what?)


    It will help me understand it better.

    This is apples and oranges to shell scripting (syntax speaking)...im struggling a bit here on coorilating from what I am use to.

    I'm having a hard time find a power* for dummies reference/book that has a collection of interpretations.  And I use DUMMIES on purpose....

    Thanks again..



  • 4.  RE: Adding the total datastore size and remaining space
    Best Answer

    Posted Nov 22, 2011 11:17 PM

    The $_ represents the object that was passed throughthe pipeline (the '|') from the previous cmdlet.

    For example

    Get-VM | %{$_.Name}

    The Get-VM cmdlet will get all the VMs on the vSphere servers to which you are connected.

    The pipeline ('|') says to pass these VM objects, one by one to the next code.

    In this that is a Foreach-Object (alias is '%') that will send the name of the VM (available in the $_ variable) to the default output.

    On a Select-Object cmdlet you can select properties from the object that was passed through the pipeline or you can use what is called a 'calculated property'. Such a calculated property consists of a hash-pair, Name (N) and Expression.

    Name is obviously the name you want to give to this calculated property.

    Expression is the code block that needs to be executed.

    For example

    Get-Datastore | Select Name,@{N="CapacityGB";E={$_.CapacityMB/1KB}}

    All datastore objects are passed to the Select-Object cmdlet.

    The Select cmdlet will display the Name of the datastore and it will display a property called CapcityGB, which is displaying the capacity of the datastore in GB.

    Note how the property CapacityMB that is present in the datastore object is converted to GB by the code in the Expression block.

    The Expression part doesn't always need to do something with a property from the object that was passed.

    For example

    Get-VM | Select Name, @{N="Current Time";E={Get-Date}}

    In this case the Select-Object cmdlet will display the Name property of the VM object that was passed to it.

    And it will display the current time under the propertyname "Current Time".

    This is an example of a calculated property that has nothing to with the object passed through the pipeline.

    I hope this clarifies the code a bit.



  • 5.  RE: Adding the total datastore size and remaining space

    Posted Nov 22, 2011 11:23 PM

    Thank you!!!



  • 6.  RE: Adding the total datastore size and remaining space

    Posted Nov 22, 2011 11:19 PM

    In my My PS Library post you will find some links to PowerShell learning resources, some of these are even free.