Permitir ao usuário criar VM do Hyper-V

2

O projeto é automatizar a criação de VMs. O servidor é o Windows Hyper-V Free 2016. Seguindo a postagem do blog link Eu escrevi o script cria VM para um usuário específico (ele se liga ao email do usuário):

$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

Como permitir que um usuário não administrador execute o script? Se for possível, como permitir que o usuário execute o script remotamente?

    
por Joshua Turnwell 18.08.2017 / 20:49

2 respostas

3

Para executar o script, adicione remotamente ao script de usuário seguindo o código

Enable-PSRemoting -Force

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

Enter-PSSession -ComputerName $Server

Fontes:

link

link

    
por 21.08.2017 / 18:08
2

Se você adicionar sua conta de usuário ao grupo local de Administradores do Hyper-V, eles poderão criar VMs com o Hyper-V. Se você atualizar seu script para usar o parâmetro -ComputerName ou agrupar o comando de criação em Invoke-Command . Não sei se os usuários também precisarão de permissões para acessar remotamente o servidor Hyper-V, uma vez que tenham permissões de Administradores do Hyper-V.

    
por 19.08.2017 / 01:16