Repository navigation
Expand file tree
/
Copy pathVmOpNotification.psm1
More file actions
223 lines (193 loc) · 10.1 KB
/
Copy pathVmOpNotification.psm1
File metadata and controls
223 lines (193 loc) · 10.1 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
function Invoke-vSphereSOAPRequest {
[cmdletbinding()]
param(
[Parameter(Mandatory = $true)]
[string] $Request,
[string[]] $Parameters,
[switch] $Initiate = $false
)
$Headers = @{
"soapaction" = "urn:vim25/8.0.0.1"
"content-type" = 'text/xml; charset="utf-8"'
}
$Uri = "https://{0}/sdk/vimService" -f $env:VMWARE_HOST
$XmlBodyTemplate = '<?xml version="1.0" encoding="UTF-8"?><Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"><Body>{0}</Body></Envelope>'
$XmlBodies = @{
"RetrieveServiceContent" = '<RetrieveServiceContent xmlns="urn:vim25"><_this type="ServiceInstance">ServiceInstance</_this></RetrieveServiceContent>'
"Login" = '<Login xmlns="urn:vim25"><_this type="SessionManager">SessionManager</_this><userName>{0}</userName><password>{1}</password><locale>en_US</locale></Login>'
"Logout" = '<Logout xmlns="urn:vim25"><_this type="SessionManager">SessionManager</_this></Logout>'
"QueryOptions" = '<QueryOptions xmlns="urn:vim25"><_this type="OptionManager">{0}</_this><name>{1}</name></QueryOptions>'
"UpdateOptions" = '<UpdateOptions xmlns="urn:vim25"><_this type="OptionManager">{0}</_this><changedValue xmlns:XMLSchema-instance="http://www.w3.org/2001/XMLSchema-instance" XMLSchema-instance:type="OptionValue"><key>{1}</key><value XMLSchema-instance:type="xsd:long">{2}</value></changedValue></UpdateOptions>'
"ReconfigVM_Task" = '<ReconfigVM_Task xmlns="urn:vim25"><_this type="VirtualMachine">{0}</_this><spec>{1}</spec></ReconfigVM_Task>'
"CreateContainerView" = '<CreateContainerView xmlns="urn:vim25"><_this type="ViewManager">ViewManager</_this><container type="Folder">{0}</container><type>{1}</type><recursive>true</recursive></CreateContainerView>'
"RetrieveContainerViewProperties" = '<RetrievePropertiesEx xmlns="urn:vim25"><_this type="PropertyCollector">propertyCollector</_this><specSet><propSet><type>ManagedEntity</type><pathSet>name</pathSet></propSet><objectSet><obj type="ContainerView">{0}</obj><skip>true</skip><selectSet xmlns:XMLSchema-instance="http://www.w3.org/2001/XMLSchema-instance" XMLSchema-instance:type="TraversalSpec"><type>ContainerView</type><path>view</path></selectSet></objectSet></specSet><options></options></RetrievePropertiesEx>'
"DestroyContainerView" = '<DestroyView xmlns="urn:vim25"><_this type="ContainerView">{0}</_this></DestroyView>'
"RetrieveObjectProperties" = '<RetrievePropertiesEx xmlns="urn:vim25"><_this type="PropertyCollector">propertyCollector</_this><specSet><propSet><type>{0}</type><pathSet>{1}</pathSet></propSet><objectSet><obj type="{2}">{3}</obj><skip>false</skip></objectSet></specSet><options></options></RetrievePropertiesEx>'
}
try {
$ProgressPreference = "SilentlyContinue"
$ConstructedBody = $XmlBodyTemplate -f ($XmlBodies[$Request] -f $Parameters)
Format-XML -Content [xml]($ConstructedBody) | Write-Verbose
if ($Initiate) {
$result = Invoke-WebRequest -SessionVariable "global:DefaultVISoapSession" -Uri $Uri -Method "POST" -Headers $Headers -Body $ConstructedBody
}
else {
$result = Invoke-WebRequest -WebSession $global:DefaultVISoapSession -Uri $Uri -Method "POST" -Headers $Headers -Body $ConstructedBody
}
$ProgressPreference = "Continue"
Format-XML -Content [xml]($result.Content) | Write-Verbose
return ([xml]($result.Content)).Envelope.Body
}
catch {
$response = $_.Exception.Response
$responseStream = $response.GetResponseStream()
$responseStream.Position = 0
$streamReader = [System.IO.StreamReader]::new($responseStream)
$responseBody = $streamReader.ReadToEnd()
$streamReader.Close()
throw "Error({0}): {1}" -f $response.StatusCode.Value__ , $responseBody
}
}
function New-vSphereSOAPSession {
[cmdletbinding()]
param()
if (-not $global:DefaultVISoapSession) {
try {
$serviceContent = Invoke-vSphereSOAPRequest -Request "RetrieveServiceContent" -Initiate
$global:DefaultVIRootFolder = $serviceContent.RetrieveServiceContentResponse.returnval.rootFolder.InnerText
$null = Invoke-vSphereSOAPRequest -Request "Login" -Parameters $env:VMWARE_USER, $env:VMWARE_PASSWORD
}
catch {
$global:DefaultVISoapSession = $null
}
}
}
function Remove-vSphereSOAPSession {
$null = Invoke-vSphereSOAPRequest -Request "Logout"
}
function Get-vSphereSOAPMoProperties {
[cmdletbinding()]
param(
[Parameter(Mandatory = $true)]
[string] $PropertyType,
[Parameter(Mandatory = $true)]
[string] $PropertyPath,
[Parameter(Mandatory = $true)]
[string] $ObjectType,
[Parameter(Mandatory = $true)]
[string] $ObjectReference
)
New-vSphereSOAPSession
$properties = Invoke-vSphereSOAPRequest -Request "RetrieveObjectProperties" -Parameters $PropertyType, $PropertyPath, $ObjectType, $ObjectReference
return $properties.RetrievePropertiesExResponse.returnval.objects
}
function Get-vSphereSOAPMoRef {
[cmdletbinding()]
param(
[Parameter(Mandatory = $true)]
[string] $Type,
[Parameter(Mandatory = $true)]
[string] $Name
)
New-vSphereSOAPSession
$containerView = Invoke-vSphereSOAPRequest -Request "CreateContainerView" -Parameters $global:DefaultVIRootFolder, $Type
$properties = Invoke-vSphereSOAPRequest -Request "RetrieveContainerViewProperties" -Parameters $containerView.CreateContainerViewResponse.returnval.InnerText
$null = Invoke-vSphereSOAPRequest -Request "DestroyContainerView" -Parameters $containerView.CreateContainerViewResponse.returnval.InnerText
foreach ($property in $properties.RetrievePropertiesExResponse.returnval.objects) {
if ($property.propSet.val.InnerText -eq $Name) {
return $property.obj.InnerText
}
}
}
function Get-VMHostVmOpNotification {
[cmdletbinding()]
param(
[Parameter(Mandatory = $true)]
[string] $Name
)
New-vSphereSOAPSession
$objectRef = Get-vSphereSOAPMoRef -Type "HostSystem" -Name $Name
$advancedOptionRef = Get-vSphereSOAPMoProperties -PropertyType "HostSystem" -PropertyPath "configManager.advancedOption" -ObjectType "HostSystem" -ObjectReference $objectRef
$queryOptions = Invoke-vSphereSOAPRequest -Request "QueryOptions" -Parameters $advancedOptionRef.propSet.val.InnerText, "VmOpNotificationToApp.Timeout"
return [PSCustomObject]@{
"Name" = $Name
"VmOpNotificationToApp.Timeout" = [int]($queryOptions.QueryOptionsResponse.returnval.value.InnerText)
}
}
function Set-VMHostVmOpNotification {
[cmdletbinding()]
param(
[Parameter(Mandatory = $true)]
[string] $Name,
[int] $Timeout
)
New-vSphereSOAPSession
$objectRef = Get-vSphereSOAPMoRef -Type "HostSystem" -Name $Name
$advancedOptionRef = Get-vSphereSOAPMoProperties -PropertyType "HostSystem" -PropertyPath "configManager.advancedOption" -ObjectType "HostSystem" -ObjectReference $objectRef
$null = Invoke-vSphereSOAPRequest -Request "UpdateOptions" -Parameters $advancedOptionRef.propSet.val.InnerText, "VmOpNotificationToApp.Timeout", $Timeout
return Get-VMHostVmOpNotification -Name $Name
}
function Get-VMVmOpNotification {
[cmdletbinding()]
param(
[Parameter(Mandatory = $true)]
[string] $Name
)
New-vSphereSOAPSession
$objectRef = Get-vSphereSOAPMoRef -Type "VirtualMachine" -Name $Name
$properties = Get-vSphereSOAPMoProperties -PropertyType "VirtualMachine" -PropertyPath "config" -ObjectType "VirtualMachine" -ObjectReference $objectRef
return [PSCustomObject]@{
"Name" = $Name
"vmOpNotificationToAppEnabled" = [System.Convert]::ToBoolean($properties.propset.val.vmOpNotificationToAppEnabled)
"vmOpNotificationTimeout" = [int]($properties.propset.val.vmOpNotificationTimeout)
}
}
function Set-VMVmOpNotification {
[cmdletbinding()]
param(
[Parameter(Mandatory = $true)]
[string] $Name,
[ValidateSet("true", "false")]
[string] $Enabled,
[int] $Timeout = $null
)
New-vSphereSOAPSession
$objectRef = Get-vSphereSOAPMoRef -Type "VirtualMachine" -Name $Name
if ($Enabled) {
$body += "<vmOpNotificationToAppEnabled>{0}</vmOpNotificationToAppEnabled>" -f $Enabled
}
if ($Timeout) {
$body += "<vmOpNotificationTimeout>{0}</vmOpNotificationTimeout>" -f $Timeout
}
$task = Invoke-vSphereSOAPRequest -Request "ReconfigVM_Task" -Parameters $objectRef, $body
while ($true) {
$properties = Get-vSphereSOAPMoProperties -PropertyType "Task" -PropertyPath "info" -ObjectType "Task" -ObjectReference $task.ReconfigVM_TaskResponse.returnval.InnerText
if ("success", "error" -contains $properties.propSet.val.state) {
break
}
Start-Sleep -Milliseconds 500
}
return Get-VMVmOpNotification -Name $Name
}
# https://devblogs.microsoft.com/powershell/format-xml/
function Format-XML ([xml]$xml, $indent = 2) {
$StringWriter = New-Object System.IO.StringWriter
$XmlWriter = New-Object System.XMl.XmlTextWriter $StringWriter
$xmlWriter.Formatting = "indented"
$xmlWriter.Indentation = $Indent
$xml.WriteContentTo($XmlWriter)
$XmlWriter.Flush()
$StringWriter.Flush()
Write-Output $StringWriter.ToString()
}
Add-Type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@