forked from poulpreben/powershell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVeeamPrnxCacheControl.ps1
More file actions
202 lines (165 loc) · 6.1 KB
/
VeeamPrnxCacheControl.ps1
File metadata and controls
202 lines (165 loc) · 6.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
Param (
[Parameter(Mandatory=$true)][string]$JobName,
[Parameter(Mandatory=$true)][ValidateSet("WriteBack", "WriteThrough")][string]$Mode
)
Function WriteLog
{
Param ([string]$logstring)
$logstring = "$(Get-Date -format s) - " + $logstring
$logstring | out-file -Filepath $logfile -append
}
Add-PSSnapin VMware.VimAutomation.Core -ErrorAction Stop
Add-PSSnapin VeeamPSSnapIn -ErrorAction Stop
# Initialize variables
$logfilepath = "C:\temp"
$logfile = $logfilepath + "\fvp_$(get-date -f yyyy_MM_dd_HH_mm_ss).log"
$passwordfile = $logfilepath + "\fvp_enc_pass.txt"
$fvp_server = "localhost"
$vcenter = "vcenter.lan.local"
$username = "domain\user"
WriteLog "Retrieving encrypted password from file $passwordfile"
Try {
$enc_pass = Get-Content $passwordfile | ConvertTo-SecureString
}
Catch {
WriteLog "Error retrieving encrypted password"
Exit 1
}
Try {
$credential = New-Object System.Management.Automation.PsCredential($username, $enc_pass)
}
Catch {
WriteLog "Error creating credential object"
Exit 1
}
# Verify the job exists
$job = Get-VBRJob -Name $JobName
WriteLog "Verifying that the job exists"
if (!$job) {
WriteLog "Backup job $jobname not found!"
Exit 2
}
$SettingsFile = $logfilepath + "\Job."+$job.TargetFile+".Settings.csv"
# Running some initial tests
if ($Mode -eq "WriteThrough") {
if (Test-Path $SettingsFile) {
WriteLog "Warning: $SettingsFile still exists from previous run. Manually remove and re-run the job"
Exit 2
} else {
WriteLog "Created peer settings file: $SettingsFile"
$SettingsFileHandle = New-Item $SettingsFile -Type File
}
} elseif ($Mode -eq "WriteBack") {
if (Test-Path $SettingsFile) {
WriteLog "Transitioning VMs to write back."
$SettingsFileHandle = Get-Content $SettingsFile
} else {
Writelog "Nothing to change, normal exit"
Exit 0
}
}
# It's showtime!
if ($Mode -eq "WriteThrough") {
WriteLog "Connecting to VMware vCenter Server: $vcenter"
$vmware = Connect-VIServer -Server $vcenter -credential $credential
writelog "Connected to VMware vCenter Server: $vcenter"
WriteLog "Getting objects in backup job."
$objects = $job.GetObjectsInJob() | ?{$_.Type -eq "Include"}
$excludes = $job.GetObjectsInJob() | ?{$_.Type -eq "Exclude"}
# Initiate empty array for VMs to exclude
[System.Collections.ArrayList]$es = @()
WriteLog "Building list of excluded job objects."
# Skip if no exclusions were found.
if ($excludes -gt 0) {
foreach ($e in $excludes) {
$e.Name
# If the object added to the job is not a VM, find the contained VMs
$view = Get-View -ViewType VirtualMachine -Filter @{"Name" = $e.Name} | Get-VIObjectByVIView
writelog View: $view
if ($view.GetType().Name -ne "VirtualMachineImpl") {
foreach ($vm in ($view | Get-VM)) {
$i = $es.Add($vm.Name)
}
} else {
$i = $es.Add($view.Name)
}
}
}
WriteLog "Building list of included objects."
# Initiate empty array for VMs to include
[System.Collections.ArrayList]$is = @()
foreach ($o in $objects) {
#$o.Name
# If the object added to the job is not a VM, find the contained VMs
$view = Get-View -ViewType VirtualMachine -Filter @{"Name" = $o.Name} | Get-VIObjectByVIView
if ($view.GetType().Name -ne "VirtualMachineImpl") {
foreach ($vm in ($view | Get-VM)) {
if ($es -notcontains $vm.Name) {
$i = $is.Add($vm.Name)
}
}
} else {
$i = $is.Add($o.Name)
}
}
WriteLog "Connecting to PernixData Management Server: $fvp_server"
Try {
import-module prnxcli -ea Stop
$prnx = Connect-PrnxServer -NameOrIpAddress $fvp_server -credentials $credential -ea Stop > $null
}
Catch {
WriteLog "Error connecting to FVP Management Server: $($_.Exception.Message)"
exit 1
}
WriteLog "Connected to PernixData Management Server: $fvp_server"
writelog "Getting list of included, powered on VMs with PernixData write back enabled."
$prnxVMs = Get-PrnxVM | Where {($_.powerState -eq "poweredOn") -and ($_.effectivePolicy -eq "7")} | Where { $is -contains $_.Name }
foreach ($vm in $prnxVMs) {
if ($vm.numWbExternalPeers -eq $null) {
$ext_peers = 0
} else {
$ext_peers = $vm.numWbExternalPeers
}
$VMName = $vm.Name
$VMWBPeers = $vm.NumWBPeers
$VMWBExternalPeers = $ext_peers
$WriteBackPeerInfo = @($VMName,$VMWBPeers,$VMWBExternalPeers)
$WriteBackPeerInfo -join ',' | Out-File $SettingsFile -Append
writelog "Transitioning $VMName (peers: $VMWBPeers, external: $VMWBExternalPeers) into write through mode."
Try {
$CacheMode = Set-PrnxAccelerationPolicy -Name $VMName -WriteThrough -ea Stop
}
Catch {
Write-Error "Failed to transition $VMName : $($_.Exception.Message)"
Exit 2
}
}
} elseif ($Mode -eq "WriteBack") {
writelog "Connecting to PernixData FVP Management Server: $fvp_server"
Try {
import-module prnxcli -ea Stop
$prnx = Connect-PrnxServer -NameOrIpAddress $fvp_server -credentials $credential -ea Stop > $null
}
Catch {
WriteLog "Error connecting to FVP Management Server: $($_.Exception.Message)"
Remove-Item -Path $SettingsFile
exit 1
}
WriteLog "Connected to PernixData Management Server: $fvp_server"
foreach ($vm in $SettingsFileHandle) {
$VMName = $vm.split(",")[0]
$VMWBPeers = $vm.split(",")[1]
$VMWBExternalPeers = $vm.split(",")[2]
writelog "Transitioning $VMName into write back mode with $VMWBPeers peers and $VMWBExternalPeers external peers."
Try {
$CacheMode = Set-PrnxAccelerationPolicy -Name $VMName -WriteBack -NumWBPeers $VMWBPeers -NumWBExternalPeers $VMWBExternalPeers -ea Stop
}
Catch {
WriteLog "Failed to transition $VMName : $($_.Exception.Message)"
Exit 2
}
}
Remove-Item -Path $SettingsFile
}
Disconnect-PrnxServer -Connection $prnx > $null
if ($vmware) { Disconnect-VIServer -server $vcenter > $null }