Automation

 View Only
  • 1.  Foreach loop for Copy-HardDisk cmdlet

    Posted Jan 14, 2015 07:05 PM

    Folks,

    I am trying to create a simple foreach loop for copying all VMDKs of a VM to its datastores including a "-bkp" name on it. The output of (Get-HardDisk -VM $vm | Select -ExpandProperty Filename) presents a VM with two VMDKs. The first VMDK is inside of the local datastore, and the second one is inside of the vm_os datastore. I have been trying to create a simple foreach loop to copy [local] OL55/OL55.vmdk to [local} OL55/OL55-bkp.vmdk, and the same for all subsequents VMDKs attached to a VM, but so far without any success.

    It seems to be simple, but I got stuck! and need help from you guys.

    Below is what I am trying to accomplish!

    Get-HardDisk -VM $vm | Select -ExpandProperty Filename

    [local] OL55/OL55.vmdk

    [vm_os] OL55/OL55.vmdk

    $vm = "OL55"

    $vmdk = Get-HardDisk -VM $vm | Select -ExpandProperty Filename

    ForEach ($hardisk in $vmdk)

    { Copy-HardDisk "[$vmdk]" -DestinationPath "[$vmdk-bkp]" }

    Here is the output "error":

    Copy-HardDisk : Cannot bind parameter 'HardDisk'. Cannot convert the "[[local] OL55/OL55.vmdk [vm_os] OL55/OL55.vmdk]" value of type "System.String" to type "VMware.VimAutomation.ViCore.Types.V1.VirtualDevice.HardDisk".

    At C:\Untitled5.ps1:14 char:16

    + { Copy-HardDisk <<<<  "[$vmdk]" -DestinationPath "[$vmdk-bkp]" }

        + CategoryInfo          : InvalidArgument: (:) [Copy-HardDisk], ParameterBindingException

        + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomation.ViCore.Cmdlets.Commands.CopyHardDisk

    Copy-HardDisk : Cannot bind parameter 'HardDisk'. Cannot convert the "[[local] OL55/OL55.vmdk [vm_os] OL55/OL55.vmdk]" value of type "System.String" to type "VMware.VimAutomation.ViCore.Types.V1.VirtualDevice.HardDisk".

    At C:\Untitled5.ps1:14 char:16

    + { Copy-HardDisk <<<<  "[$vmdk]" -DestinationPath "[$vmdk-bkp]" }

        + CategoryInfo          : InvalidArgument: (:) [Copy-HardDisk], ParameterBindingException

        + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomation.ViCore.Cmdlets.Commands.CopyHardDisk

    Any help is appreciated!!!

    Thank you!



  • 2.  RE: Foreach loop for Copy-HardDisk cmdlet

    Posted Jan 14, 2015 08:38 PM

    Afaik, you can't use the Copy-Harddisk cmdlet to copy and rename a VMDK.

    But you could use the datastore provider and the Copy-DatastoreItem cmdlet, with that cmdlet you can give a new name for the copy

    Something like this

    $hd = Get-VM -Name MyVM | Get-HardDisk

    $dsName,$path = ($hd.Filename.split('[] '))[1,3]

    $newPath = $path.replace('.vmdk','-bkp.vmdk')

    $ds = Get-Datastore -Name $dsName

    New-PSDrive -Name DS -Location $ds -PSProvider VimDatastore -Root '\'

    Copy-DatastoreItem -Item "DS:\$($path)" -Destination "DS:\$($newPath)"

    Remove-PSDrive -Name DS -Confirm:$false



  • 3.  RE: Foreach loop for Copy-HardDisk cmdlet

    Posted Jan 17, 2015 12:57 AM

    Hi LucD,

    Thank you very much for your answer. This is exactly what I have been looking for. I just have a problem with the split file manipulation part. For some reason, the Filename.split is not returning the correct information. I tried different syntax, but unfortunately it didn't work. Do you know what could be causing this error?

    You cannot call a method on a null-valued expression

    + $dsName,$path = ($hd.Filename.split <<<< ('[] '))[1,3]

        + CategoryInfo          : InvalidOperation: (split:String) [], RuntimeException

        + FullyQualifiedErrorId : InvokeMethodOnNull

    .

    I cut the code in a small part just for debugging proposes... I got the same error.

    $vm ="OL55"

    $hd = Get-VM -Name $vm | Get-HardDisk

    ($hd.Filename -split "([])")

    Bad argument to operator '-split': parsing "([])" - Unterminated [] set..

    At line:3 char:21

    + ($hd.Filename -split <<<<  "([])")

        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException

        + FullyQualifiedErrorId : BadOperatorArgument

    Thank you for your help.

    I really appreciate!



  • 4.  RE: Foreach loop for Copy-HardDisk cmdlet

    Posted Jan 17, 2015 01:21 AM

    Well, the idea was that the filename would look something like this

    "[datastore] VMname\VMname.vmdk"

    The split method should split that string on the 2 square brackets and the blank, resulting in 4 parts

    $null

    datastore

    $null

    VMname\VMname.vmdk

    From those four we need the 2nd one (index 1) and the 4th one (index 3).

    But from the error it looks as if the Filename property is empty.



  • 5.  RE: Foreach loop for Copy-HardDisk cmdlet

    Posted Jan 17, 2015 02:28 AM

    LucD, thank you for your answer.

    Sure. I agree! This exactly what I am looking for.

    I don;t know why, but if I ran just the Get-VM command against the VM that I am working with the Filename returns the correct information. If I include a Select -expand Filename, also it returns the correct information...

    PowerCLI C:\> Get-VM -Name OL55| Get-HardDisk

    CapacityGB      Persistence                                            Filename

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

    10.000          Persistent                               [local] OL55/OL55.vmdk

    1.000           Persistent                               [vm_os] OL55/OL55.vmdk



  • 6.  RE: Foreach loop for Copy-HardDisk cmdlet

    Posted Jan 17, 2015 05:03 PM

    Can you check if the following returns the datastorename and the path ?

    $fileName = Get-VM -Name OL55| Get-HardDisk | Select -First 1 -ExpandProperty Filename

    $fileName.split('[] '))[1,3]



  • 7.  RE: Foreach loop for Copy-HardDisk cmdlet

    Posted Jan 20, 2015 08:19 PM

    Hi LucD,

    Yep! this one is working fine. Thank you very much for your help. I really appreciate!

    PowerCLI C:\> $fileName -HardDisk | Select -First 1 -ExpandProperty Filename

    PowerCLI C:\> $fileName.split('[] ')[1,3]

    Results:

    local

    OL55/OL55_1.vmdk



  • 8.  RE: Foreach loop for Copy-HardDisk cmdlet

    Posted Jan 20, 2015 09:50 PM

    I think I know what happened, you have the value to the Split method in double quotes.

    As a result PowerShell will look inside the string to try and substitute variables and/or expression.

    The square brackets in the string cause the error message.

    Place the Split parameter between single quotes.