Hyper-V單機(jī)服務(wù)器的一些powershell命令
雖然手里有SCVMM的授權(quán),可是我到現(xiàn)在都沒怎么去用.
下面是,對于我來說常用的一條命令..
假設(shè):我在”D:\backup\OS Templates”放了下圖這些硬盤文件.
腳本只有兩個參數(shù)需要輸入,一個是虛機(jī)名,一個是版本.
請忽略英文拼寫或語法問題
運(yùn)行命令后,先從模塊目錄中復(fù)制出相應(yīng)版本的模版文件.
以虛機(jī)名命令名存儲在hyper-v目錄中.
在虛機(jī)平臺目錄中創(chuàng)建一臺虛機(jī),
Function new-vms{
Get-Variable -Name VM* | Remove-Variable
Clear-Host
$VMName=Read-host “Input the VM Name”
$VMSwitch=”vlan”
$VMTempPath=”D:\backup\OS Templates”
$VMRootPath=”D:\Hyper-V\”
$VMPath=$VMRootPath+”Virtual Machines\”
$VHDPath=$VMRootPath+”Virtual Hard Disks\”
$VHDFiles=(-join(“$VHDPath”,”$VMName”,”-C.vhdx”))
$VMTemplates=Get-childItem $VMTempPath
Write-host ” `n List ALL Available OS Template in $VMTempPath : `n ” -ForegroundColor Yellow
Foreach ($VMversion in $VMTemplates )
{Write-Host $VMTemplates.IndexOf($VMversion) $vmversion.Name.Split(“.vhdx”) -ForegroundColor DarkYellow }
$VMVchoice=Read-Host “Type the index-number of version”
$VMTfiles=Get-childItem $VMTempPath | Select-Object -Index $VMVchoice | Copy-Item -Destination $VHDFiles -Force
New-VM -Name $VMName -MemoryStartupBytes 4GB -Path $VMPath -VHDPath $VHDFiles -SwitchName $VMSwitch
}
虛機(jī)的$VMSwitch=”vlan” 是預(yù)先定義好了.我想你也不會常改更換.
-MemoryStartupBytes 4GB 內(nèi)存填了4G.主要不想運(yùn)行命令填太多參數(shù).
VHDX加了_C 是想標(biāo)注以C盤.后期添加多余的硬盤.
這里有個注意點(diǎn).我用了索引號為選擇,所以是從0開始,這點(diǎn)要注意一下.
模版這種東西,越簡單越好.太多參數(shù).所以不方便導(dǎo)出.
這兩條有時候也會用.全關(guān), 全開虛機(jī).不常用.
但這是最基本的格式.
function shutdown-vms{
Get-Variable -Name VM* | Remove-Variable
$VMlist=get-vm|select name
foreach ($VMname in $VMlist)
{
stop-vm -Name $vmname.name
}
}
function startup-vms{
Get-Variable -Name VM* | Remove-Variable
$VMlist=get-vm|select name
foreach ($VMname in $VMlist)
{
start-vm -Name $vmname.name
}
}