PoshCode Archive  Artifact [de6e4a3a69]

Artifact de6e4a3a6936e2aa9e26addc83044655409e984a3dbb3bd081bda48615795bba:

  • File Move-VMThin-SVMotion.ps1 — part of check-in [814f89f55f] at 2018-06-10 12:58:40 on branch trunk — A powershell module to perform Storage VMotions from Thick-to-Thin. Meant to be used in place of Move-VM. Currently only accepts one VM and strings for performance reasons, will accept objects in next revision as well as more documentation. (user: jgrote size: 1903)

# encoding: ascii
# api: powershell
# title: Move-VMThin SVMotion
# description: A powershell module to perform Storage VMotions from Thick-to-Thin. Meant to be used in place of Move-VM. Currently only accepts one VM and strings for performance reasons, will accept objects in next revision as well as more documentation.
# version: 0.1
# type: function
# author: jgrote
# license: CC0
# function: Move-VMThin
# x-poshcode-id: 1579
# x-archived: 2016-09-09T23:10:27
# x-published: 2010-01-15T10:44:00
#
#
function Move-VMThin {
    PARAM(
         [Parameter(Mandatory=$true,ValueFromPipeline=$true,HelpMessage="Virtual Machine Objects to Migrate")]
         [ValidateNotNullOrEmpty()]
            [System.String]$VM
        ,[Parameter(Mandatory=$true,HelpMessage="Destination Datastore")]
         [ValidateNotNullOrEmpty()]
            [System.String]$Datastore
    )
    
	Begin {
        #Nothing Necessary to process
	} #Begin
    
    Process {        
        #Prepare Migration info, uses .NET API to specify a transformation to thin disk
        $vmView = Get-View -ViewType VirtualMachine -Filter @{"Name" = "$VM"}
        $dsView = Get-View -ViewType Datastore -Filter @{"Name" = "$Datastore"}
        
        #Abort Migration if free space on destination datastore is less than 50GB
        if (($dsView.info.freespace / 1GB) -lt 50) {throw "Move-ThinVM ERROR: Destination Datastore $Datastore has less than 50GB of free space. This script requires at least 50GB of free space for safety. Please free up space or use the VMWare Client to perform this Migration"}

        #Prepare VM Relocation Specificatoin
        $spec = New-Object VMware.Vim.VirtualMachineRelocateSpec
        $spec.datastore =  $dsView.MoRef
        $spec.transform = "sparse"
        
        #Perform Migration
        $vmView.RelocateVM($spec, $null)
    } #Process
}