2

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? If it's possible, how to allow user to execute the script remotely?

1
  • If your goal is to allow non-administrative users to execute specific PowerShell scripts that normally require administrator access, consider Just Enough Administration (JEA). msdn.microsoft.com/en-us/library/dn896648.aspx. If this looks promising, I'll write it up more specifically as an answer. – Matthew Wetmore Aug 22 '17 at 3:00
3

To run script remotely add to user script following code

Enable-PSRemoting -Force

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

Enter-PSSession -ComputerName $Server

Sources:

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

0
2

If you add their user account to the Hyper-V Administrators local group they should be able to create VMs with Hyper-V. If you update your script to use either the -ComputerName parameter or wrap the creation command in Invoke-Command. I do not know if the users will also need permissions to remotely access the Hyper-V server once given Hyper-V Administrators permissions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.