|
| 1 | +function New-vServer { |
| 2 | + [CmdletBinding()] |
| 3 | + param ( |
| 4 | + [Parameter(Mandatory = $true, |
| 5 | + Position = 0, |
| 6 | + ValueFromPipeline = $true, |
| 7 | + ValueFromPipelineByPropertyName = $true)] |
| 8 | + [alias('Name')] |
| 9 | + [array] |
| 10 | + $ComputerName, |
| 11 | + |
| 12 | + [Parameter(Mandatory = $false, |
| 13 | + ValueFromPipeline = $true, |
| 14 | + ValueFromPipelineByPropertyName = $true)] |
| 15 | + [string] |
| 16 | + $Path = 'C:\Hyper-V\Virtual Machines', |
| 17 | + |
| 18 | + [Parameter(Mandatory = $false, |
| 19 | + ValueFromPipeline = $true, |
| 20 | + ValueFromPipelineByPropertyName = $true)] |
| 21 | + [ValidateSet('1GB','2GB','3','4GB')] |
| 22 | + [long] |
| 23 | + $Memory = 2GB, |
| 24 | + |
| 25 | + [Parameter(Mandatory = $false, |
| 26 | + ValueFromPipeline = $true, |
| 27 | + ValueFromPipelineByPropertyName = $true)] |
| 28 | + [ValidateSet('1','2')] |
| 29 | + [long] |
| 30 | + $ProcessorCount = '1', |
| 31 | + |
| 32 | + [Parameter(Mandatory = $false, |
| 33 | + ValueFromPipeline = $true, |
| 34 | + ValueFromPipelineByPropertyName = $true)] |
| 35 | + [string] |
| 36 | + $vSwitch = (Get-VMSwitch -SwitchType Internal).Name, |
| 37 | + |
| 38 | + [Parameter(Mandatory = $false, |
| 39 | + ValueFromPipeline = $true, |
| 40 | + ValueFromPipelineByPropertyName = $true)] |
| 41 | + [string] |
| 42 | + $ParentDisk = 'C:\Hyper-V\Base Images\Test.vhdx' |
| 43 | + ) |
| 44 | + |
| 45 | + Begin { |
| 46 | + $Split = $Path.Split('\') |
| 47 | + $VHDPath = $Split[0]+'\'+$Split[1]+'\Virtual Hard Disks' |
| 48 | + } |
| 49 | + |
| 50 | + Process { |
| 51 | + foreach ($Computer in $ComputerName) { |
| 52 | + Try { |
| 53 | + New-VHD -ParentPath $ParentDisk -Differencing -Path "$Path\$Computer\Virtual Hard Disks\$Computer.VHDX" -ErrorAction Stop |
| 54 | + } |
| 55 | + Catch [System.Exception] { |
| 56 | + Write-Error -Message $_.Exception.Message |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + if (-not (Get-VM $Computer -ErrorAction SilentlyContinue)) { |
| 61 | + New-VM -Generation 2 -Name $Computer -MemoryStartupBytes $Memory -SwitchName $vSwitch -VHDPath "$Path\$Computer\Virtual Hard Disks\$Computer.VHDX" -Path $Path |
| 62 | + Set-VM -VMName $Computer -ProcessorCount $ProcessorCount |
| 63 | + Set-VMMemory -VMName $Computer -DynamicMemoryEnabled $true -MinimumBytes 256MB -MaximumBytes $Memory -StartupBytes $Memory -Buffer 20 |
| 64 | + Get-VMIntegrationService -VMName $Computer | Enable-VMIntegrationService |
| 65 | + Start-VM $Computer |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + End {} |
| 72 | +} |
0 commit comments