Created
April 3, 2024 13:22
-
-
Save gowatana/957ea8dc90f3b8094d0cb2286cb87ea2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# VMの構成ファイルだけ別データストアに移動するスクリプト | |
# | |
# PowerCLIを使用して、個々のディスクを別々のデータストアに移行する方法 | |
# https://communities.vmware.com/t5/-/-/m-p/3014244 | |
# 配置指定 | |
$vm_name = "test-vm01" | |
$target_ds_name_vmdk = "vsanDatastore" # VMDKのデータストア名 | |
$target_ds_name_vmx = "ds-nfs-repo-01" # 構成ファイル移動先のデータストア名 | |
# テストの準備(再配置) | |
Get-VM -Name $vm_name | Move-VM -Datastore $target_ds_name_vmdk -Confirm:$false | |
# select -First 1 は同名VMがひとつだけである想定にする調整 | |
$vm = Get-VM -Name $vm_name | select -First 1 | |
# Idを取得 | |
$vm_id = $vm.Id | |
$ds_id_vmdk = (Get-Datastore -Name $target_ds_name_vmdk).ExtensionData.MoRef.Value | |
$ds_id_vmx = (Get-Datastore -Name $target_ds_name_vmx).ExtensionData.MoRef.Value | |
$spec = New-Object VMware.Vim.VirtualMachineRelocateSpec | |
$vmdk_count = ($vm | Get-HardDisk).Count | |
$spec.Disk = New-Object VMware.Vim.VirtualMachineRelocateSpecDiskLocator[] ($vmdk_count) | |
$i = 0 | |
$vm | Get-HardDisk | % { | |
$key = $_.ExtensionData.Key | |
$spec.Disk[$i] = New-Object VMware.Vim.VirtualMachineRelocateSpecDiskLocator | |
$spec.Disk[$i].DiskBackingInfo = New-Object VMware.Vim.VirtualDiskFlatVer2BackingInfo | |
$spec.Disk[$i].DiskBackingInfo.FileName = '' | |
$spec.Disk[$i].DiskBackingInfo.EagerlyScrub = $false | |
$spec.Disk[$i].DiskBackingInfo.ThinProvisioned = $true | |
$spec.Disk[$i].DiskBackingInfo.DiskMode = '' | |
$spec.Disk[$i].Datastore = New-Object VMware.Vim.ManagedObjectReference | |
$spec.Disk[$i].Datastore.Type = 'Datastore' | |
$spec.Disk[$i].Datastore.Value = $ds_id_vmdk | |
$spec.Disk[$i].DiskId = $key | |
$i++ | |
} | |
$spec.Datastore = New-Object VMware.Vim.ManagedObjectReference | |
$spec.Datastore.Type = 'Datastore' | |
$spec.Datastore.Value = $ds_id_vmx # datastore-38 | |
$_this = Get-View -Id $vm_id # VirtualMachine-vm-27311 | |
$_this.RelocateVM($spec, $null) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment