Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- param(
- [string]$Thumb = "",
- [string]$ComputerName = "",
- [string]$UserName = '',
- <# use single quotes for password character escaping #>
- [string]$Password = '',
- [string]$Location = "",
- [switch]$GetCerts,
- [switch]$Verbose
- )
- $elapsed = [System.Diagnostics.Stopwatch]::StartNew()
- # This will create the credential
- # object that is used to get the certificates
- #######################################
- function createCredentials{
- # Generate Credentials Object first
- $SecPasswd = ConvertTo-SecureString $Password -AsPlainText -Force
- $Credentials= New-Object System.Management.Automation.PSCredential ($Username, $secpasswd)
- return $Credentials
- }
- # This will read the certificates from the remote host
- ######################################################
- function retrieveCertificates{
- $Credentials = (createCredentials);
- try {
- if($verbose) { Write-Host ("Searching for certificates with thumbprint {0} under {1} on {2}" -f $Thumb,$Location,$ComputerName) }
- if((($env:COMPUTERNAME) -ne $ComputerName)){
- if($verbose) { Write-Host "Not querying localhost, invoking command..." }
- $Certificates = (Invoke-Command -ComputerName $ComputerName -ScriptBlock { param($Location); Get-ChildItem -Path "$($Location)" } -Credential $Credentials -ArgumentList @($Location))
- }
- else{ $Certificates = (Get-ChildItem -Path $Location ) }
- if($verbose) {
- Write-Host ("Certificates loaded! Found {0} certificates." -f $Certificates.Count)
- $Certificates | Select-Object Thumbprint,Subject,FriendlyName | ft | Out-Host -Paging;
- }
- return $Certificates
- }
- catch {
- $ErrorMessage = $_.Exception.Message
- Write-Host "0:Error checking Remote - CA";
- if($verbose) { Write-Host $ErrorMessage; }
- }
- }
- # This function will check the certificates and
- # search for either all of them or for one with a matching thumbprint
- ##############################
- function evaluateCertificates{
- # Get the current date
- $TimeStamp = (Get-Date)
- $Certificates = (retrieveCertificates);
- if($GetCerts) { $Certificates | Select-Object Thumbprint,Subject,FriendlyName | ft | Out-Host -Paging; exit 0; }
- # Filter for a specific thumb, create output
- $SpecificCertificate = ( $Certificates | Where-Object { $_.Thumbprint -Match $Thumb } )
- if($verbose) { Write-Host ("Found {0} certificates that match. If multiple certificates are found, please specify the location!" -f $SpecificCertificate.Count); }
- $TimeSpan = $SpecificCertificate.NotAfter - $TimeStamp
- Write-Host ("{0}:Cert with thumb {1} expires in {0} days" -f $TimeSpan.Days, $SpecificCertificate.Thumbprint)
- if($verbose){ Write-Host "Execution time: $($elapsed.Elapsed.TotalSeconds.ToString()) seconds" }
- exit 0;
- }
- evaluateCertificates;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement