Automation

 View Only
  • 1.  List Vm of particular datastore

    Posted Nov 29, 2017 01:06 PM

    Hello

    I have a list of datastore.

    C:\> Get-Datastore

    Name                               FreeSpaceGB      CapacityGB

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

    Datastore_Local_gardon                 265,947         271,750

    DS_ASO_P-01                             92,584         499,750

    DS_BKO_P-01                             78,322         499,750

    DS_DBO_P-01                             93,090         499,750

    DS_ASO_P-02                            442,799         499,750

    DS_DBO_P-02                             39,886         499,750

    DS_DBO_NP-01                           123,201         499,750

    DS_ASO_NP-01                           253,971         499,750

    DS_DBO_NP-02                            78,090         499,750

    DS_BKO_NP-01                           248,320       1 099,750

    DS_DBO_NP-03                           166,074       1 749,750

    DS_DBO_NP-04                            94,927         499,750



    I would like to know the vm which are on only datastores having NP in their names

    $ds=get-datastore

    foreach($d in $ds)

    {

        write-host $d

        $a=select-string -pattern "NP" -InputObject $d

        write-host $a

        get-vm -datastore $a

    }

    Get-VM : Cannot bind parameter 'Datastore'. Cannot convert the "DS_DBO_NP-04" value of type "Microsoft.PowerShell.Commands.MatchInfo" to type

    "VMware.VimAutomation.ViCore.Types.V1.DatastoreManagement.StorageResource".

    can you help me?



  • 2.  RE: List Vm of particular datastore

    Posted Nov 29, 2017 01:48 PM

    In one line if you want all of them without printing which datastore they belong to:

    Get-VM -Datastore ( Get-Datastore -Name *NP* )

    Modifying your script, use the wildcard in the -Name attribute of Det-Datastore to pull multiple datastores matching the criteria:

    $ds = get-datastore -Name *NP*

    foreach($d in $ds)

    {

        write-host $d

        get-vm -datastore $d

    }



  • 3.  RE: List Vm of particular datastore
    Best Answer

    Posted Nov 29, 2017 02:11 PM

    Try like this

    Get-Datastore *NP* -PipelineVariable ds | Get-VM |

    select Name,@{N='Datastore';E={$ds.Name}}



  • 4.  RE: List Vm of particular datastore

    Posted Nov 29, 2017 02:20 PM

    And then you could sort it by datastore and name if you want:

    Get-Datastore -PipelineVariable ds | Get-VM |

    select Name,@{N='Datastore';E={$ds.Name}} | Sort-Object Datastore,Name