Advertisement
stephanlinke

[PRTG] Read Windows Cert Store

Dec 3rd, 2015
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. param(
  2.     [string]$Thumb = "",
  3.     [string]$ComputerName = "",
  4.     [string]$UserName = '',
  5.     <# use single quotes for password character escaping #>
  6.     [string]$Password = '',
  7.     [string]$Location = "",
  8.     [switch]$GetCerts,
  9.     [switch]$Verbose
  10. )
  11. $elapsed = [System.Diagnostics.Stopwatch]::StartNew()
  12. # This will create the credential
  13. # object that is used to get the certificates
  14. #######################################
  15. function createCredentials{
  16.         # Generate Credentials Object first
  17.         $SecPasswd  = ConvertTo-SecureString $Password -AsPlainText -Force
  18.         $Credentials= New-Object System.Management.Automation.PSCredential ($Username, $secpasswd)
  19.         return $Credentials
  20. }
  21.  
  22. # This will read the certificates from the remote host
  23. ######################################################
  24. function retrieveCertificates{
  25.  
  26.     $Credentials = (createCredentials);
  27.  
  28.     try {
  29.         if($verbose) { Write-Host ("Searching for certificates with thumbprint {0} under {1} on {2}" -f $Thumb,$Location,$ComputerName) }
  30.         if((($env:COMPUTERNAME) -ne $ComputerName)){
  31.                     if($verbose) { Write-Host "Not querying localhost, invoking command..." }
  32.                     $Certificates = (Invoke-Command -ComputerName $ComputerName -ScriptBlock { param($Location); Get-ChildItem -Path "$($Location)" } -Credential $Credentials -ArgumentList @($Location))
  33.         }
  34.         else{ $Certificates = (Get-ChildItem -Path $Location ) }
  35.        
  36.         if($verbose) {
  37.             Write-Host ("Certificates loaded! Found {0} certificates." -f $Certificates.Count)
  38.             $Certificates | Select-Object Thumbprint,Subject,FriendlyName | ft | Out-Host -Paging;
  39.         }
  40.        
  41.         return $Certificates
  42.     }
  43.     catch {
  44.         $ErrorMessage = $_.Exception.Message
  45.         Write-Host "0:Error checking Remote - CA";
  46.         if($verbose) { Write-Host  $ErrorMessage; }
  47.     }
  48.    
  49. }
  50.    
  51. # This function will check the certificates and
  52. # search for either all of them or for one with a matching thumbprint
  53. ##############################
  54. function evaluateCertificates{
  55.     # Get the current date
  56.     $TimeStamp = (Get-Date)
  57.  
  58.     $Certificates = (retrieveCertificates);
  59.     if($GetCerts) { $Certificates | Select-Object Thumbprint,Subject,FriendlyName | ft | Out-Host -Paging; exit 0; }
  60.  
  61.     # Filter for a specific thumb, create output
  62.     $SpecificCertificate = ( $Certificates | Where-Object { $_.Thumbprint -Match $Thumb } )
  63.     if($verbose) { Write-Host ("Found {0} certificates that match. If multiple certificates are found, please specify the location!" -f $SpecificCertificate.Count); }
  64.  
  65.     $TimeSpan = $SpecificCertificate.NotAfter - $TimeStamp
  66.     Write-Host ("{0}:Cert with thumb {1} expires in {0} days" -f $TimeSpan.Days, $SpecificCertificate.Thumbprint)
  67.     if($verbose){  Write-Host "Execution time: $($elapsed.Elapsed.TotalSeconds.ToString()) seconds" }
  68.     exit 0;
  69. }
  70.  
  71. evaluateCertificates;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement