GetAppGWCerts.psm1
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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
<#
.Synopsis Displays and Exports all the certs (in .cer format) uploaded to the selected AppGW. .Description Displays and Exports all the certs (in .cer format) uploaded to the selected AppGW. .Parameter ResourceGroupName Name of the Resource group. .Parameter AppGWName Name of the AppGW .Example # Displays and Exports all the certs (in .cer format) uploaded to the selected AppGW. Get-AppGWCerts -ResourceGroupName RGname -AppGWName AppGwname -Export #> #------------------------------------------------------------------------------ # # # THIS CODE AND ANY ASSOCIATED INFORMATION ARE PROVIDED “AS IS” WITHOUT # WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT # LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS # FOR A PARTICULAR PURPOSE. THE ENTIRE RISK OF USE, INABILITY TO USE, OR # RESULTS FROM THE USE OF THIS CODE REMAINS WITH THE USER. # #------------------------------------------------------------------------------ # This script will list the thumbprint of all the certificates uploaded to the AppGW Function Get-AppGWCerts{ Param( [Parameter(Mandatory=$true, ValueFromPipeline=$true)] [String]$ResourceGroupName, [Parameter(Mandatory=$true, ValueFromPipeline=$true)] [String]$AppGWName, [Parameter(Mandatory=$false)] [switch]$Export ) If($export -eq $true){ Write-Host "All certs will be exported to $HOME in .CER format." -ForegroundColor Green } "Only Last certificate in the chain will be presented in the output" $date = (Get-Date).ToUniversalTime() "Test Date : $($date) UTC" "`n" #Get AppGW Details $getGW = Get-AzApplicationGateway -ResourceGroupName $ResourceGroupName -Name $AppGWName if ($getgw.sku.Tier.Contains("v2") -ne "True"){ ######################### V1 part Write-Host "All Authentication Certs uploaded for $($getGW.Name) (Certs for HTTP Setting)" -ForegroundColor Green #Get the Setting Certificate details, you have to provide the name of the certificate $SettingCert = Get-AzApplicationGatewayAuthenticationCertificate -ApplicationGateway $getGW if($SettingCert.count -gt 1){ for($i=0; $i -lt $SettingCert.count; $i++){ #cmd to convert raw data to thumbprint. $Settingcertraw = $SettingCert.Data[$i] $Settingpfx= New-Object System.Security.Cryptography.X509Certificates.X509Certificate2Collection $Settingpfx.Import([System.Convert]::FromBase64String($Settingcertraw),$null,[System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable) #Output - Name and Thumbprint of the Setting certificate ("Cert name : $($SettingCert.Name[$i])" | Out-String ).split("`n") -match '\S' ("Cert Type : HTTP Setting").split("`n") -match '\S' ($Settingpfx[-1] | FL -Property Thumbprint, Subject, Issuer, Notafter, dnsnamelist | Out-String ).split("`n") -match '\S' "Expiring in : $(($Settingpfx[-1].NotAfter - $date).Days)" ("AppGwID : $($getGW.ID)" | Out-String ).split("`n") -match '\S' "`n" # Next code section will export the certificates. If you do not want to export certificate, comment these line. If($export -eq $true){ $Settingpfx | ForEach-Object { $certexpo = $Settingpfx.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Cert, "") $outPfxPath = $HOME+'\'+$($SettingCert.Name[$i])+'.cer' [io.file]::WriteAllBytes($outPfxPath, $certexpo) } } }} if($SettingCert.count -eq 1){ #cmd to convert raw data to thumbprint. $Settingcertraw = $SettingCert.Data $Settingpfx= New-Object System.Security.Cryptography.X509Certificates.X509Certificate2Collection $Settingpfx.Import([System.Convert]::FromBase64String($Settingcertraw),$null,[System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable) #Output - Name and Thumbprint of the Setting certificate ("Cert name : $($SettingCert.Name)" ).split("`n") -match '\S' ("Cert Type : HTTP Setting").split("`n") -match '\S' ($Settingpfx[-1] | FL -Property Thumbprint, Subject, Issuer, Notafter, dnsnamelist | Out-String).split("`n") -match '\S' "Expiring in : $(($Settingpfx[-1].NotAfter - $date).Days)Days" ("AppGwID : $($getGW.ID)" | Out-String ).split("`n") -match '\S' "`n" # Next code section will export the certificates. If you do not want to export certificate, comment these line. If($export -eq $true){ $Settingpfx | ForEach-Object { $certexpo = $Settingpfx.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Cert, "") $outPfxPath = $HOME+'\'+$($SettingCert.Name)+'.cer' [io.file]::WriteAllBytes($outPfxPath, $certexpo) } } } ######################### } Else{# V2 HTTP Setting certs, and V2 SSL or Listener Certs Write-Host "All Trusted Root Certs uploaded for $($getGW.Name) (Certs for HTTP Setting)" -ForegroundColor Green #Get the Setting Certificate details, you have to provide the name of the certificate $SettingCert = Get-AzApplicationGatewayTrustedRootCertificate -ApplicationGateway $getGW if($SettingCert.count -gt 1){ for($i=0; $i -lt $SettingCert.count; $i++){ #cmd to convert raw data to thumbprint. $Settingcertraw = $SettingCert.Data[$i] $Settingpfx= New-Object System.Security.Cryptography.X509Certificates.X509Certificate2Collection $Settingpfx.Import([System.Convert]::FromBase64String($Settingcertraw),$null,[System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable) #Output - Name and Thumbprint of the Setting certificate ("Cert name : $($SettingCert.Name[$i])" ).split("`n") -match '\S' ("Cert Type : HTTP Setting").split("`n") -match '\S' ($Settingpfx[-1] | FL -Property Thumbprint, Subject, Issuer, Notafter, dnsnamelist | Out-String ).split("`n") -match '\S' "Expiring in : $(($Settingpfx[-1].NotAfter - $date).Days)Days" ("AppGwID : $($getGW.ID)" | Out-String ).split("`n") -match '\S' "`n" # Next code section will export the certificates. If you do not want to export certificate, comment these line. If($export -eq $true){ $Settingpfx | ForEach-Object { $certexpo = $Settingpfx.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Cert, "") $outPfxPath = $HOME+'\'+$($SettingCert.Name[$i])+'.cer' [io.file]::WriteAllBytes($outPfxPath, $certexpo) } } } } if($SettingCert.count -eq 1){ #cmd to convert raw data to thumbprint. $Settingcertraw = $SettingCert.Data $Settingpfx= New-Object System.Security.Cryptography.X509Certificates.X509Certificate2Collection $Settingpfx.Import([System.Convert]::FromBase64String($Settingcertraw),$null,[System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable) #Output - Name and Thumbprint of the Setting certificate ("Cert name : $($SettingCert.Name)" | Out-String ).split("`n") -match '\S' ("Cert Type : HTTP Setting").split("`n") -match '\S' ($Settingpfx[-1] | FL -Property Thumbprint, Subject, Issuer, Notafter, dnsnamelist | Out-String ).split("`n") -match '\S' "Expiring in : $(($Settingpfx[-1].NotAfter - $date).Days)Days" ("AppGwID : $($getGW.ID)" | Out-String ).split("`n") -match '\S' "`n" # Next code section will export the certificates. If you do not want to export certificate, comment these line. If($export -eq $true){ $Settingpfx | ForEach-Object { $certexpo = $Settingpfx.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Cert, "") $outPfxPath = $HOME+'\'+$($SettingCert.Name)+'.cer' [io.file]::WriteAllBytes($outPfxPath, $certexpo) } } } } Write-Host "All SSL Certs uploaded for $($getGW.Name) (Certs for Listener)" -ForegroundColor Green #Get the Listener Certificate details, you have to provide the name of the certificate $ListenerCert = Get-AzApplicationGatewaySslCertificate -ApplicationGateway $getGW if($ListenerCert.count -gt 1){ [array]$kvname = $ListenerCert.KeyVaultSecretId | where {$_ -ne $null} | foreach {$_ -replace"https://" -replace ".vault.*" -replace "`n"} [array]$kvcertname = $ListenerCert.KeyVaultSecretId | where {$_ -ne $null} | foreach {$_ -replace".*/secrets/" -replace "/.*"} [array]$ListenerCertKVname = ($ListenerCert | where {$_.KeyVaultSecretId -ne $null}).Name [array]$KVID = $ListenerCert.KeyVaultSecretId | where {$_ -ne $null} for($i=0; $i -lt $kvname.count; $i++){ $kvcertdata = Get-AzKeyVaultCertificate -VaultName $kvname[$i] -Name $kvcertname[$i] $ListenerpfxKV = $kvcertdata.Certificate ("Cert name : $($ListenerCertKVname[$i])").split("`n") -match '\S' ("Cert Type : Listener").split("`n") -match '\S' ("Key VaultID : $($kvid[$i])").split("`n") -match '\S' ($ListenerpfxKV[-1] | FL -Property Thumbprint, Subject, Issuer, Notafter, dnsnamelist | Out-String).split("`n") -match '\S' "Expiring in : $(($ListenerpfxKV[-1].NotAfter - $date).Days) Days" ("AppGwID : $($getGW.ID)" | Out-String ).split("`n") -match '\S' "`n" #}#temp # Next code section will export the certificates. If you do not want to export certificate, comment these line. If($export -eq $true){ $ListenerpfxKV | ForEach-Object { $certexpo = $ListenerpfxKV.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Cert, "") $outPfxPath = $HOME+'\'+$($ListenerCertKVname[$i])+'.cer' [io.file]::WriteAllBytes($outPfxPath, $certexpo) } } }# for loop for Key vault certs # Normal Listener Cert part $normalcertcount = $ListenerCert.count - $kvname.count [array]$Listenercertraw = $ListenerCert.PublicCertData | where {$_ -ne $null} [array]$normalcertname = ($ListenerCert | where {$_.PublicCertData -ne $null}).Name #$normalcertname = $normalcertname.name #foreach ($normalcertname in $normalcertname){ for($i=0; $i -lt $normalcertcount; $i++){ #cmd to convert raw data to thumbprint. $Listenerpfx = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2collection $Listenerpfx.Import([System.Convert]::FromBase64String($Listenercertraw[$i]),$null,[System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable) #Output - Name and Thumbprint of the Listener certificate ("Cert name : $($normalcertname[$i])").split("`n") -match '\S' ("Cert Type : Listener").split("`n") -match '\S' ($Listenerpfx[-1] | FL -Property Thumbprint, Subject, Issuer, Notafter, dnsnamelist | Out-String).split("`n") -match '\S' "Expiring in : $(($Listenerpfx[-1].NotAfter - $date).Days)Days" ("AppGwID : $($getGW.ID)" | Out-String ).split("`n") -match '\S' "`n" #} # Next code section will export the certificates. If you do not want to export certificate, comment these line. If($export -eq $true){ $Listenerpfx[-1] | ForEach-Object { $certexpo = $Listenerpfx[-1].Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Cert, "") $outPfxPath = $HOME+'\'+$($normalcertname[$i])+'.cer' [io.file]::WriteAllBytes($outPfxPath, $certexpo) } } } } if($ListenerCert.count -eq 1){ if($ListenerCert.KeyVaultSecretId -ne $null){ $kvname = $ListenerCert.KeyVaultSecretId | where {$_ -ne $null} | foreach {$_ -replace"https://" -replace ".vault.*" -replace "`n"} $kvcertname = $ListenerCert.KeyVaultSecretId | where {$_ -ne $null} | foreach {$_ -replace".*/secrets/" -replace "/.*"} [array]$ListenerCertKVname = ($ListenerCert | where {$_.KeyVaultSecretId -ne $null}).Name $kvcertdata = Get-AzKeyVaultCertificate -VaultName $kvname -Name $kvcertname $ListenerpfxKV = $kvcertdata.Certificate ("Cert name : $($ListenerCertKVname)").split("`n") -match '\S' ("Cert Type : Listener").split("`n") -match '\S' ("Key VaultID : $($ListenerCert.KeyVaultSecretId)").split("`n") -match '\S' ($ListenerpfxKV[-1] | FL -Property Thumbprint, Subject, Issuer, Notafter, dnsnamelist | Out-String).split("`n") -match '\S' "Expiring in : $(($ListenerpfxKV[-1].NotAfter - $date).Days)Days" ("AppGwID : $($getGW.ID)" | Out-String ).split("`n") -match '\S' "`n" # Next code section will export the certificates. If you do not want to export certificate, comment these line. If($export -eq $true){ $ListenerpfxKV | ForEach-Object { $certexpo = $ListenerpfxKV.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Cert, "") $outPfxPath = $HOME+'\'+$($ListenerCertKVname)+'.cer' [io.file]::WriteAllBytes($outPfxPath, $certexpo) } } }# for loop for Key vault certs Else{ #normal cert part $Listenercertraw = $ListenerCert.PublicCertData | where {$_ -ne $null} #cmd to convert raw data to thumbprint. $Listenerpfx= New-Object System.Security.Cryptography.X509Certificates.X509Certificate2Collection $Listenerpfx.Import([System.Convert]::FromBase64String($Listenercertraw),$null,[System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable) #Output - Name and Thumbprint of the Listener certificate ("Cert name : $($ListenerCert.name)").split("`n") -match '\S' ("Cert Type : Listener").split("`n") -match '\S' ($Listenerpfx[-1] | FL -Property Thumbprint, Subject, Issuer, Notafter, dnsnamelist | Out-String).split("`n") -match '\S' "Expiring in : $(($Listenerpfx[-1].NotAfter - $date).Days)Days" ("AppGwID : $($getGW.ID)" | Out-String ).split("`n") -match '\S' "`n" } # Next code section will export the certificates. If you do not want to export certificate, comment these line. If($export -eq $true){ $Listenerpfx[-1] | ForEach-Object { $certexpo = $Listenerpfx[-1].Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Cert, "") $outPfxPath = $HOME+'\'+$($ListenerCert.name)+'.cer' [io.file]::WriteAllBytes($outPfxPath, $certexpo) } } } } Export-ModuleMember -Function Get-AppGWCerts |