I know that if the web service was installed with certificate, thus simple test connection in powershell like Invoke-WebRequest to that https site can give me whether it’s possible to connect there or not. However I am wondering whether it’s possible to test connection to that web without the certificate, like can it happen that connection will fail with certificate but will succeed without, because of certificate validation reasons?
Since I already can connect to that web with Invoke-WebRequest -Uri “https://…” -UseBasicParsing -TimeoutSec 10, I am wondering whether it’s possible to somehow to test the connection without certificate.
pEpOo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3
You can inspect the remote certificate in PowerShell as follows:
# Extracted portion of https://github.com/Mike-Crowley/Public-Scripts/blob/main/OSINT/Request-AdfsCerts.ps1
function Get-SSLCertificate {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$Url,
[Parameter(Mandatory = $false)]
[string]$HostHeader
)
begin {
if ($PSVersionTable.PSEdition -ne "Desktop") {
Throw "This function requires Windows PowerShell (Desktop edition)."
}
# If no host header provided, extract from URL
if (-not $HostHeader) {
$uri = [System.Uri]::new($Url)
$HostHeader = $uri.Host
}
}
process {
try {
[Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
# Make HTTPS connection and get content
$request = [Net.HttpWebRequest]::Create($Url)
$request.Host = $HostHeader
$request.AllowAutoRedirect = $false
$response = $request.GetResponse()
# Extract the certificate from the request
if ($null -ne $request.ServicePoint.Certificate) {
$HttpsCertBytes = $request.ServicePoint.Certificate.GetRawCertData()
# Extract HTTPS cert
$CertInBase64 = [convert]::ToBase64String($HttpsCertBytes)
$Cert_x509 = [Security.Cryptography.X509Certificates.X509Certificate2]([System.Convert]::FromBase64String($CertInBase64))
Write-Output $Cert_x509
}
else {
Write-Warning "No Certificate Found at $Url"
}
}
catch {
Write-Error "Error retrieving certificate: $_"
}
finally {
if ($null -ne $response) {
$response.Close()
}
}
}
}
Sample outputs:
# Returns:
# WARNING: No Certificate Found at http://example.com
Get-SSLCertificate -Url http://example.com
# In PS 6+ returns an error:
# This function requires Windows PowerShell (Desktop edition).
Get-SSLCertificate -Url https://example.com
# Returns:
# Thumbprint Subject
# ---------- -------
# 4DA25A6D5EF62C5F95C7BD0A73EA3C177B36999D CN=www.example.org, O=Internet Corporation for Assigned Names and Numbers, L=Los Angeles, S=California, C=US
Get-SSLCertificate -Url https://example.com