- 因為我是複製過來的,所以參數值為 "button.uuid.copiedTheVM"
完整的 Script
#設定變數
$Cluster = "Secondary"
$DS = Get-Cluster -Name $Cluster | Get-Datastore | where{$_.Name -like "snap-*"}
$RootPath = ("vmstore:\Datacenter") + $DS.Name
#掛載磁碟機 路徑為RootPath
New-PSDrive -Name TgtDS -PSProvider VimDatastore -Root $RootPath | Out-Null
#搜尋磁碟機 .vmx file
$Unregistered = @( Get-ChildItem -Path TgtDS: -Recurse | where {$_.FolderPath -notmatch ".snapshot" -and $_.Name -like "*.vmx"} )
#卸載磁碟機
Remove-PSDrive -Name TgtDS
#將vm註冊至ESXi,同時修改VM名稱
foreach($VMXFile in $Unregistered){
$ESXi = Get-Cluster -Name $Cluster | Get-VMHost | Get-Random
New-VM -VMFilePath $VMXFile.DatastoreFullPath -VMHost $ESXi | %{Set-VM -VM $_ -Confirm:$False -Name ("DR-"+$_.Name)}
}
#修改VM網卡
$VMList = Get-VM -Datastore $DS.Name
foreach($vm in $VMList){
$adapterList = $vm | Get-NetworkAdapter
foreach($adapter in $adapterList){
if($Adapter.NetworkName -eq "255"){
Get-VM $vm.Name | Get-NetworkAdapter -Name $Adapter.Name | Set-NetworkAdapter -NetworkName "Test_255" -Confirm:$False
}elseif($Adapter.NetworkName -eq "28"){
Get-VM $vm.Name | Get-NetworkAdapter -Name $Adapter.Name | Set-NetworkAdapter -NetworkName "Test_28" -Confirm:$False
elseif($Adapter.NetworkName -eq "84"){
Get-VM $vm.Name | Get-NetworkAdapter -Name $Adapter.Name | Set-NetworkAdapter -NetworkName "Test_84" -Confirm:$False
}elseif($Adapter.NetworkName -eq "85"){
Get-VM $vm.Name | Get-NetworkAdapter -Name $Adapter.Name | Set-NetworkAdapter -NetworkName "Test_85" -Confirm:$False
}elseif($Adapter.NetworkName -eq "86"){
Get-VM $vm.Name | Get-NetworkAdapter -Name $Adapter.Name | Set-NetworkAdapter -NetworkName "Test_86" -Confirm:$False
}else{
Write-Host "NetworkAdapter not found"
}
}
#vm開機,回答VMQuestion
Get-VM -Datastore $ds | Start-VM | Get-VMQuestion | Set-VMQuestion -Options "button.uuid.copiedTheVM"
沒有留言:
張貼留言