Allow user create Hyper-V VM

The project is to automate VM creation. The server is Windows Hyper-V Free 2016. Following the blog post https://www.starwindsoftware.com/blog/automate-the-hyper-v-virtual-machine-deployment-with-powershell I wrote the script creates VM for a specific user (it binds to user email):

$User = Read-Host -Prompt 'Input the user email'

$VMName = "sr-si-01_$User"
# Automatic Start Action (Nothing = 0, Start =1, StartifRunning = 2)
$AutoStartAction = 0
# In second
$AutoStartDelay  = 10
# Automatic Start Action (TurnOff = 0, Save =1, Shutdown = 2)
$AutoStopAction  = 2

###### Hardware Configuration ######
# VM Path
$VMPath         = "E:\VMs\"

# VM Generation (1 or 2)
$Gen            = 2

# Processor Number
$ProcessorCount = 1

## Memory (Static = 0 or Dynamic = 1)
$Memory         = 1
# StaticMemory
$StaticMemory   = 2GB

# DynamicMemory
$StartupMemory  = 2GB
$MinMemory      = 1GB
$MaxMemory      = 4GB

# Sysprep VHD path (The VHD will be copied to the VM folder)
$SysVHDPath     = "E:\Windows8dot1.vhdx"
# Rename the VHD copied in VM folder to:
$OsDiskName     = $VMName

### Additional virtual drives
$ExtraDrive  = @()
# Drive 1
$Drive       = New-Object System.Object
$Drive       | Add-Member -MemberType NoteProperty -Name Name -Value Data-$VMName
$Drive       | Add-Member -MemberType NoteProperty -Name Path -Value $($VMPath + "\" + $VMName)
$Drive       | Add-Member -MemberType NoteProperty -Name Size -Value 10GB
$Drive       | Add-Member -MemberType NoteProperty -Name Type -Value Dynamic
$ExtraDrive += $Drive

# Drive 2
$Drive       = New-Object System.Object
$Drive       | Add-Member -MemberType NoteProperty -Name Name -Value Bin-$VMName
$Drive       | Add-Member -MemberType NoteProperty -Name Path -Value $($VMPath + "\" + $VMName)
$Drive       | Add-Member -MemberType NoteProperty -Name Size -Value 20GB
$Drive       | Add-Member -MemberType NoteProperty -Name Type -Value Fixed
$ExtraDrive += $Drive
# You can copy/delete this below block as you wish to create (or not) and attach several VHDX

### Network Adapters
# Primary Network interface: VMSwitch 
$VMSwitchName = "Internal"
$VlanId       = 0
$VMQ          = $False
$IPSecOffload = $False
$SRIOV        = $False
$MacSpoofing  = $False
$DHCPGuard    = $False
$RouterGuard  = $False
$NicTeaming   = $False

## Additional NICs
$NICs  = @()

# NIC 1
$NIC   = New-Object System.Object
$NIC   | Add-Member -MemberType NoteProperty -Name VMSwitch -Value "Internal"
$NIC   | Add-Member -MemberType NoteProperty -Name VLAN -Value 10
$NIC   | Add-Member -MemberType NoteProperty -Name VMQ -Value $False
$NIC   | Add-Member -MemberType NoteProperty -Name IPsecOffload -Value $True
$NIC   | Add-Member -MemberType NoteProperty -Name SRIOV -Value $False
$NIC   | Add-Member -MemberType NoteProperty -Name MacSpoofing -Value $False
$NIC   | Add-Member -MemberType NoteProperty -Name DHCPGuard -Value $False
$NIC   | Add-Member -MemberType NoteProperty -Name RouterGuard -Value $False
$NIC   | Add-Member -MemberType NoteProperty -Name NICTeaming -Value $False
$NICs += $NIC

#NIC 2
#$NIC   = New-Object System.Object
#$NIC   | Add-Member -MemberType NoteProperty -Name VMSwitch -Value "LS_VMWorkload"
#$NIC   | Add-Member -MemberType NoteProperty -Name VLAN -Value 20
#$NIC   | Add-Member -MemberType NoteProperty -Name VMQ -Value $False
#$NIC   | Add-Member -MemberType NoteProperty -Name IPsecOffload -Value $True
#$NIC   | Add-Member -MemberType NoteProperty -Name SRIOV -Value $False
#$NIC   | Add-Member -MemberType NoteProperty -Name MacSpoofing -Value $False
#$NIC   | Add-Member -MemberType NoteProperty -Name DHCPGuard -Value $False
#$NIC   | Add-Member -MemberType NoteProperty -Name RouterGuard -Value $False
#$NIC   | Add-Member -MemberType NoteProperty -Name NICTeaming -Value $False
#$NICs += $NIC
# You can copy/delete the above block and set it for additional NIC


######################################################
###           VM Creation and Configuration        ###
######################################################

## Creation of the VM
# Creation without VHD and with a default memory value (will be changed after)
New-VM -Name $VMName -Path $VMPath -NoVHD -Generation $Gen -MemoryStartupBytes 1GB -SwitchName $VMSwitchName

if ($AutoStartAction -eq 0){$StartAction = "Nothing"}
Elseif ($AutoStartAction -eq 1){$StartAction = "Start"}
Else{$StartAction = "StartIfRunning"}

if ($AutoStopAction -eq 0){$StopAction = "TurnOff"}
Elseif ($AutoStopAction -eq 1){$StopAction = "Save"}
Else{$StopAction = "Shutdown"}

## Changing the number of processor and the memory
# If Static Memory
if (!$Memory){

    Set-VM -Name $VMName -ProcessorCount $ProcessorCount -StaticMemory -MemoryStartupBytes $StaticMemory -AutomaticStartAction $StartAction -AutomaticStartDelay $AutoStartDelay -AutomaticStopAction $StopAction

}
# If Dynamic Memory
Else{
    Set-VM -Name $VMName -ProcessorCount $ProcessorCount -DynamicMemory -MemoryMinimumBytes $MinMemory -MemoryStartupBytes $StartupMemory -MemoryMaximumBy

How to allow a non-administrator user to run the script? Если возможно, как разрешить пользователю удаленно выполнять скрипт?

2
задан 18 August 2017 в 21:49
2 ответа

Если вы добавите их учетные записи пользователей в локальную группу администраторов Hyper-V, они смогут создавать виртуальные машины с Hyper-V. Если вы обновите свой сценарий, чтобы использовать параметр -ComputerName , либо заключите команду создания в Invoke-Command . Я не знаю, потребуются ли пользователям разрешения для удаленного доступа к серверу Hyper-V после получения разрешений администратора Hyper-V.

2
ответ дан 3 December 2019 в 09:32

Для удаленного запуска скрипта добавьте в пользовательский скрипт следующий код

Enable-PSRemoting -Force

$Server = Read-Host -Prompt 'Input the Hype-V server's name or IP Address'

Enter-PSSession -ComputerName $Server

Источники:

https://techtalk.gfi.com/how-to-manage-your-servers-remotely- with-powershell /

https://blogs.technet.microsoft.com/heyscriptingguy/2013/12/11/use-powershell-to-create-remote-session

3
ответ дан 3 December 2019 в 09:32

Теги

Похожие вопросы