Install LDAP /SSL certificates in single/multiple Servers (Powershell script)

This is a script which will be helpful to install SSL/LDAP certificate in single or multiple servers.

The verification is doing by comparing the certificate serial No.


       
#--Script for install and verify  LDAP certificates in web servers--#
#--Last modified 3/29/2018 12:38 AM EDT--#
#--Take the server list from the text file--#

$servers =  Get-Content -path "D:\Scripts\Approved Scripts\LDAP_Cert_ installation\servers.txt"

#--Loop for retrive server and install/verify LDAP Certificate--#
foreach ($server in $servers)
{
$Computername = $server
$pfx = new-object System.Security.Cryptography.X509Certificates.X509Certificate2


#--Get the s=certificate path from user imput--#
$certPath = read-host "Certificate Path"

#--Install the provided LDAP certificate--#
$pfx.Import($certPath)
$CertStore = New-Object System.Security.Cryptography.X509Certificates.X509Store  -ArgumentList  "\\$($server)\root", "LocalMachine"
$pfx_serialno=$pfx.SerialNumber
$CertStore.open(“MaxAllowed”)
$CertStore.add($pfx)
$CertStore.close()

#------Verification---------#
#-- This Section  checks  provided certificate serial no  is matching with the installed certificate certificate Serial no -------------#
$installed_cert_serialno = Invoke-Command -Computername $server  -Scriptblock {param ($pfx_serialno)
$value=(get-childitem -Path Cert:\LocalMachine\root | Where-Object {$_.SerialNumber -eq $pfx_serialno}).SerialNumber

Return $value } -ArgumentList $pfx_serialno

 if (!$installed_cert_serialno)
    {
        write-host ("The Certificate not installed successfully in $server") -ForegroundColor Red
    }
else 
    {
        write-host ("The Certificate installed successfully in $server") -ForegroundColor green
    }
write-host "The certificate serial no is $installed_cert_serialno " -ForegroundColor Cyan
 }

 #-- Delete the certificate frim the file location -- #
 #-- Added on 3/29/2018 --#
 Write-host -nonewline "Do you want to delete " $certPath  "? (Y/N) " -ForegroundColor Yellow
 $response = Read-Host 
          

     if ($response -ne "N")
         {
            Remove-Item $certPath |Where { ! $_.PSIsContainer }
            Write-host -nonewline $certPath "deleted Successfully" -ForegroundColor Green
        }
      
     else
        {
           Write-host -nonewline "File is not deleted from" $certPath -ForegroundColor red
        }
#-- End of Script-- #

Comments

Popular posts from this blog

Get AWS snapshot report into Excel (Powershell script)

Recycle IIS app pools in EC2 instance on application load balances (Powers-hell script)