· 9 years ago · Nov 23, 2016, 12:54 PM
1<#$awsAsmb = [System.Reflection.Assembly]::LoadFrom("C:\Users\Dusan\GDrive-Inspired\Development\ip_ingress\AWSPowerShell.dll")
2[System.Reflection.Assembly]::LoadFrom("C:\Users\Dusan\GDrive-Inspired\Development\ip_ingress\AWSSDK.EC2.dll")
3[System.Reflection.Assembly]::LoadFrom("C:\Users\Dusan\GDrive-Inspired\Development\ip_ingress\AWSSDK.SecurityToken.dll")
4
5#>
6<#start application#>
7##$aws = Add-Type -LiteralPath "$($env:userprofile)\Desktop\ip_ingress\AWSPowerShell.dll" -PassThru
8
9
10#>
11<#global#>
12
13$execPol = Invoke-Command -ScriptBlock {
14try { Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Force
15}
16catch {
17 return $_
18}
19} -ErrorAction SilentlyContinue
20
21if ($execPol -like "*Default*" -or $execPol -eq '') {
22
23Write-Host "Rights to execute present"
24}
25else {
26Write-Host "$($execPol) `nAbove is your error but DN says Run 'Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Force' in an elevated mode"
27}
28
29try {
30
31##$env:PSModulePath = $env:PSModulePath + ";$env:userprofile\ip_ingress_v2\dependencies"
32Import-Module AWSPowerShell
33write-host "AWS Module Imported"
34
35<#$mods = Get-Module -Name AWSPowerShell
36if($mods -ne '') {
37Import-Module AWSPowerShell
38write-host "AWS Module Imported"
39} else {
40
41Write-Host "install the AWS SDK for .net and remember to tick all boxes for powershell extentions `n installer package is included in zip directory"
42 }
43
44#>
45
46
47<#creds#>
48try{
49
50##Clear-AWSCredentials -ProfileName AWSMainConf
51
52#Set-AWSCredentials -StoreAs 'AWSMainConf' -ProfileName AWSMainConf
53
54Initialize-AWSDefaults -AccessKey 'AKIAJZYMLY5H44QX7UTA' -SecretKey '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' -ProfileName "Default" -Region us-east-1
55
56Write-Host "AWS Creds Initialised"
57
58}
59catch {
60 write-host "AWS Error: $($_.Exception.Message)"
61 }
62
63try {
64<#IP specific#>
65
66$getIp = Invoke-WebRequest -Uri "http://www.geoplugin.net/json.gp?" -Method Get -UseBasicParsing
67
68$obj = $getIp.Content
69
70$newip = $obj | ConvertFrom-Json | Select -ExpandProperty "geoplugin_request"
71
72Write-Host "Your New IP has been set as $($newip) to access SQL-EE-2014"
73
74$vpcId = 'vpc-10f77f75'
75$instanceId = 'i-72234ecd'
76$secGroupId = 'sg-89e6fdec'
77
78$portInput = Read-Host "ENTER PORT (accepted values RDP or MSSQL): "
79
80if($portInput -eq "RDP") {
81 $port = 3389
82} elseif ($portInput -eq "MSSQL"){
83 $port = 1433
84
85} else {
86 throw "port not accepted"
87}
88#$groupid = Get-EC2SecurityGroup -GroupId $secGroupId ##-GroupName "ingressGroupVPC" -GroupDescription "ingress security group for EC2-VPC usage"
89
90# define the ingress permissions; using New-Object and property assignment
91# works for PowerShell version 2 and higher
92
93[string]$IPReplace = "$($newip)/32"
94
95$ip = new-object Amazon.EC2.Model.IpPermission
96$ip.IpProtocol = "tcp"
97$ip.FromPort = $port
98$ip.ToPort = $port
99$ip.IpRanges.Add("$($IPReplace)")
100}
101
102catch {
103 write-host "IP Error: $($_.Exception.Message)"
104 }
105
106try {
107
108 try {
109 # add these ingress permissions to the security group
110 Grant-EC2SecurityGroupIngress -GroupId $secGroupId -IpPermission @($ip ) -ProfileName "Default"
111 Write-Host "this IP has been added: $($ip)"
112 }
113 catch
114 {
115 Write-Host "$($_.Exception.Message)"
116 }
117# confirm
118#$checker = ($groupid | Get-EC2SecurityGroup).IpPermission | Where {$_.FromPort -eq 1433}
119
120<#foreach ($ipitm in $checker.IpRanges) {
121 $list += "$($ipitm);`n"
122 }
123#>
124#Write-Host "this IP has been added: $($checker)"
125<#now we remove the old entries for this machine
126 write themn in a file
127#>
128 try {
129 $pathTF = "$env:userprofile\ip_ingress_v2"
130 $writeFil = "ips.txt"
131 if ((Test-Path $pathTF)) {
132
133 $file = Get-ChildItem $pathTF
134 if ($file.Name -like "ips.txt" ) {
135 write-host "path exists"
136 }
137 else {
138 New-Item "$pathTF\$writeFil"
139 }
140
141
142 } else {
143 New-Item -Path "$pathTF"
144 write-host "dir created";
145 New-Item -Path "$pathTF\$writeFil"
146 }
147
148 Write-Host "logger file created: $("$pathTF\$writeFil")"
149
150 }
151 catch {
152 Write-Host "$($_.Exception.Message)"
153 }
154<# reading the file for all past entries#>
155
156 try {
157 $cont = Get-content "$pathTF\$writeFil"
158
159 if(!($cont)) {
160 Add-Content $($pathTF+"\"+$writeFil) "Date IP"
161 Add-Content $($pathTF+"\"+$writeFil) "$((Get-date).ToString("yyyyMMddhhmmss")) $($IPReplace)"
162 } else {
163
164 Add-Content $($pathTF+"\"+$writeFil) "$((Get-date).ToString("yyyyMMddhhmmss")) $($IPReplace)"
165
166 }
167
168 $csv = "$pathTF\$writeFil" | Import-Csv -Delimiter " "
169
170 $unlistIPs = $csv | Select-Object | where {$_.IP -ne $IPReplace } ##"20151228125717" }
171
172 foreach ($unlistIP in $unlistIPs) {
173 ## Write-Host "previous IPs $($unlistIP.IP)"
174
175 $ip2 = new-object Amazon.EC2.Model.IpPermission
176 $ip2.IpProtocol = "tcp"
177 $ip2.FromPort = 1433
178 $ip2.ToPort = 1433
179 $ip2.IpRanges.Add("$($unlistIP.IP)")
180
181 Revoke-EC2SecurityGroupIngress -GroupId $secGroupId -IpPermission @($ip2)
182
183 }
184 Write-Host "Old ip: $($ip2) successfully removed"
185
186 }
187 catch {
188
189 Write-Host "$($_.Exception.Message)"
190
191 }
192 write-host "Your IP is now whitelisted $($IPReplace)"
193
194
195
196}
197
198
199catch {
200 write-host "$($_.Exception.Message)"
201 }
202<# add entry to file
203
204#>
205}
206
207catch {
208 write-host "Global error: $($_.Exception.Message) `nDN SAYS: Run in elevated mode"
209 }
210
211 finally {
212 $ip = $null
213 $groupId = $null
214 $ip2 = $null
215
216 }