2016-10-02 14:10:16 +00:00
Param (
[ Parameter ( Mandatory = $true ) ]
2016-10-02 16:45:51 +00:00
[ string ] $vm_config_file ,
2016-10-02 14:10:16 +00:00
[ Parameter ( Mandatory = $true ) ]
2016-10-08 14:38:42 +00:00
[ string ] $source_path ,
[ Parameter ( Mandatory = $true ) ]
[ string ] $dest_path ,
[ Parameter ( Mandatory = $true ) ]
[ string ] $data_path ,
2016-10-02 14:10:16 +00:00
[ string ] $switchname = $null ,
[ string ] $memory = $null ,
[ string ] $maxmemory = $null ,
[ string ] $cpus = $null ,
[ string ] $vmname = $null ,
[ string ] $auto_start_action = $null ,
2016-10-08 14:38:42 +00:00
[ string ] $auto_stop_action = $null ,
[ string ] $differencing_disk = $null
2016-10-02 14:10:16 +00:00
)
2016-10-08 14:38:42 +00:00
" $( $data_path ) /Snapshots "
2016-10-02 14:10:16 +00:00
# Include the following modules
$Dir = Split-Path $script:MyInvocation . MyCommand . Path
. ( [ System.IO.Path ] :: Combine ( $Dir , " utils\write_messages.ps1 " ) )
# load the config from the vmcx and make a copy for editing, use TMP path so we are sure there is no vhd at the destination
2016-10-08 14:38:42 +00:00
$vmConfig = ( Compare-VM -Copy -Path $vm_config_file -GenerateNewID -SnapshotFilePath " $( $data_path ) Snapshots " -VhdDestinationPath " $( $data_path ) Virtual Hard Disks " -VirtualMachinePath " $( $data_path ) Virtual Machines " )
2016-10-02 14:10:16 +00:00
$generation = $vmConfig . VM . Generation
if ( ! $vmname ) {
# Get the name of the vm
$vm_name = $vmconfig . VM . VMName
} else {
$vm_name = $vmname
}
if ( ! $cpus ) {
# Get the processorcount of the VM
$processors = ( Get-VMProcessor -VM $vmConfig . VM ) . Count
} else {
$processors = $cpus
}
function GetUniqueName($name ) {
Get-VM | ForEach-Object -Process {
if ( $name -eq $_ . Name ) {
$name = $name + " _1 "
}
}
return $name
}
do {
$name = $vm_name
$vm_name = GetUniqueName $name
} while ( $vm_name -ne $name )
if ( ! $memory ) {
$configMemory = Get-VMMemory -VM $vmConfig . VM
$dynamicmemory = $configMemory . DynamicMemoryEnabled
# Memory values need to be in bytes
$MemoryMaximumBytes = ( $configMemory . Maximum )
$MemoryStartupBytes = ( $configMemory . Startup )
$MemoryMinimumBytes = ( $configMemory . Minimum )
}
else {
if ( ! $maxmemory ) {
$dynamicmemory = $False
$MemoryMaximumBytes = ( $memory -as [ int ] ) * 1 MB
$MemoryStartupBytes = ( $memory -as [ int ] ) * 1 MB
$MemoryMinimumBytes = ( $memory -as [ int ] ) * 1 MB
}
else {
$dynamicmemory = $True
$MemoryMaximumBytes = ( $maxmemory -as [ int ] ) * 1 MB
$MemoryStartupBytes = ( $memory -as [ int ] ) * 1 MB
$MemoryMinimumBytes = ( $memory -as [ int ] ) * 1 MB
}
}
if ( ! $switchname ) {
$switchname = ( Get-VMNetworkAdapter -VM $vmConfig . VM ) . SwitchName
}
2016-10-08 14:38:42 +00:00
Connect-VMNetworkAdapter -VMNetworkAdapter ( Get-VMNetworkAdapter -VM $vmConfig . VM ) -SwitchName $switchname
Set-VM -VM $vmConfig . VM -NewVMName $vm_name -MemoryStartupBytes $MemoryStartupBytes
Set-VM -VM $vmConfig . VM -MemoryMinimumBytes $MemoryMinimumBytes -MemoryMaximumBytes $MemoryMaximumBytes
Set-VM -VM $vmConfig . VM -ErrorAction " Stop " -ProcessorCount $processors
2016-10-02 14:10:16 +00:00
If ( $dynamicmemory ) {
2016-10-08 14:38:42 +00:00
Set-VM -VM $vmConfig . VM -DynamicMemory
2016-10-02 14:10:16 +00:00
} else {
2016-10-08 14:38:42 +00:00
Set-VM -VM $vmConfig . VM -StaticMemory
2016-10-02 14:10:16 +00:00
}
if ( $notes ) {
2016-10-08 14:38:42 +00:00
Set-VM -VM $vmConfig . VM -Notes $notes
2016-10-02 14:10:16 +00:00
}
if ( $auto_start_action ) {
2016-10-08 14:38:42 +00:00
Set-VM -VM $vmConfig . VM -AutomaticStartAction $auto_start_action
2016-10-02 14:10:16 +00:00
}
if ( $auto_stop_action ) {
2016-10-08 14:38:42 +00:00
Set-VM -VM $vmConfig . VM -AutomaticStartAction $auto_stop_action
2016-10-02 14:10:16 +00:00
}
# Only set EFI secure boot for Gen 2 machines, not gen 1
if ( $generation -ne 1 ) {
2016-10-08 14:38:42 +00:00
Set-VMFirmware -VM $vmConfig . VM -EnableSecureBoot ( Get-VMFirmware -VM $vmConfig . VM ) . SecureBoot
2016-10-02 14:10:16 +00:00
}
2016-10-08 14:38:42 +00:00
$report = Compare-VM -CompatibilityReport $vmConfig
if ( $report . Incompatibilities . Length -gt 0 ) {
$report . Incompatibilities
Write-Error -Message ConvertTo-Json $report . Incompatibilities
2016-10-02 14:10:16 +00:00
}
2016-10-08 14:38:42 +00:00
# Differencing disk
if ( $differencing_disk ) {
# Get all controller on the VM, first scsi, then IDE if it is a Gen 1 device
$controllers = Get-VMScsiController -VM $vmConfig . VM
if ( $generation -eq 1 ) {
$controllers = @ ( $controllers ) + @ ( Get-VMIdeController -VM $vmConfig . VM )
}
2016-10-02 14:10:16 +00:00
2016-10-08 14:38:42 +00:00
foreach ( $controller in $controllers ) {
foreach ( $drive in $controller . Drives ) {
if ( [ System.IO.Path ] :: GetFileName ( $drive . Path ) -eq [ System.IO.Path ] :: GetFileName ( $source_path ) ) {
# Remove the old disk and replace it with a differencing version
$path = $drive . Path
Remove-VMHardDiskDrive $drive
New-VHD -Path $dest_path -ParentPath $source_path -ErrorAction Stop
Add-VMHardDiskDrive -VM $vmConfig . VM -Path $dest_path
}
2016-10-02 14:10:16 +00:00
}
}
2016-10-08 14:38:42 +00:00
2016-10-02 14:10:16 +00:00
}
2016-10-08 14:38:42 +00:00
Import-VM -CompatibilityReport $vmConfig
2016-10-02 14:10:16 +00:00
$vm_id = ( Get-VM $vm_name ) . id . guid
$resultHash = @ {
name = $vm_name
id = $vm_id
}
$result = ConvertTo-Json $resultHash
Write-Output -Message $result