Automation

 View Only
Expand all | Collapse all

PowerCLI Move-Harddisk from csv file

  • 1.  PowerCLI Move-Harddisk from csv file

    Posted Jul 16, 2015 03:38 PM

    Hi all,

    Been banging my head with this one its drving me mad hopefully someone can help please. I am using LucD excellent Orphaned files and folders - Spring cleaning - LucD notes script to identify orphaned VMDK files. I have ran the script and exported the results into a CSV file.

    What I need to do now is to run a script that will move every VMDK in the csv file to another datastore. I am struggling to get the powercli command correct and I am hoping someone will be able to help please.

    I have tried these:

    Move-HardDisk -Harddisk (Import-Csv C:\csvfilename.csv | %{$_.Name})


    Cannot bind parameter 'HardDisk'. Cannot convert the "filename_0001.vmdk" value of type "System.String" to type  "VMware.VimAutomation.ViCore.Types.V1.VirtualDevice.HardDisk"

    And

    Get-HardDisk -Name (Import-Csv C:\Users\DeanE\Documents\ListofDeletedDC2_A_T1_SRM_PL003VMDKs.csv | %{$_.Name})

    Get-HardDisk Please specify at least one of the following: Path, Datastore, VirtualMachine, Template or Snapshot. 

    And

    Get-HardDisk -Datastore DatastoreName -Name (Import-Csv C:\csvfilename.csv | %{$_.Name}) 

    Parameter set cannot be resolved using the specified named parameters.

    Any help would be greatly appreciated please.

    Thanks very much



  • 2.  RE: PowerCLI Move-Harddisk from csv file

    Posted Jul 16, 2015 06:33 PM

    Did you already try with the Path parameter instead of the Name parameter ?



  • 3.  RE: PowerCLI Move-Harddisk from csv file

    Posted Jul 17, 2015 08:51 AM

    No sorry, will give that a whirl now.



  • 4.  RE: PowerCLI Move-Harddisk from csv file

    Posted Jul 17, 2015 08:56 AM

    So the headers I get in the csv file are

    "Extents","Name","Thin","CapacityKB","Folder","Size"

    the extents field gives the following results:

    [Datastore Name] filename-00001-delta.vmdk

    Tried running this:

    Move-HardDisk -Harddisk (Import-Csv C:\vmdk.csv | %{$_.Extents})

    Cannot bind parameter 'HardDisk'. Cannot convert the value of type "System.String" to type   "VMware.VimAutomation.ViCore.Types.V1.VirtualDevice.HardDisk".



  • 5.  RE: PowerCLI Move-Harddisk from csv file

    Posted Jul 17, 2015 09:00 AM

    Does this work ?

    Import-Csv -DatastorePath 'C:\Users\DeanE\Documents\ListofDeletedDC2_A_T1_SRM_PL003VMDKs.csv' | %{

        Get-HardDisk -Path $_.Extents

    }



  • 6.  RE: PowerCLI Move-Harddisk from csv file

    Posted Jul 17, 2015 09:06 AM

    It gives me this error for each line on the csv

    Get-HardDisk : Cannot validate argument on parameter 'Path'. The argument is null. Supply a non-null argument and try the command again.

    Get-HardDisk The specified path filename.vmdk is not valid. Only 'VimDatastore' provider paths are valid for the Path parameter



  • 7.  RE: PowerCLI Move-Harddisk from csv file

    Posted Jul 17, 2015 09:08 AM

    That should have been Extents, not Extent



  • 8.  RE: PowerCLI Move-Harddisk from csv file

    Posted Jul 17, 2015 09:15 AM

    Thanks, yeah I noticed that still get this error.

    Get-HardDisk : 17/07/2015 10:12:40 Get-HardDisk The specified path '[datastore] filename-000010-delta.vmdk' is not valid. Only 'VimDatastore' provider

    paths are valid for the Path parameter. 

    At C:\Users\DeanE\Documents\Get-VMDKsfromcsv.ps1:6 char:5

    + Get-HardDisk -Path $_.Extents

    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

      + CategoryInfo : InvalidArgument: (VMware.VimAutom...DatastoreItem[]:DatastoreItem[]) [Get-HardDisk], VimException

      + FullyQualifiedErrorId : Core_GetVirtualHardDisk_TryValidateProviderPath_InvalidPathProvider,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.GetHardDisk



  • 9.  RE: PowerCLI Move-Harddisk from csv file

    Posted Jul 17, 2015 09:17 AM

    And with DatastorePath instead of Path ?



  • 10.  RE: PowerCLI Move-Harddisk from csv file

    Posted Jul 17, 2015 09:19 AM

    Different error this time:

    Get-HardDisk Value cannot be found for the mandatory parameter Datastore 



  • 11.  RE: PowerCLI Move-Harddisk from csv file
    Best Answer

    Posted Jul 17, 2015 09:42 AM

    Ok, had another look.

    The following seems to work for me, at least for the entries that have `flat` in the VMDK filename

    Import-Csv c:\orphan.csv -UseCulture | %{

        $ds = $_.Extents.Split(']')[0].Trim('[')

        $dsPath = $_.Extents.Replace('-flat','')

        Get-HardDisk -Datastore $ds -DatastorePath $dsPath

    }



    If you have entries in the CSV for which it doesn´t work, let me know what is in the Extents field.



  • 12.  RE: PowerCLI Move-Harddisk from csv file

    Posted Jul 17, 2015 09:56 AM

    You my friend are a legend!!! This is what I used as all  the files listed are "-delta". Thank you very much for taking the time to help, its very much appreciated.

    Import-Csv C:\orphan.csv -UseCulture | %{

     

       $ds = $_.Extents.Split(']')[0].Trim('[')

     

       $dsPath = $_.Extents.Replace('-delta','')

     

       Get-HardDisk -Datastore $ds -DatastorePath $dsPath

    } | Move-HardDisk -Datastore datastorename



  • 13.  RE: PowerCLI Move-Harddisk from csv file

    Posted Jul 24, 2015 03:26 PM

    I've been looking at a similar thing, so going to give this a go.  I generate the csv from the RVTools Healthcheck page and we have a LOT of orphan files.  I'd like to output the filename, file size, start and finish times to a text file as well.  I'm just starting to get my head around PowerCLi and enjoying the power it brings!