-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorigin_change.ps1
64 lines (46 loc) · 1.53 KB
/
origin_change.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
# Set to run on start via scheduled task for Windows servers
$file = 'C:\CurrentImpervaIp.txt'
if (-not(Test-Path -Path $file -PathType Leaf)) {
try {
$null = New-Item -ItemType File -Path $file -Force -ErrorAction Stop
$Temp = (Invoke-WebRequest -uri "http://ifconfig.me/ip").Content
echo $Temp | Out-File -FilePath C:\CurrentImpervaIp.txt
Write-Host "The file [$file] has been created."
$setFile = 0
}
catch {
throw $_.Exception.Message
}
}
else {
Write-Warning "Imperva txt file exists. Comparing to current public IP.."
$setFile = 1
}
Start-Sleep -s 3
$pubIpCurrent = Get-Content -Path C:\CurrentImpervaIp.txt
$pubIpTemp = (Invoke-WebRequest -uri "http://ifconfig.me/ip").Content
if (($pubIpTemp -eq $pubIpCurrent) -and ($setFile -eq 1)) {
try {
Write-Warning "$($pubIpCurrent) is already set as orgin .. Exiting ..."
}
catch {
throw $_.Exception.Message
}
}
else {
$pubIpCurrent = $pubIpTemp
# Site Id goes here
$SiteID = '<Site ID>'
$ApiUrl = "https://my.imperva.com/api/prov/v1/sites/configure?site_id=$SiteID¶m=site_ip&value=$pubIpCurrent"
# API Details go here, Encoding needed.
$Headers = @{
"x-API-Id" = '<API Id>'
"x-API-Key" = '<API Key>'
}
Write-Warning "Changing origin server to $($pubIpCurrent)"
Start-Sleep -s 10
Invoke-RestMethod -Method Post -Headers $Headers -Uri $ApiUrl -ContentType "application/json"
Write-Host "Origin server address changed .."
echo $pubIpCurrent | Out-File -FilePath C:\CurrentImpervaIp.txt
Exit
}