forked from bseltz-cohesity/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotectSQL.ps1
More file actions
58 lines (49 loc) · 2.27 KB
/
protectSQL.ps1
File metadata and controls
58 lines (49 loc) · 2.27 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
# usage: ./protectPhysicalWindows.ps1 -vip mycluster -username myusername -jobName 'My Job' -serverList ./servers.txt -exclusionList ./exclusions.txt
# process commandline arguments
[CmdletBinding()]
param (
[Parameter(Mandatory = $True)][string]$vip, # the cluster to connect to (DNS name or IP)
[Parameter(Mandatory = $True)][string]$username, # username (local or AD)
[Parameter()][string]$domain = 'local', # local or AD domain
[Parameter(Mandatory = $True)][string]$jobname,
[Parameter(Mandatory = $True)][string]$servername,
[Parameter()][string]$instancename = 'MSSQLSERVER',
[Parameter(Mandatory = $True)][string]$dbname
)
# source the cohesity-api helper code
. $(Join-Path -Path $PSScriptRoot -ChildPath cohesity-api.ps1)
# authenticate
apiauth -vip $vip -username $username -domain $domain -password $password
# get the protectionJob
$job = api get protectionJobs | Where-Object {$_.name -ieq $jobName}
if(!$job){
Write-Warning "Job $jobName not found!"
exit
}
$sources = api get protectionSources?environments=kSQL
$server = $sources.nodes | Where-Object {$_.protectionSource.name -eq $servername}
if(!$server){
Write-Warning "Server $servername not found!"
exit
}
$serverId = $server.protectionSource.id
$instance = $server.applicationNodes | Where-Object {$_.protectionSource.name -eq $instancename}
if(!$instance){
Write-Warning "Instance $instancename not found!"
exit
}
$db = $instance.nodes | Where-Object {$_.protectionSource.name -eq "$instancename/$dbname"}
if(!$db){
Write-Warning "Database $instancename/$dbname not found!"
exit
}
$dbId = $db.protectionSource.id
$sourceSpecialParameter = $job.sourceSpecialParameters | Where-Object {$_.sourceId -eq $serverId }
if(!$sourceSpecialParameter){
$job.sourceSpecialParameters += @{"sourceId" = $serverId; "sqlSpecialParameters" = @{"applicationEntityIds" = @($dbId)}}
}else{
$sourceSpecialParameter.sqlSpecialParameters.applicationEntityIds += $dbId
$sourceSpecialParameter.sqlSpecialParameters.applicationEntityIds = @($sourceSpecialParameter.sqlSpecialParameters.applicationEntityIds | Where-Object {$_ -in $instance.nodes.protectionSource.id})
}
write-host "Adding $instancename/$dbname to protection job $jobname..."
$null = api put "protectionJobs/$($job.id)" $job