powershell 使用用户提示创建新的Hyper-V VM

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell 使用用户提示创建新的Hyper-V VM相关的知识,希望对你有一定的参考价值。

#----------------USER CREATION QUESTIONS-------------------
[string]$vmName= Read-Host ”Name of VM”
#__________________________________________________________
[int32]$generation = Read-Host "Generation Type"
#__________________________________________________________
[string]$dynamic = $null
while("yes","no" -notcontains $dynamic){
    $dynamic = Read-Host "Will this VM use dyanmic memory? (yes/no)"
}
if($dynamic -eq "yes"){
    [bool]$dynMemory = $true
    [int64]$minMemory = Read-Host "Memory Minimum (MB)"
    [int64]$maxMemory = Read-Host "Memory Maximum (MB)"
    [int64]$startMemory = Read-Host "Starting Memory (MB)"
    #convert to bytes
    $minMemory = 1MB*$minMemory
    $maxMemory = 1MB*$maxMemory
    $startMemory = 1MB*$startMemory
    [int64]$memory = $minMemory
}
else{
    [int64]$memory = Read-Host "Memory (MB)"
    #convert to bytes
    $memory = 1MB*$memory
}
#__________________________________________________________
Write-Host "--------AVAILABLE SWITCHES--------" -BackgroundColor Black
Get-VMSwitch | Select-Object -ExpandProperty Name
Write-Host "--------AVAILABLE SWITCHES--------" -BackgroundColor Black
[string]$vmSwitch = Read-Host "Please enter a virtual switch name"
#__________________________________________________________
[int32]$cpu = Read-Host "Number of CPUs"
#__________________________________________________________
[string]$vmPath = Read-Host "Enter path for VM config files (Ex E:\VM\)"
[string]$newVMPath = $vmPath
#__________________________________________________________
[string]$vhdPath = Read-Host "Enter path where .vhdx will reside (Ex E:\VHD\)"
[string]$newVHD = $vhdPath+$VMName+".vhdx"
[int64]$vhdSize = Read-Host "Enter VHDSize (GB)"
$vhdSize = [math]::round($vhdSize *1Gb, 3) #converts GB to bytes
#__________________________________________________________
 
#----------------END USER CREATION QUESTIONS---------------
 
try{
    #-----------------CONFIRM CREATE NEW VM----------------
    Write-Host "Creating new VM:" $vmName "Generation type:" $generation `
        "Starting memory:" $memory "stored at:" $newVMPath ", `
            with its .vhdx stored at:" $newVHD "(size" $vhdSize ")" -ForegroundColor Cyan
    [string]$confirm = $null
    while("yes","no" -notcontains $confirm){
        $confirm = Read-Host "Proceed? (yes/no)"
    }
    #---------------END CONFIRM CREATE NEW VM--------------
     
    if($confirm -eq "yes"){
         #------------------CREATE NEW VM-----------------------
        NEW-VM –Name $vmName -Generation $generation –MemoryStartupBytes $memory `
            -Path $newVMPath –NewVHDPath $newVHD –NewVHDSizeBytes $vhdSize | Out-Null
        Start-Sleep 5 #pause script for a few seconds to allow VM creation to complete
        #----------------END CREATE NEW VM---------------------
 
        #---------------CONFIGURE NEW VM-----------------------
        ADD-VMNetworkAdapter –VMName $vmName –Switchname $vmSwitch
        #______________________________________________________
        Set-VMProcessor –VMName $vmName –count $cpu
        #______________________________________________________
        if($dynMemory -eq $true){
            Set-VMMemory $vmName -DynamicMemoryEnabled $true -MinimumBytes $minMemory `
                -StartupBytes $startMemory -MaximumBytes $maxMemory
        }
        Start-Sleep 8 #pause script for a few seconds - allow VM config to complete
        #---------------END CONFIGURE NEW VM-------------------
        #display new VM information
        Get-VM -Name $vmName | Select Name,State,Generation,ProcessorCount,`
            @{Label=”MemoryStartup”;Expression={($_.MemoryStartup/1MB)}},`
            @{Label="MemoryMinimum";Expression={($_.MemoryMinimum/1MB)}},`
            @{Label="MemoryMaximum";Expression={($_.MemoryMaximum/1MB)}} `
            ,Path,Status | ft -AutoSize
    }
    else{
        Exit
    }
    
}
catch{
    Write-Host "An error was encountered creating the new VM" `
        -ForegroundColor Red -BackgroundColor Black
    Write-Error $_
}

以上是关于powershell 使用用户提示创建新的Hyper-V VM的主要内容,如果未能解决你的问题,请参考以下文章

powershell 将新的提示链接添加到SharePoint列表。也是在powershell中使用带参数的函数的示例。

自定义Powershell提示符

如何使用c#在Exchange邮件服务器中连接和创建新的邮件ID?

如何使用 PowerShell 启动/停止远程服务器上的服务 - Windows 2008 并提示输入凭据?

使用 powershell 添加新的 AD 用户不会使用 AD LDS 添加 samaccountname

使用PowerShell在Azure China创建Data Warehouse