PowerCLI

 View Only
  • 1.  Get-FileHash on vmdk file using PowerCLI

    Posted Oct 27, 2016 01:09 PM

    I need to calculate a hash on a vmdk file (around 50GB), I want to do this using PowerCLI, and In a remote way (the remote thing is critical).

    The remote vmdk file is in the datastore (ESXi host)

    I tried to map the disk the following way:

    1. connect to the Vcenter using :

         Connect-VIServer

    2. declared my variable:

         $ssds = Get-Datastore -Name "CCT_VMs_22"

         $dataStoreForSnapshots = "z"

    3. and mapped the path:

         New-PSDrive -Location $ssDs -Name $dataStoreForSnapshots -PSProvider VimDatastore -Root "" -Scope "Local"

    4.Then from the path of the vmdk location I tried:

         Get-FileHash <filepath> -Algorithm MD5

    filepath: z:\ELK-Win8x64_tmp\ELK-Win8x64_tmp.vmdk

    and get the follwing error:

    Test-Path : File [CCT_VMs_22] server@443/ILHZ-Lab/CCT_VMs_22/ELK-Win8x64_tmp/ was not found

    At

    C:\windows\system32\windowspowershell\v1.0\Modules\Microsoft.PowerShell.Utility\Microsoft.PowerShell.Utility.psm1:80

    char:20

    + ...              if(Test-Path -LiteralPath $filePath -PathType Container)

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

        + CategoryInfo          : NotSpecified: (:) [Test-Path], FileNotFound

        + FullyQualifiedErrorId : VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.FileNotFound,Microsoft.PowerShell.Com

       mands.TestPathCommand

    Get-FileHash : The file

    '\server@443\ILHZ-Lab\CCT_VMs_22\ELK-Win8x64_tmp\ELK-Win8x64_tmp.vmdk' cannot be read: Could

    not find a part of the path

    'C:\server@443\ILHZ-Lab\CCT_VMs_22\ELK-Win8x64_tmp\ELK-Win8x64_tmp.vmdk'.

    At line:1 char:1

    + Get-FileHash z:\ELK-Win8x64_tmp\ELK-Win8x64_tmp.vmdk -Algorithm MD5

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

        + CategoryInfo          : ReadError: (\server...in8x64_tmp.vmdk:PSObject) [Write-Error], WriteErrorExcepti

       on

        + FullyQualifiedErrorId : FileReadError,Get-FileHash

    any ideas how to get the following done?



  • 2.  RE: Get-FileHash on vmdk file using PowerCLI

    Posted Oct 27, 2016 01:54 PM

    I suspect tat the datastore provider doesn't implement all the features.

    That could explain why the Test-Path seems to go back to the internal notation.

    I'll have to some more testing to confirm that, and eventually find an alternative



  • 3.  RE: Get-FileHash on vmdk file using PowerCLI

    Posted Oct 27, 2016 02:32 PM

    I tried a Get-member on a vmx file item:

    Get-Item 'vmstore:\DCname\DSname\VMfolder\VMname.vmx' | Get-Member

    There is a method called GetHashCode : https://msdn.microsoft.com/en-us/library/system.object.gethashcode(v=vs.110).aspx

    My test:

    (Get-Item 'vmstore:\DCname\DSname\VMfolder\VMname.vmx').GetHashCode()


    >> 66599629


    Alter the .vmx by adding 1GB to the disk of the VM


    (Get-Item 'vmstore:\DCname\DSname\VMfolder\VMname.vmx').GetHashCode()


    >> 11991899


    I did the test on a vmx because it's easy to alter but you get the idea.

    I'm not saying it's the solution you're looking for but maybe a starting point.



  • 4.  RE: Get-FileHash on vmdk file using PowerCLI

    Posted Oct 27, 2016 04:46 PM

    The GetHashCode method gets the hash for the object, not the file.

    It is mostly used to compare two objects.



  • 5.  RE: Get-FileHash on vmdk file using PowerCLI

    Posted Oct 27, 2016 05:36 PM

    As I suspected, the datastore provider has not implemented the functionality to "read" a file.

    When you do for example (DS is the name I gave to the datastore drive)

    Get-Content -Path "DS:/vm1/vm1.vmdk"

    You get the error

    But ESXi has a builtin command for calculating the md5 sum.

    If you have SSH enabled, you can do something like this

    $vmName = 'TestVM'

    $vm = Get-VM -Name $vmName

    $hd = Get-HardDisk -VM $vm | where{$_.Name -eq 'Hard disk 1'}

    $dsName = $hd.Filename.Split(']')[0].TrimStart('[')


    $user = 'root'

    $pswd = 'P@ssW0rd!'

    $plink = 'C:\PuTTY\plink.exe'

    $plinkoptionsPre = " -pw $Pswd"

    $plinkoptions = " -batch -pw $Pswd"

    $cmd1 = "cd /vmfs/volumes/$($dsName)/$($vm.Name); md5sum $($hd.Filename.Split('/')[-1])"

    $remoteCommand = '"' + $cmd1 + '"'

    $command = $plink + " " + $plinkoptionsPre + " " + $User + "@" + $vm.VMHost + " " + $remoteCommand

    Invoke-Expression -Command $command