通过PowerShlle Script修改Hyper-V虚拟机的BIOS GUID

通过PowerShell Script修改Hyper-V虚拟机的BIOS GUID


测试中总会遇到客户各种奇怪的需求和使用场景,这不又遇到了一个,客户Hyper-v虚拟机的BIOS GUID为空值导致产品出现异常。因此需要查找命令或者脚本修改Hyper-v虚拟机的BIOS GUID

脚本

很可惜,没有找到命令可以去直接修改Hyper-v Virtual Machine的guest BiosGuid,需要通过PowerShell脚本进行修改。

脚本来自powershell-script-change-bios-guid-hyper-v-virtual-machine

function New-VMBIOSGUID
{
   
	<#
	.SYNOPSIS
		Changes the BIOSGUID for Hyper-V guests running on Hyper-V versions 8/2012 or later.
	.DESCRIPTION
		Changes the BIOSGUID for Hyper-V guests running on Hyper-V versions 8/2012 or later.
		A GUID can be supplied. If not, one is automatically generated.
		If the virtual machine is running, this script will attempt to shut it down prior to the operation. Once the replacement is complete, the virtual machine will be turned back on.
	.PARAMETER VM
		The name or virtual machine object (from Get-VM) of the virtual machine whose BIOSGUID is to be changed.
	.PARAMETER NewID
		The new GUID to assign to the virtual machine. If empty, a new GUID will be automatically generated.
	.PARAMETER ComputerName
		The Hyper-V host that owns the virtual machine to be modified.
	.PARAMETER Timeout
		Number of seconds to wait when shutting down the guest before assuming the shutdown failed and ending the script.
		Default is 300 (5 minutes).
		If the virtual machine is off, this parameter has no effect.
	.PARAMETER Force
		Suppresses prompts. If this parameter is not used, you will be prompted to shut down the virtual machine if it is running and you will be prompted to replace the BIOSGUID.
		Force can shut down a running virtual machine. It cannot affect a virtual machine that is saved or paused.
	.PARAMETER WhatIf
		Performs normal WhatIf operations by displaying the change that would be made. However, the new BIOSGUID is automatically generated on each run. The one that WhatIf displays will not be used.
	.NOTES
		Version 1.0
		February 29, 2016
		Author: Eric Siron
		(c) 2016 Altaro Software

		This script comes with no warranty, express or implied. Neither Altaro Software nor Eric Siron are liable for any damages, intentional or otherwise, that arise from its use in any capacity.
	.INPUTS
		Microsoft.HyperV.PowerShell.VirtualMachine or System.String
		System.GUID
	.EXAMPLE
		New-VMBIOSGUID -VM svtest
		
		Replaces the BIOS GUID on the virtual machine named svtest with an automatically-generated ID.

	.EXAMPLE
		New-VMBIOSGUID svtest

		Exactly the same as example 1; uses positional parameter.

	.EXAMPLE
		Get-VM svtest | New-VMBIOSGUID

		Exactly the same as example 1 and 2; uses the pipeline.

	.EXAMPLE
		New-VMBIOSGUID svtest -Force

		Exactly the same as examples 1, 2, and 3; prompts suppressed.

	.EXAMPLE
		New-VMBIOSGUID svtest -NewID $Guid

		Replaces the BIOS GUID of svtest with the supplied ID. These IDs can be generated with [System.Guid]::NewGuid().

	.EXAMPLE
		New-VMBIOSGUID svtest -WhatIf

		Shows how the BIOS GUID will be changed. TIP: Use this to view the current BIOS GUID without changing it.
	#>
	#requires -Version 4
	#requires -Modules Hyper-V
	#requires -RunAsAdministrator

	[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='High')]
	param
	(
		[Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=1)][PSObject]$VM,
		[Parameter()][System.GUID]$NewID,
		[Parameter()][String]$ComputerName = $env:COMPUTERNAME,
		[Parameter()][UInt32]$Timeout = 300,
		[Parameter()][Switch]$Force
	)

	begin
	{
   
		<# adapted from http://blogs.msdn.com/b/taylorb/archive/2008/06/18/hyper-v-wmi-rich-error-messages-for-non-zero-returnvalue-no-more-32773-32768-32700.aspx #>
		function Process-WMIJob
		{
   
			param
			(
				[Parameter(ValueFromPipeline=$true)][System.Management.ManagementBaseObject]$WmiResponse,
				[Parameter()][String]$WmiClassPath = $null,
				[Parameter()][String]$MethodName = $null,
				[Parameter()][String]$VMName,
				[Parameter()][String]$ComputerName
			)
	
			process
			{
   
				$ErrorCode = 0
 
				if($WmiResponse.ReturnValue -eq 4096)
				{
   
					$Job = [WMI]$WmiResponse.Job
 
					while ($Job.JobState -eq 4)
					{
   
				
						Write-Progress -Activity ('Modifying virtual machine {0}' -f $VMName, $ComputerName) -Status ('{0}% Complete' -f $Job.PercentComplete) -PercentComplete 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值