|
| 1 | +$ErrorActionPreference = "Stop" |
| 2 | + |
| 3 | +$vmname = "OpenStack WS 2012 R2 Standard Evaluation" |
| 4 | +$vhdpath = "C:\VM\windows-server-2012-r2.vhd" |
| 5 | +$isoPath = "C:\ISO\9600.16384.WINBLUE_RTM.130821-1623_X64FRE_SERVER_EVAL_EN-US-IRM_SSS_X64FREE_EN-US_DV5.ISO" |
| 6 | +$floppyPath = "C:\tools\Autounattend.vfd" |
| 7 | +$vmSwitch = "external2" |
| 8 | + |
| 9 | +$vm = (Get-VM | where {$_.Name -eq $vmname }) |
| 10 | +if ($vm) { |
| 11 | + if ($vm.State -ne "Off") { |
| 12 | + $vm | Stop-VM -Force |
| 13 | + } |
| 14 | + $vm | Remove-VM -Force |
| 15 | +} |
| 16 | + |
| 17 | +if(Test-Path $vhdpath) { |
| 18 | + del $vhdpath |
| 19 | +} |
| 20 | + |
| 21 | +New-VHD $vhdpath -Dynamic -SizeBytes (16 * 1024 * 1024 * 1024) |
| 22 | +$vm = New-VM $vmname -MemoryStartupBytes (2 * 1024 * 1024 *1024) |
| 23 | +$vm | Set-VM -ProcessorCount 2 |
| 24 | +$vm.NetworkAdapters | Connect-VMNetworkAdapter -SwitchName $vmSwitch |
| 25 | +$vm | Add-VMHardDiskDrive -ControllerType IDE -Path $vhdpath |
| 26 | +$vm | Add-VMDvdDrive -Path $isopath |
| 27 | +$vm | Set-VMFloppyDiskDrive -Path $floppyPath |
| 28 | + |
| 29 | +$vm | Start-Vm |
| 30 | + |
| 31 | +while((Get-VM $vmname).State -ne "Off") { |
| 32 | + Start-Sleep 5 |
| 33 | +} |
| 34 | + |
| 35 | +$ENV:OS_USERNAME="admin" |
| 36 | +$ENV:OS_TENANT_NAME="admin" |
| 37 | +$ENV:OS_PASSWORD="b3de08ab70e2456c" |
| 38 | +$ENV:OS_AUTH_URL="http://192.168.209.130:35357/v2.0/" |
| 39 | + |
| 40 | +$imageName="Windows Server 2012 R2 Std Eval VHD" |
| 41 | +$vmName = "vm1" |
| 42 | +$keyPath = "C:\Tools\id_rsa_key1" |
| 43 | + |
| 44 | +glance show $imageName | out-null |
| 45 | +if (!$lastexitcode) { |
| 46 | + glance image-delete "$IMAGE_NAME" |
| 47 | +} |
| 48 | + |
| 49 | +cmd /c glance image-create --property hypervisor_type=hyperv --name "$imageName" --container-format bare --disk-format vhd `< $vhdpath |
| 50 | +if ($LastExitCode) { throw "glance image-create failed" } |
| 51 | + |
| 52 | +$netId = (neutron net-show net1)[4].Split("|")[2].Trim() |
| 53 | +if ($LastExitCode) { throw "neutron net-show failed" } |
| 54 | + |
| 55 | +$out = nova boot --flavor fl4 --image "$imageName" --key-name key1 --nic net-id=$netId --meta admin_pass=Passw0rd --poll $vmName |
| 56 | +if ($LastExitCode) { throw "nova boot failed" } |
| 57 | + |
| 58 | +$vmId = $out[9].Split("|")[2].Trim() |
| 59 | +#$vmId = (nova show $vmName)[15].Split("|")[2].Trim() |
| 60 | +#if ($LastExitCode) { throw "nova show failed" } |
| 61 | + |
| 62 | +$instanceName = $out[6].Split("|")[2].Trim() |
| 63 | +#Get-VMConsole $instanceName |
| 64 | + |
| 65 | +$portId = (neutron port-list --device_id $vmId)[3].Split("|")[1].Trim() |
| 66 | +if ($LastExitCode) { throw "neutron port-list failed" } |
| 67 | + |
| 68 | +$floatIpId = (neutron floatingip-list)[3].Split("|")[1].Trim() |
| 69 | +if ($LastExitCode) { throw "neutron floatingip-list failed" } |
| 70 | + |
| 71 | +neutron floatingip-associate $floatIpId $portId |
| 72 | +if ($LastExitCode) { throw "neutron floatingip-associate failed" } |
| 73 | + |
| 74 | +$floatIp=(neutron floatingip-show $floatIpId)[4].Split("|")[2].Trim() |
| 75 | +if ($LastExitCode) { throw "neutron floatingip-show failed" } |
| 76 | + |
| 77 | +Write-Host "IP address: $floatIp" |
| 78 | + |
| 79 | +$password=nova get-password $vmId $keyPath |
| 80 | +if ($LastExitCode) { throw "nova get-password failed" } |
| 81 | + |
| 82 | +if(!$password) |
| 83 | +{ |
| 84 | + throw "Password not set in the metadata" |
| 85 | +} |
| 86 | + |
| 87 | +# login via RDP, to be replaced with automated testing via WinRM |
| 88 | +# check hostname |
| 89 | +#hostname -eq $vmName |
| 90 | + |
| 91 | +pause |
| 92 | + |
| 93 | +#Check partition extension |
| 94 | +#diskpart |
| 95 | +#list disk |
| 96 | +#Output must contain only disk 0 |
| 97 | + |
| 98 | +pause |
| 99 | + |
| 100 | +nova delete $vmId |
| 101 | +if ($LastExitCode) { throw "nova delete failed" } |
0 commit comments