PowerCLI

 View Only
  • 1.  Finding 'datastore' folder info using the 'New-PSDrive' cmdlet

    Posted Sep 04, 2012 08:16 AM

    Hey all

    Just needed some assistance with the following:

    I'm trying to peruse my 'datastores' using the 'New-PSDrive' cmdlet to create a PS drive with the following cmd:

    $datastore = Get-datastore datastore1


    New-PSDrive -Name DS -PSProvider VimDatastore -Root "\" -Datastore (get-datastore $datastore)

    This works a treat but the problem is I can search ONE datastore at a time. I was wondering if there was a way could search more than one???? I've tried adding in more datastores by means of the following but it fails:

    $datastores = Get-datastore (get-content c:\datastores.txt)

    Any help would be greatly appreciated ?


    Munster



  • 2.  RE: Finding 'datastore' folder info using the 'New-PSDrive' cmdlet

    Posted Sep 04, 2012 08:20 AM

    Is it failing because you are trying to add it as the same name ? ' -name DS' ?



  • 3.  RE: Finding 'datastore' folder info using the 'New-PSDrive' cmdlet

    Posted Sep 04, 2012 08:25 AM

    Anyway... for exampel i wanted to make new-psdrive for datastores that start from ds*

    so

    foreach ($ds in Get-Datastore ds*) {New-PSDrive -Name $($ds.name) -PSProvider VimDatastore -Root "\" -Datastore $ds}

    And all ps-drives were created.

    and with the get-content part:

    $datastores = Get-datastore (get-content c:\datastores.txt)

    foreach ($ds in $datastores) {New-PSDrive -Name $($ds.name) -PSProvider VimDatastore -Root "\" -Datastore $ds}

    Greg



  • 4.  RE: Finding 'datastore' folder info using the 'New-PSDrive' cmdlet

    Posted Sep 04, 2012 09:42 AM

    If you want to recurse a search through all your datastores, you can do something like this

    $dsNames = Get-Content -Path "c:\datastores.txt" 
    $ds
    = Get-Datastore -Name $dsNames
    $psDS
    = @() 0..($dsNames.Count-1) | %{   $psDS += New-PSDrive -Name "DS$_" -PSProvider VimDatastore -Root "\" -Datastore $ds[$_] } $psDS | %{   Set-Location -Path "$($_.Name):"
     
    Get-ChildItem -Recurse | where {$_.Name -like "*.vmx"} } Remove-PSDrive $psDS -Confirm:$false

    In this example the search will find all .VMX files on the datastores.

    Unfortunately the VimDatastore provider doesn't support the Filter parameter, so you have to do the filtering with a Where-clause after the retrieval of the all the objects on the drives.



  • 5.  RE: Finding 'datastore' folder info using the 'New-PSDrive' cmdlet

    Posted Sep 04, 2012 11:37 AM

    Nearly there LucD ......

    What I really would like to do is find a VM folder amongst all of our datastores. How could I amend the above script to output only the VM folder name and its associated datastore.

    I removed the " where {$_.Name -like "*.vmx"} " and replaced it with " where {$_.Name -eq "servername1"}. This finds it ok but i would like to add in the datastore it found it in .....

    Thanks again

    munster



  • 6.  RE: Finding 'datastore' folder info using the 'New-PSDrive' cmdlet

    Posted Sep 04, 2012 02:59 PM

    Try something like this

    $dsNames = Get-Content -Path "c:\datastores.txt"
    $ds = Get-Datastore -Name $dsNames
    $psDS
    = @() 0..($dsNames.Count-1) | %{   $psDS += New-PSDrive -Name "DS$_" -PSProvider VimDatastore -Root "\" -Datastore $ds[$_] } $psDS | %{   Set-Location -Path "$($_.Name):" 
      Get-ChildItem
    -Recurse | where {$_.Name -eq "servername"} |
      select Name,Datastore
    } Remove-PSDrive $psDS -Confirm:$false