title: How to do LDAP query using Powershell and PKI
body: <p>I'm new to Powershell, and I'm trying to do a secure LDAP query using PKI authentication. I'm getting stuck on how to set the certificate and key. Based on Googling/research, I have some of the basics, e.g.:</p>
  
  <pre><code>$connection = new-object System.DirectoryServices.Protocols.LDAPConnection('$domainName:$portNum')
  [string[] $get] = "$attribute1", "$attribute2", "attribute3"
  $request = new-object System.DirectoryServices.Protocol.SearchRequest("$targetOu", "$filter", "subtree", $get)
  $response = new-object $connection.SendRequest($request)
  </code></pre>
  
  <p>Like I said, I'm getting stuck on how to set/send the certificate and key. I thought I could do <code>$connection.ClientCertificates = $path</code>, but that property is read-only. I also thought I had to do something with <code>$System.Net.NetworkCredential</code>, but I'm not sure if the cert and key actually correspond to username and password. I referred to a Perl script that did an LDAP query and used PKI, and you could do: </p>
  
  <pre><code>clientcert =&gt; '/path/to/cert.pem'
  clientkey =&gt; '/path/to/key.pem'
  </code></pre>
  
  <p>What's the equivalent for Powershell? Do I have to do something with <code>System.Security.Cryptography.X509Certificates.X509Certificate</code>? </p>
  
  <p>Any help would be appreciated! </p>