Timetombs

泛义的工具是文明的基础,而确指的工具却是愚人的器物

66h / 117a
,更新于 2024-04-27T21:37:28Z+08:00 by   25c71be

[PowerShell] Hyper-V

版权声明 - CC BY-NC-SA 4.0

1 Enable Hyper-V

Enable-WindowsOptionalFeature -Online -FeatureName:Microsoft-Hyper-V -All

https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/quick-start/enable-hyper-v#enable-hyper-v-using-powershell

2 Create virtual switch

$NetNetAdapter = (Get-NetAdapter)[0]
New-VMSwitch -Name 'External Virtual Network Switch' -NetAdapterName $NetNetAdapter.Name -AllowManagementOS $TRUE

https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/quick-start/connect-to-network
https://docs.microsoft.com/en-us/powershell/module/hyper-v/new-vmswitch?view=win10-ps

3 Create virtual hard disk

$VHDFilePath = 'd:\test.vhdx'
New-VHD -Path $VHDFilePath -Dynamic -SizeBytes 32GB

https://docs.microsoft.com/en-us/powershell/module/hyper-v/new-vhd?view=win10-ps

4 Create virtual machine

$VMName = 'test_vm'
$VMPath = "d:\$VMName"
$VMSwitch = 'External Virtual Network Switch'
$InstallMedia = 'd:\en_windows_server_2016_x64_dvd_9718492.iso'
$VHDFilePath = 'd:\test.vhdx'

# Create New Virtual Machine
New-VM -Name $VMName -Generation 2 -VHDPath $VHDFilePath -SwitchName $VMSwitch -MemoryStartupBytes 2147483648 -Path $VMPath

# Add DVD Drive to Virtual Machine
Add-VMScsiController -VMName $VMName
Add-VMDvdDrive -VMName $VMName -ControllerNumber 1 -ControllerLocation 0 -Path $InstallMedia

# Mount Installation Media
$DVDDrive = Get-VMDvdDrive -VMName $VMName

# Configure Virtual Machine to Boot from DVD
Set-VMFirmware -VMName $VMName -FirstBootDevice $DVDDrive

5 config

# linux
Vm-From-Json -JsonFilePath D:\_code\blog\src\powershell\hyper-v\linux.json

# windows
Vm-From-Json -JsonFilePath D:\_code\blog\src\powershell\hyper-v\windows.json
Hyper-V Json
{
  "name": "deb1",
  "generation": 2,
  "path": "e:\\_hyper-v",
  "automatic": {
    "start": "Nothing",
    "stop": "ShutDown",
    "checkpoint": false
  },
  "boot": {
    "secure": "Off"
  },
  "checkpoint": {
    "type": "Disabled"
  },
  "cpu": {
    "count": 4
  },
  "mem": {
    "min": "1GB",
    "max": "4GB"
  },
  "net": {
    "switch": "HVS",
    "mac": "00:15:5D:02:02:01"
  },
  "vhd": {
    "path": "e:\\_vhd",
    "size": "127GB",
    "blockSize": "1MB"
  },
  "dvd": {
    "iso": "z:\\_iso\\debian-12.1.0-amd64-dvd-1.9168ff53d789537db4f5233e7dfa5e860519c44b68132b70805218f842b00041.iso"
  }
}
{
  "name": "testlinuxvm",
  "generation": 2,
  "path": "e:\\_hyper-v",
  "automatic": {
    "start": "Nothing",
    "stop": "ShutDown",
    "checkpoint": false
  },
  "boot": {
    "secure": "Off"
  },
  "checkpoint": {
    "type": "Disabled"
  },
  "cpu": {
    "count": 2
  },
  "mem": {
    "min": "1GB",
    "max": "4GB"
  },
  "net": {
    "switch": "HVS",
    "mac": "00:15:5D:02:02:01"
  },
  "vhd": {
    "path": "z:\\_vhd",
    "size": "127GB",
    "blockSize": "1MB"
  },
  "dvd": {
    "iso": "z:\\linux.iso"
  }
}
{
  "name": "ltsc2019",
  "generation": 2,
  "path": "e:\\_hyper-v",
  "automatic": {
    "start": "Nothing",
    "stop": "ShutDown",
    "checkpoint": false
  },
  "boot": {
    "secure": "On"
  },
  "checkpoint": {
    "type": "Disabled"
  },
  "cpu": {
    "count": 8
  },
  "mem": {
    "min": "8GB",
    "max": "16GB"
  },
  "net": {
    "switch": "HVS",
    "mac": "00:15:5D:02:02:09"
  },
  "vhd": {
    "path": "e:\\_vhd",
    "size": "127GB",
    "blockSize": "32MB"
  },
  "dvd": {
    "iso": null
  }
}

