Checking for certificate expiration with PowerShell (on multiple servers) RRS feed

  • Question

  • Hello,

     I am using a powershell script that I pulled a site that works really good. However, of course I need to adapt it to my needs and I am having a hard time doing so.

    The script is:

    $threshold = 30   #Number of days to look for expiring certificates 


    $deadline = (Get-Date).AddDays($threshold)   #Set deadline date 


    Invoke-Command -ComputerName TEST01, TEST02 SASMACWEBCTR01 { Dir Cert:\LocalMachine\My } | foreach { 

      If ($_.NotAfter -le $deadline) { $_ | Select Issuer, Subject, NotAfter, @{Label="Expires In (Days)";Expression={($_.NotAfter - (Get-Date)).Days}} } 

    As I said it works great..it displays

    I had to cut out the details but you get the picture. Since I am running this against several servers I would like to display the server name column in the results line just like the ones above so that I can correlate the server name to the certs. I suspect that the area I need to make the addition is the  Select Issuer, Subject, NotAfter, area of the script. I have tried many variations but have yet to hit on something to display the server name. Please keep in mind that I am at the beginner level of Powershell scripting (and learning more each day) when giving your answer. Thanks! -ACE

    • Moved by Bill_Stewart Thursday, January 25, 2018 9:51 PM Abandoned
    Monday, October 23, 2017 4:45 PM

All replies

  • Just add "PsComputerName to the select:

    $threshold = 30 #Number of days to look for expiring certificates 
    $deadline = [datetime]::Today.AddDays($threshold) #Set deadline date
    $computers = 'TEST01','TEST02','SASMACWEBCTR01'
    $sb = {Get-ChildItem Cert:\LocalMachine\My } 
    Invoke-Command -ScriptBlock $sb -ComputerName $computers|
    	Where-Object{$_.NotAfter -le $deadline}|
    	Select-Object PsComputerName, Issuer, Subject, NotAfter, @{n='Expires In (Days)';e={($_.NotAfter-[datetime]::Today).Days}}


    • Edited by jrv Monday, October 23, 2017 5:27 PM
    Monday, October 23, 2017 5:25 PM