新建VM_Script
在Hyper-V群集中,不需要设置VM的自启动,当宿主机意外关机重新启动后,上面的VM会自动转移到另一台主机;如果另一台主机处于关机状态,则宿主机重新启动后,其VM也会自启动(如果其VM在宿主机关机前已处于关机状态,则不会自启动)
# Get-Module -ListAvailable
#导入VMM管理模块
#Import-Module -Name virtualmachinemanager
#定义变量
$VMName = "psvmname"
$VMDescription = "psdescrib1"
$computerName = "pspcname1"
$templateName = "Template_Win08R2_3"
$hostName = "hyperv01.testt.com"
$vmhost = Get-SCVMHost | ? {$_.Name -eq $hostName}
$vmtemplate = Get-SCVMTemplate -VMMServer localhost |? {$_.name -eq $templateName}
#新建虚拟机:阻止动态优化,延迟30S启动,开机自启动,关闭GuestOS
New-SCVirtualMachine -Name $VMName -ComputerName $computerName -VMTemplate $vmtemplate -Description $VMDescription -BlockDynamicOptimization $true -ReturnImmediately -DelayStartSeconds "30" -path "C:\ClusterStorage\Volume1\" -VMHost $vmhost -StartVM -StartAction AlwaysAutoTurnOnVM -StopAction ShutdownGuestOS
-Domain "" -DomainJoinCredential
-Workgroup ""
############################################################
$UserName = "administrator"
#将纯文本字符串“boc.123”转换为安全字符串,并将结果存储在 $Password变量中
$Password = ConvertTo-SecureString "boc.123" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($UserName,$Password)
$cred.password
$cred.username
#Get-WmiObject Win32_OperatingSystem -Credential $cred -Computer 192.168.12.6
############################################################
======================================================
# Get-Module -ListAvailable
#导入VMM管理模块
#Import-Module -Name virtualmachinemanager
#定义变量
$VMName = "psvmname"
$VMDescription = "psdescrib"
$computerName = "pspcname"
$templateName = "Template_Win08R2_2"
$hardwareprofileName = "ConfigFile_Hardware"
$guestosprofileName = "ConfigFile_OS"
$hostName = "hyperv02.testt.com"
$vmhost = Get-SCVMHost | ? {$_.Name -eq $hostName}
$vmtemplate = Get-SCVMTemplate -VMMServer localhost |? {$_.name -eq $templateName}
$vmhwprofile = Get-HardwareProfile -VMMServer localhost | ? {$_.Name -eq $hardwareprofileName}
$vmosprofile = Get-GuestOSProfile -VMMServer localhost | ? {$_.Name -eq $guestosprofileName}
#使用已有的硬件和OS配置文件创建一个新的临时模板
New-SCVMTemplate -Name "Temporary NewTemplate43210" -Template $vmtemplate -HardwareProfile $vmhwprofile -GuestOSProfile $vmosprofile -ComputerName $computerName
$template_New =Get-SCVMTemplate |? {$_.name -eq "Temporary NewTemplate43210"}
#设置虚拟机配置信息
$vmconfig = New-SCVMConfiguration -VMTemplate $template_New
Set-SCVMConfiguration -VMConfiguration $vmconfig -VMHost $vmhost
Update-SCVMConfiguration -VMConfiguration $vmconfig
#新建虚拟机
New-SCVirtualMachine -Name $VMName -VMConfiguration $vmconfig -Description $VMDescription -BlockDynamicOptimization $false -ReturnImmediately -DelayStartSeconds "0" #-JobGroup "32082ef0-f4e5-4346-89a7-94bb7754dc5d"
#删除临时模板
Get-SCVMTemplate | ? {$_.Name -eq "Temporary NewTemplate43210"} |Remove-SCVMTemplate
可参考:http://technet.microsoft.com/en-us/library/jj654339(v=sc.10).aspx
=============================================================
New-VM
#########################################脚本功能######################################## #该脚本用来新建虚机,虚机可运行于不同宿主机上,使用前先编辑vmconfig.csv文件,确定虚机配置 ######################################################################################### #定义变量 #定义虚机配置文件名称,该文件与脚本位于相同目录下 $configFileName = "vmconfig.csv" #定义虚拟交换机名称 $VMSwitchName = "Broadcom NetXtreme Gigabit Ethernet - Virtual Switch" #脚本开始 $CurrentPath = $MyInvocation.MyCommand.Path.substring(0,$MyInvocation.MyCommand.Path.LastIndexOf('\')+1) $configFilePath = $CurrentPath + $configFileName $vmconfigs = Import-Csv $configFilePath foreach ($vmconfig in $vmconfigs) { $vmName = $vmconfig.Name $vmPath = $vmconfig.Path $vmPath2 = Join-Path $vmconfig.Path $vmName $vmVHDPath = Join-Path $vmPath2 "Virtual Hard Disks" New-Item $vmVHDPath -ItemType directory -Force|Out-Null $OldVHDFile = $vmconfig.OldVHDPath Copy-Item -Path $OldVHDFile $vmVHDPath $OldVHDFileName = $OldVHDFile.Split("\")[$OldVHDFile.Split("\").Length-1] $NewVHDFileName = $vmName + "_" + $OldVHDFileName Rename-Item "$vmVHDPath\$OldVHDFileName" $NewVHDFileName $vmVHDFilePath = $vmVHDPath + "\" + $NewVHDFileName $Phy_ServerName = $vmconfig.Server $MemorySize = ([int64]$vmconfig.Memory)*1024*1024 If (($NewVHDFileName.Contains("2003"))) { New-VM -Name $vmName -ComputerName $Phy_ServerName -MemoryStartupBytes $MemorySize -Path $vmPath -VHDPath $vmVHDFilePath -BootDevice IDE Remove-VMNetworkAdapter -vmname $vmName -ComputerName $Phy_ServerName Add-VMNetworkAdapter -IsLegacy $true –VMName $vmname -SwitchName $vmSwitchName -ComputerName $Phy_ServerName } Else {New-VM -Name $vmName -ComputerName $Phy_ServerName -MemoryStartupBytes $MemorySize -Path $vmPath -VHDPath $vmVHDFilePath -BootDevice IDE -SwitchName $VMSwitchName} If ($vmconfig.VlanID -ne $null) {Set-VMNetworkAdapterVlan –VMName $vmName -Access –VlanId $vmconfig.VlanID -ComputerName $Phy_ServerName} Set-VMProcessor -VMName $vmName -Count $vmconfig.CPU -ComputerName $Phy_ServerName Set-VM -VMName $vmName -AutomaticStartAction StartIfRunning -AutomaticStopAction ShutDown -ComputerName $Phy_ServerName #Start-VM -VMName $vmName -ComputerName $Phy_ServerName }
附:vmconfig.csv
Name,CPU,Memory,VlanID,Server,Path,OldVHDPath
VM04,2,2000,101,WHVCS01,C:\ClusterStorage\Volume1,D:\Hyper-V\VHD\Win2008R2.vhdx
VM05,8,8000,102,WHVCS02,C:\ClusterStorage\Volume1,D:\Hyper-V\VHD\Win2003.vhdx
===
将虚机加入到群集
get-vm |Add-VMToCluster
get-vm |select name,isclustered