{
  "name": "ltsc2021",
  "generation": 2,
  "path": "e:\\_hyper-v",
  "automatic": {
    "start": "Nothing",
    "stop": "ShutDown",
    "checkpoint": false
  },
  "boot": {
    "secure": "Off"
  },
  "checkpoint": {
    "type": "Disabled"
  },
  "cpu": {
    "count": 8
  },
  "mem": {
    "min": "8GB",
    "max": "16GB"
  },
  "net": {
    "switch": "HVS",
    "mac": "00:15:5D:02:02:09"
  },
  "vhd": {
    "path": "e:\\_vhd",
    "size": "127GB",
    "blockSize": "32MB"
  },
  "dvd": {
    "iso": "z:\\_iso\\sw_dvd9_win_10_ltsc_2021_64bit_chnsimp_mlf_x22-84402.60ee40b9c76c228832d07c6157155c1cfccba959.iso"
  }
}
{
  "name": "openwrt",
  "generation": 1,
  "path": "e:\\_hyper-v",
  "automatic": {
    "start": "Nothing",
    "stop": "ShutDown",
    "checkpoint": false
  },
  "boot": {
    "secure": "Off"
  },
  "checkpoint": {
    "type": "Disabled"
  },
  "cpu": {
    "count": 2
  },
  "mem": {
    "min": "512MB",
    "max": "1GB"
  },
  "net": {
    "switch": "HVS",
    "mac": "00:15:5D:02:02:A1"
  },
  "vhd": {
    "path": "e:\\_vhd",
    "size": "16GB",
    "blockSize": "1MB"
  },
  "dvd": {
    "iso": null
  }
}
{
  "name": "tnas",
  "generation": 2,
  "path": "e:\\_hyper-v",
  "automatic": {
    "start": "Nothing",
    "stop": "ShutDown",
    "checkpoint": false
  },
  "boot": {
    "secure": "Off"
  },
  "checkpoint": {
    "type": "Disabled"
  },
  "cpu": {
    "count": 4
  },
  "mem": {
    "min": "1GB",
    "max": "8GB"
  },
  "net": {
    "switch": "HVS",
    "mac": "00:15:5D:02:02:A2"
  },
  "vhd": {
    "path": "e:\\_vhd",
    "size": "127GB",
    "blockSize": "1MB"
  },
  "dvd": {
    "iso": "z:\\_iso\\truenas-scale-22.12.3.3.6c5742b936cb5f0daea41333e23147eaae884834a1307952a55b1623b8f2c6e2.iso"
  }
}
{
  "name": "win7",
  "generation": 1,
  "path": "e:\\_hyper-v",
  "automatic": {
    "start": "Nothing",
    "stop": "ShutDown",
    "checkpoint": false
  },
  "boot": {
    "secure": "Off"
  },
  "checkpoint": {
    "type": "Disabled"
  },
  "cpu": {
    "count": 2
  },
  "mem": {
    "min": "1GB",
    "max": "4GB"
  },
  "net": {
    "switch": "HVS",
    "mac": "00:15:5D:02:02:07"
  },
  "vhd": {
    "path": "e:\\_vhd",
    "size": "127GB",
    "blockSize": "32MB"
  },
  "dvd": {
    "iso": "z:\\_iso\\cn_windows_7_professional_with_sp1_vl_build_x64_dvd_u_677816.647b26479a3a46c078f5b1364a89003a31f4aada.iso"
  }
}
{
  "name": "testwindowsvm",
  "generation": 2,
  "path": "e:\\_hyper-v",
  "automatic": {
    "start": "Nothing",
    "stop": "ShutDown",
    "checkpoint": false
  },
  "boot": {
    "secure": "Off"
  },
  "checkpoint": {
    "type": "Disabled"
  },
  "cpu": {
    "count": 2
  },
  "mem": {
    "min": "1GB",
    "max": "4GB"
  },
  "net": {
    "switch": "HVS"
  },
  "vhd": {
    "path": "z:\\_vhd",
    "size": "127GB",
    "blockSize": "32MB"
  },
  "dvd": {
    "iso": "z:\\windows.iso"
  }
}

6 参考引用

https://docs.microsoft.com/en-us/virtualization/index#pivot=main&panel=server

https://docs.microsoft.com/en-us/windows-server/virtualization/hyper-v/hyper-v-on-windows-server

https://docs.microsoft.com/en-us/windows-server/virtualization/hyper-v/plan/should-i-create-a-generation-1-or-2-virtual-machine-in-hyper-v

https://docs.microsoft.com/en-us/windows-server/virtualization/hyper-v/deploy/export-and-import-virtual-machines

https://docs.microsoft.com/en-us/windows-server/administration/performance-tuning/role/hyper-v-server/

上一篇 : [PowerShell] Script