powershell - Trusting certificate of HTTPS site in web request -


i want test if our web server (located on intranet) online (1) or offline (0) server containing using powershell 2.0. have script navigates page , check if html-string available on page.

function get-heartbeat {     $isalive = $false      try {         $webclient = new-object webclient          $username = "xxx"         $password = "xxx"         $domain = "xxx"          $url  = "oururl.com"         $html = '<input type="submit">'          $webclient.credentials = new-object system.net.networkcredential($username, $password, $domain)         $webpage = $webclient.downloadstring($url)          $isalive = $webpage.contains($html)     } catch {         # webexception thrown if status code 200 (ok)         write-host $_         $isalive = $false     }      return "$([int32]$isalive)" } 

unfortunately returns error:

exception calling "downloadstring" "1" argument(s): "the underlying connection closed: not establish trust relationship ssl/tls secure channel."

a way trust our certificate create type certificate policy follows (modification of this answer):

add-type @"     using system.net;     using system.security.cryptography.x509certificates;      public class trustourcertspolicy : icertificatepolicy {         public bool checkvalidationresult(             servicepoint servicepoint, x509certificate certificate,             webrequest request, int certificateproblem)          {             return certificate.issuer.equals("our issuer")                 && certificate.subject.contains("our application");         }     } "@  [system.net.servicepointmanager]::certificatepolicy = new-object trustourcertspolicy 

this still feels bit "stringly typed" instead of 100% secure.

is safe? there better way create webclient has 1 certificate accepted? certificate should trust available in cert:localmachine\my.

the correct way handle certificate errors import certificate chain (root , intermediat ca certificates) of webserver's certificate local certificate store (as trusted root ca , intermediate ca respectively). if server certificate self-signed need import server certificate trusted root ca.

another option, depending on need check, might modify algorithm. instance, if need heartbeat (not verify actual request returns specific result) try establish tcp connection port:

function get-heartbeat {   $url  = 'oururl.com'   $port = 443    $clnt = new-object net.sockets.tcpclient   try {     $clnt.connect($url, $port)     1   } catch {     0   } {     $clnt.dispose()   } } 

on more recent windows versions use test-netconnection cmdlet same end:

function get-heartbeat {   $url  = 'oururl.com'   $port = 443    [int](test-net-connection -computer $url -port $port -informationlevel quiet) } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -