LoginSignup
3
7

More than 3 years have passed since last update.

Hyper-VをPowerShellで操作する

Last updated at Posted at 2021-02-10

マニュアル

  • Hyper-V

  • Hyper-V と Windows PowerShell の使用

仮想マシン

VMの作成

New-VM -Name <VM名> -Path <VMの場所> -MemoryStartupBytes <起動時のメモリサイズ> -VHDPath <VHDファイルの場所> -Generation 2

既存のVHDXファイルを使用し、VM世代は2世代の例

VMのメモリの設定

Set-VMMemory -VMName <VM名> -DynamicMemoryEnabled $False

動的メモリは使用しない例

VMのプロセッサの設定

Set-VMProcessor -VMName <VM名> -Count 2

仮想CPU数2の例

VMのDVDドライブの設定

Add-VMDvdDrive -VMName <VM名> -ControllerNumber 0 -Path <ISOイメージのフルパス>

コントロール番号0にくっつける例

VMのファームウェアの設定

Set-VMFirmware -VMName <VM名> -FirstBootDevice <DVDドライブ名>

ブート順序の最初をDVDドライブにする例

VMの設定(チェックポイント)

Set-VM -VMName <VM名> -CheckpointType disable

チェックポイントを無効にする例

Set-VM -VMName <VM名> -CheckpointType "Production" -AutomaticCheckpointsEnabled $false

運用モードにして、自動チェックポイントを無効にする例

VMの設定(スナップショット)

Set-VM -VMName <VM名> -SnapshotFileLocation <スナップショットの保存場所>

VMの設定(スマートページ)

Set-VM -VMName <VM名> -SmartPagingFilePath <スマートページの保存場所>

VM自動起動停止

Set-VM -VMName <VM名> -AutomaticStartAction Start

Set-VM -VMName <VM名> - AutomaticStopAction Shutdown

VM時刻同期の解除

Disable-VMIntegrationService -Name "時刻の同期" -VMName <VM名>

仮想マシン ネットワークアダプタ

VMのネットワークアダプタを削除する

Remove-VMNetworkAdapter -vmname <VM名> -name (Get-VMNetworkAdapter -vmname <VM名> ).name

VMにネットワークアダプタを追加する

Add-Vmnetworkadapter -vmname <VM名> -switchname <スイッチ名>

固定MACアドレスに変更

Set-VMNetworkAdapter -vmname <VM名>-StaticMacAddress <MACアドレス>

仮想ディスク

VHDの作成

New-VHD -Path <VHDファイルのパス> -Dynamic -SizeBytes <サイズ(byte)>

VHDの可変から固定への変更

Convert-VHD -Path <VHDファイルのパス> -DestinationPath <変更後のVHDファイルのパス> -VHDType Fixed

VHDの変更は別のファイルとして指定する必要がある。変換後のVHDファイルは、VMに付け替える必要がある

VHDをVMにくっつける

Add-VMHardDiskDrive -vmname <VM名> -Path <VHDファイルのパス>

VHDの付け替えをする

Set-VMHardDiskDrive -vmname <VM名> -ControllerType SCSI -ControllerNumber 0 -ControllerLocation 1 -Path <VHDファイルのパス>

VMのエクスポート、インポート

エクスポート

Export-VM -vmname <VM名> -Path <エクスポートファイルのパス>

インポート

Import-VM -Path <インポート対象のファイルのパス(<vmid>.vmcxのファイル)> -Copy -VhdDestinationPath <VHDXのパス> -VirtualMachinePath <VMのパス> -GenerateNewId

仮想スイッチ

MACアドレスのレンジの変更

Set-VMHost -MacAddressMinimum <MACアドレス最小> -MacAddressMaximum <MACアドレス最大>

いろいろ情報みてみよう

VM

Get-VM | select-object name,path,Generation,MemoryStartup|format-table -autosize

VM Disk

(Get-VM).vmid | Get-VHD |select-object VhdFormat,vhdtype,path,size|format-table -autosize

VM ブート順序

(Get-VMFirmware (get-vm)).bootorder|format-table -autosize

Memory

Get-VM|Get-VMMemory|format-table -autosize

CPU

Get-VM|Get-VMProcessor|format-table -autosize

HardDisk

Get-VM|Get-VMScsiController|format-table -autosize

Get-VM|Get-VMHardDiskDrive|format-table -autosize

DVDDrive

Get-VM |Get-VMDvdDrive|format-table -autosize

VM NetworkAdapter

Get-VM|Get-VMNetworkAdapter|format-table -autosize

Get-VMNetworkAdapterVlan|format-table -autosize

統合サービス

Get-VM|Get-VMIntegrationService|format-table -autosize

チェックポイント、スマートページ

Get-VM|select-object vmname,checkpointtype,CheckpointFileLocation,SmartPagingFilePath|format-table -autosize

自動起動/停止アクション

Get-VM|select-object vmname,AutomaticStartAction,AutomaticStartDelay,AutomaticStopAction|format-table -autosize

MACアドレス範囲

Get-VMHost|select-object MacAddressMinimum ,MacAddressMaximum

3
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
7