{"id":3986,"date":"2024-02-01T15:13:15","date_gmt":"2024-02-01T07:13:15","guid":{"rendered":"https:\/\/benson80.eu.org\/?p=3986"},"modified":"2024-02-02T17:06:41","modified_gmt":"2024-02-02T09:06:41","slug":"powershell%e5%88%9b%e5%bb%bahyper-v%e8%99%9a%e6%8b%9f%e6%9c%ba","status":"publish","type":"post","link":"https:\/\/benson80.eu.org\/?p=3986","title":{"rendered":"PowerShell\u521b\u5efaHyper-V\u865a\u62df\u673a"},"content":{"rendered":"\n<p>\u5728\u65e5\u5e38\u5de5\u4f5c\u4e2d\uff0c\u7ecf\u5e38\u9047\u5230Hyper-V\u521b\u5efa\u865a\u62df\u673a\u7684\u60c5\u51b5\u3002<br>\u624b\u52a8\u521b\u5efa\u5341\u5206\u7e41\u7410\u4e14\u7f13\u6162\uff0c\u4ee5\u4e0b\u662f\u901a\u8fc7PowerShell\u521b\u5efaHyper-V\u865a\u62df\u673a\u7684\u65b9\u5f0f\u3002<\/p>\n\n\n\n<p><strong>PowerShell\u81ea\u52a8\u521b\u5efa\u865a\u62df\u673a<\/strong><\/p>\n\n\n\n<p><strong>Install_Ubuntu.ps1<\/strong><\/p>\n\n\n\n<p># \u68c0\u67e5\u662f\u5426\u4ee5\u7ba1\u7406\u5458\u8eab\u4efd\u8fd0\u884c\uff0c\u5982\u679c\u4e0d\u662f\uff0c\u5219\u91cd\u65b0\u4ee5\u7ba1\u7406\u5458\u8eab\u4efd\u8fd0\u884c<\/p>\n\n\n\n<p>if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {<br># \u91cd\u65b0\u4ee5\u7ba1\u7406\u5458\u8eab\u4efd\u8fd0\u884c\u811a\u672c<br>Start-Process powershell.exe -ArgumentList &#8221; -NoProfile -ExecutionPolicy Bypass -File <code>\"$($MyInvocation.MyCommand.Path)<\/code>&#8220;&#8221; -Verb RunAs<br>Exit<br>}<\/p>\n\n\n\n<p>function Create-VM {<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Param(\n    &#91;Parameter(Mandatory=$true)]\n    &#91;string]$VMName,\n    $VHDPath,  # VHD template path, can be empty\n    $BootOp = \"CD\",   # Boot Up\n    $VMRootPath = \"F:\\Hyper-V\\\",  # VM Save Path\n    $VMGen = 1,   # Memory Gen 1 or 2\n    $VMSwitch = \"Default Switch\",  # Switch Name\n    $VMISOPath = \"E:\\\u7cfb\u7edf\u76d8\\Ubuntu Server 22.04.3\\ubuntu-22.04.3-live-server-amd64.iso\",  # ISO path, can be empty\n    $VMDiskSize = 50GB,\n    $VMMem = 4GB,\n    $VMProcessorCount = 2,\n    $StaticMemory = $false\n)\n\nif ($VMRootPath -match \"\\\\$\") {\n    $VMRootPath = $VMRootPath -replace \"\\\\$\"\n}\n\n# judge VM path --inexistence,Create\nif (!(Test-Path -Path $VMRootPath)) {\n    New-Item $VMRootPath -ItemType Directory\n}\n\n$VMPath = (Get-Item $VMRootPath).FullName + \"\\\" + $VMName\nif (!(Test-Path $VMPath)) {\n    New-Item $VMPath -ItemType Directory\n}\n\n# Use existing template VHD or create a new one\nif ($VHDPath) {\n    Get-Item $VHDPath\n    Copy-Item -Path $VHDPath -Destination $($VMPath + \"\\\" + $VMName + \".vhdx\")\n    New-VM -Name $VMName -MemoryStartupBytes 4GB -Path $VMPath  -Generation 1 -SwitchName \"Default Switch\" \n\n    Add-VMHardDiskDrive -VMName $VMName  -Path $($VMPath + \"\\\" + $VMName + \".vhdx\")  -ControllerType IDE \n    $VMDiskDrive = Get-VMHardDiskDrive -VMName $VMName\n    Set-VMFirmware -FirstBootDevice $VMDiskDrive -VMName $VMName\n\n} else {\n    $VMHDPath = $VMPath + \"\\\" + $VMName + \".vhdx\"\n\n    # Check if file exists and delete if it does\n    if (Test-Path $VMHDPath) {\n        Remove-Item $VMHDPath -Force\n    }\n\n    $VMHDPath = New-VHD -Path $VMHDPath -Dynamic -SizeBytes $VMDiskSize\n    New-VM -Name $VMName -MemoryStartupBytes $VMMem -Path $VMPath  -Generation 1 -SwitchName \"Default Switch\"  -BootDevice $BootOp\n    Add-VMHardDiskDrive -VMName $VMName -Path $VMHDPath.Path -ControllerType IDE\n}\n\nif ($VMISOPath) {\n    Set-VMDvdDrive -VMName $VMName -Path $VMISOPath\n}\n\n# Set VM Processor Count and Disable AutomaticCheckpoints\nSet-VMProcessor -VMName $VMName -Count $VMProcessorCount -ExposeVirtualizationExtensions $true \nif ($StaticMemory) {\n    Set-VM -AutomaticCheckpointsEnabled $false -VMName $VMName -StaticMemory\n} else {\n    Set-VM -AutomaticCheckpointsEnabled $false -VMName $VMName\n}\n\nStart-VM $VMName<\/code><\/pre>\n\n\n\n<p>}<\/p>\n\n\n\n<p>Create-VM -VMName &#8220;Ubuntu 22.04&#8221; -VMISOPath &#8220;E:\\\u7cfb\u7edf\u76d8\\Ubuntu Server 22.04.3\\ubuntu-22.04.3-live-server-amd64.iso&#8221; -VMGen 1<\/p>\n\n\n\n<p>\u84dd\u594f\u4e91\u4e0b\u8f7d\uff1a<\/p>\n\n\n\n<p><a href=\"https:\/\/benson88.lanzn.com\/imG981n2lk3g\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/benson88.lanzn.com\/imG981n2lk3g<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u65e5\u5e38\u5de5\u4f5c\u4e2d\uff0c\u7ecf\u5e38\u9047\u5230Hyper-V\u521b\u5efa\u865a\u62df\u673a\u7684\u60c5 &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,9],"tags":[],"class_list":["post-3986","post","type-post","status-publish","format-standard","hentry","category-windows","category-9"],"_links":{"self":[{"href":"https:\/\/benson80.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/3986","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/benson80.eu.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/benson80.eu.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/benson80.eu.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/benson80.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3986"}],"version-history":[{"count":14,"href":"https:\/\/benson80.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/3986\/revisions"}],"predecessor-version":[{"id":4029,"href":"https:\/\/benson80.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/3986\/revisions\/4029"}],"wp:attachment":[{"href":"https:\/\/benson80.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3986"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/benson80.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3986"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/benson80.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3986"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}