forked from xenserver/xenserver-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpTest.ps1
103 lines (72 loc) · 3.75 KB
/
HttpTest.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#
# Copyright (c) Cloud Software Group, Inc.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1) Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2) Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
# OF THE POSSIBILITY OF SUCH DAMAGE.
#
Param([Parameter(Mandatory = $true)][String]$svr,
[Parameter(Mandatory = $true)][String]$usr,
[Parameter(Mandatory = $true)][String]$passwd,
[Parameter(Mandatory = $true)][String]$patchPath)
### Connect to a server
[Net.ServicePointManager]::SecurityProtocol = 'tls,tls11,tls12'
# Trust all certificates. This is for test purposes only.
# DO NOT USE -NoWarnCertificates and -NoWarnNewCertificates IN PRODUCTION CODE.
Connect-XenServer -Server $svr -UserName $usr -Password $passwd -NoWarnCertificates -NoWarnNewCertificates
### Create a VM
$template = @(Get-XenVM -Name 'Windows *' | Where-Object { $_.is_a_template })[0]
Invoke-XenVM -VM $template -XenAction Clone -NewName "testVM" -Async -PassThru |`
Wait-XenTask -ShowProgress
$vm = Get-XenVM -Name "testVM"
$sr = Get-XenSR -Ref (Get-XenPool).default_SR
$other_config = $vm.other_config
$other_config["disks"] = $other_config["disks"].Replace('sr=""', 'sr="{0}"' -f $sr.uuid)
New-XenVBD -VM $vm -VDI $null -Userdevice 3 -Bootable $false -Mode RO `
-Type CD -Unpluggable $true -Empty $true -OtherConfig @{ } `
-QosAlgorithmType "" -QosAlgorithmParams @{ }
Set-XenVM -VM $vm -OtherConfig $other_config
Invoke-XenVM -VM $vm -XenAction Provision -Async -PassThru | Wait-XenTask -ShowProgress
# Export the VM using the DataCopiedDelegate parameter to track bytes received
$path = $env:TEMP + "\vm.xva"
$trackDataReceived = [XenAPI.HTTP+DataCopiedDelegate] {
param($bytes);
Write-Host "Bytes received: $bytes" }
Export-XenVm -XenHost $svr -Uuid $vm.uuid -Path $path -DataCopiedDelegate $trackDataReceived
$vm | Remove-XenVM
### Import the previously exported VM using the ProgressDelegate parameter to track send progress
$trackProgress = [XenAPI.HTTP+UpdateProgressDelegate] {
param($percent);
Write-Progress -Activity "Importing Vm..." -PercentComplete $percent }
Import-XenVm -XenHost $svr -Path $path -ProgressDelegate $trackProgress
### Upload a patch
$trackProgress = [XenAPI.HTTP+UpdateProgressDelegate] {
param($percent);
Write-Progress -Activity "Uploading patch..." -PercentComplete $percent }
Send-XenPoolPatch -XenHost $svr -Path $patchPath
### Get host RRDs
$path = $env:TEMP + "\rrd.xml"
Receive-XenHostRrd -XenHost $svr -Path $path -DataCopiedDelegate $trackDataReceived
### Disconnect before finishing
Get-XenSession | Disconnect-XenServer