support/hyperv.ps1 in kitchen-hyperv-0.1.7 vs support/hyperv.ps1 in kitchen-hyperv-0.1.8

- old
+ new

@@ -1,62 +1,92 @@ +#requires -Version 2 -Modules Hyper-V -$ProgressPreference = "SilentlyContinue" +#implicitly import hyperv module to avoid powercli cmdlets +if((Get-Module -Name hyperv) -ne $null) +{ + Remove-Module -Name hyperv + Import-Module -Name hyperv +} +else +{ + Import-Module -Name hyperv +} +$ProgressPreference = 'SilentlyContinue' + function New-DifferencingDisk { - [cmdletbinding()] - param ([string[]]$Path, [string]$ParentPath) - if (-not (test-path $Path)) + [cmdletbinding()] + param ([string[]]$Path, [string]$ParentPath) + if (-not (Test-Path $Path)) { - $null = new-vhd @psboundparameters -Differencing + $null = new-vhd @psboundparameters -Differencing } } function Assert-VmRunning { - [cmdletbinding()] - param([string]$Id) + [cmdletbinding()] + param([string]$Id) - if ([string]::IsNullOrEmpty($ID)) + if ([string]::IsNullOrEmpty($Id)) { - $Output = [pscustomobject]@{Name = '';State =''} + $Output = [pscustomobject]@{ + Name = '' + State = '' + } } else { - $Output = Get-VM -Id $ID | - foreach { - if ($_.State -notlike 'Running') - { - $_ | start-vm -passthru - } - else - { - $_ - } + $Output = Get-VM -Id $Id | + ForEach-Object -Process { + if ($_.State -notlike 'Running') + { + $_ | + Start-VM -passthru + } + else + { + $_ + } } | - select Name, Id, State + Select-Object -Property Name, Id, State } - $output + $Output } function New-KitchenVM { - [cmdletbinding()] - param ( - $Generation = 1, + [cmdletbinding()] + param ( + $Generation = 1, $MemoryStartupBytes, $Name, $Path, $VHDPath, $SwitchName, - $ProcessorCount + $ProcessorCount, + $UseDynamicMemory, + $DynamicMemoryMinBytes, + $DynamicMemoryMaxBytes ) $null = $psboundparameters.remove('ProcessorCount') + $null = $psboundparameters.remove('UseDynamicMemory') + $null = $psboundparameters.remove('DynamicMemoryMinBytes') + $null = $psboundparameters.remove('DynamicMemoryMaxBytes') + $UseDynamicMemory = [Convert]::ToBoolean($UseDynamicMemory) + $vm = new-vm @psboundparameters | Set-Vm -ProcessorCount $ProcessorCount -passthru - $vm | Set-VMMemory -DynamicMemoryEnabled $false + if ($UseDynamicMemory) { + $vm | Set-VMMemory -DynamicMemoryEnabled $true -MinimumBytes $DynamicMemoryMinBytes -MaximumBytes $DynamicMemoryMaxBytes + } + else { + $vm | Set-VMMemory -DynamicMemoryEnabled $false + } + $vm | Start-Vm -passthru | foreach { $vm = $_ do { start-sleep -seconds 2 @@ -66,87 +96,108 @@ select Name, Id, State } function Get-VmIP($vm) { - $vm.networkadapters.ipaddresses | - where {$_ -match '^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$'} | - select -first 1 + start-sleep -seconds 10 + $vm.networkadapters.ipaddresses | + Where-Object { + $_ -match '^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$' + } | + Select-Object -First 1 } Function Set-VMNetworkConfiguration { [CmdletBinding()] Param ( [parameter(valuefrompipeline)] [object]$NetworkAdapter, - [String[]]$IPAddress=@(), - [String[]]$Subnet=@() + [String[]]$IPAddress = @(), + [String[]]$Subnet = @() ) - $VM = Get-WmiObject -Namespace 'root\virtualization\v2' -Class 'Msvm_ComputerSystem' | Where-Object { $_.ElementName -eq $NetworkAdapter.VMName } - $VMSettings = $vm.GetRelated('Msvm_VirtualSystemSettingData') | Where-Object { $_.VirtualSystemType -eq 'Microsoft:Hyper-V:System:Realized' } + $vm = Get-WmiObject -Namespace 'root\virtualization\v2' -Class 'Msvm_ComputerSystem' | Where-Object { + $_.ElementName -eq $NetworkAdapter.VMName + } + $VMSettings = $vm.GetRelated('Msvm_VirtualSystemSettingData') | Where-Object { + $_.VirtualSystemType -eq 'Microsoft:Hyper-V:System:Realized' + } $VMNetAdapters = $VMSettings.GetRelated('Msvm_SyntheticEthernetPortSettingData') $NetworkSettings = @() - foreach ($NetAdapter in $VMNetAdapters) { - if ($NetAdapter.Address -eq $NetworkAdapter.MacAddress) { - $NetworkSettings = $NetworkSettings + $NetAdapter.GetRelated("Msvm_GuestNetworkAdapterConfiguration") + foreach ($NetAdapter in $VMNetAdapters) + { + if ($NetAdapter.Address -eq $NetworkAdapter.MacAddress) + { + $NetworkSettings = $NetworkSettings + $NetAdapter.GetRelated('Msvm_GuestNetworkAdapterConfiguration') } } $NetworkSettings[0].IPAddresses = $IPAddress $NetworkSettings[0].Subnets = $Subnet $NetworkSettings[0].ProtocolIFType = 4096 $NetworkSettings[0].DHCPEnabled = $false - $Service = Get-WmiObject -Class "Msvm_VirtualSystemManagementService" -Namespace "root\virtualization\v2" - $setIP = $Service.SetGuestNetworkAdapterConfiguration($VM, $NetworkSettings[0].GetText(1)) + $Service = Get-WmiObject -Class 'Msvm_VirtualSystemManagementService' -Namespace 'root\virtualization\v2' + $setIP = $Service.SetGuestNetworkAdapterConfiguration($vm, $NetworkSettings[0].GetText(1)) - if ($setip.ReturnValue -eq 4096) { - $job=[WMI]$setip.job + if ($setIP.ReturnValue -eq 4096) + { + $job = [WMI]$setIP.job - while ($job.JobState -eq 3 -or $job.JobState -eq 4) { - start-sleep 1 - $job=[WMI]$setip.job + while ($job.JobState -eq 3 -or $job.JobState -eq 4) + { + Start-Sleep 1 + $job = [WMI]$setIP.job } - if ($job.JobState -ne 7) { + if ($job.JobState -ne 7) + { $job.GetError() } } - (Get-VM -id $NetworkAdapter.VmId).NetworkAdapter | select Name, IpAddress + (Get-VM -Id $NetworkAdapter.VmId).NetworkAdapter | Select-Object Name, IpAddress } function Get-VmDetail { - [cmdletbinding()] - param($Id) + [cmdletbinding()] + param($Id) - get-vm -id $Id | - foreach { - $vm = $_ - do { - start-sleep -seconds 1 - } while (-not (Get-VmIP $vm)) + Get-VM -Id $Id | + ForEach-Object { + $vm = $_ + do + { + Start-Sleep -Seconds 1 + } + while (-not (Get-VmIP $vm)) - [pscustomobject]@{ - Name = $vm.name - Id = $vm.ID - IpAddress = (Get-VmIP $vm) - } - } + [pscustomobject]@{ + Name = $vm.name + Id = $vm.ID + IpAddress = (Get-VmIP $vm) + } + } } function Get-DefaultVMSwitch { - Get-VMSwitch | Select -First 1 | Select Name, Id + Get-VMSwitch | + Select-Object -First 1 | + Select-Object Name, Id } -function Mount-VMISO { +function Mount-VMISO +{ [cmdletbinding()] param($Id, $Path) +<<<<<<< HEAD + set-VMDvdDrive -VMName (Get-VM -Id $Id).Name -Path $Path +======= set-VMDvdDrive -VMName (get-vm -id $Id).Name -Path $Path -} \ No newline at end of file +>>>>>>> brantb/dynamic-memory +}