Powershell:copy-item将.vhdx附加到VM

时间:2016-07-03 22:19:45

标签: powershell hyper-v

我的目标是创建一个新VM,将.vhdx复制到某个位置,将.vhdx添加到新VM,然后再启动它。

我的问题是.vhdx没有附加到VM。我不知道为什么。

我想说Copy-Item是错误的,因为它在我运行该行时会挂起,至少看起来是这样,因为它永远不会完成,我在SSD驱动器上尝试这个。

$VM_NAME = "PS_TEST_VM"
$VM_RAM = 1GB
$VM_RAM_MAX = 2GB
$VM_CPUS = "4"
$VM_DEST_PATH = "C:\Users\Public\Documents\Hyper-V\Virtual hard disks"
$VM_TO_COPY = "WIN81.1.vhdx"
$VM_HOST = "MAINKITTEN"
$NETWORK_SWITCH = "DEFAULTSWITCH"
$ROOT_VHD_TPL = "C:\Users\Public\Documents\Hyper-V\Virtual hard disks\WIN81.1.vhdx"

# CREATE
New-VM -Name $VM_NAME -Path $VM_DEST_PATH -NoVHD -Generation 2 -MemoryStartupBytes $VM_RAM -SwitchName $NETWORK_SWITCH 
Set-VM -Name $VM_NAME -ProcessorCount $VM_CPUS -DynamicMemory -MemoryMinimumBytes $VM_RAM -MemoryStartupBytes $VM_RAM -MemoryMaximumBytes $VM_RAM_MAX
# COPY AND ATTACH
Copy-Item -Path "${VM_DEST_PATH}\${VM_TO_COPY}" -Destination "${VM_DEST_PATH}\${VM_NAME}.vhdx"
Add-VMHardDiskDrive -VMName $VM_NAME -path "${VM_DEST_PATH}\${VM_NAME}.vhdx"

Start-VM $VM_NAME
echo "done"

1 个答案:

答案 0 :(得分:1)

您是否尝试过使用.vhdx参数先复制-Force,然后使用existing VHD parameter set syntax创建新的VM?

<强>语法

Parameter Set: Existing VHD
New-VM [[-Name] <String> ] [[-MemoryStartupBytes] <Int64> ] -VHDPath <String> [-AsJob] 
             [-BootDevice <BootDevice> ] [-ComputerName <String[]> ] [-Path <String> ] 
                    [-SwitchName <String> ] [-Confirm] [-WhatIf] [ <CommonParameters>]

<强>代码

# COPY
Copy-Item -Path "${VM_DEST_PATH}\${VM_TO_COPY}" -Destination "${VM_DEST_PATH}\${VM_NAME}.vhdx" -Force
# CREATE and Attach vhdx
New-VM -Name $VM_NAME -MemoryStartupBytes $VM_RAM -VHDPath "${VM_DEST_PATH}\${VM_NAME}.vhdx" -Path $VM_DEST_PATH -SwitchName $NETWORK_SWITCH -Generation 2
Set-VM -Name $VM_NAME -ProcessorCount $VM_CPUS -DynamicMemory -MemoryMinimumBytes $VM_RAM -MemoryStartupBytes $VM_RAM -MemoryMaximumBytes $VM_RAM_MAX

Start-VM $VM_NAME