Search code examples
powershellvmwarepowercli

How do i get sub properties


With VMWare PowerCLi, I am trying to access the properties from a variable but not sure how to get it, My code looks like this so far :

    $datastore = Get-Datastore | where {$_.name -like "*geko*"} | select 
    name,remotehost, remotepath | Sort-Object name | ft -AutoSize
    $datastore

so the result i get is:

Name            RemoteHost        RemotePath      
----            ----------        ----------      
Serv_Geko       {192.168.134.137} /Serv_Geko

Question is how do I get the RemotePath in a variable or access it from the above $datastore variable.

I thought I would be able to get it from $datastore.RemoteHost but that doesnt seem to work.

I essentially just need to get the IP into a variable so that I can use it lower down in the script.

Any help most appreciated.


Solution

  • Remove ft -AutoSize from your code and then you can access $datastore.RemoteHost.

    PS> $datastore = Get-Datastore | where {$_.name -like "*geko*"} | select 
        name, remotehost, remotepath | Sort-Object name
    PS> $datastore.RemoteHost
    

    OR

    Use -ExpandProperty parameter of Select-Object.

    $IPAddress = Get-Datastore | where {$_.name -like "*geko*"} | select-object -ExpandProperty remotehost
    

    Now your $IPAddress contains the IP address.