SOLVED

Monitor  SSL Certificates that will be expired soon and also provide an email notification

%3CLINGO-SUB%20id%3D%22lingo-sub-1923617%22%20slang%3D%22en-US%22%3EMonitor%26nbsp%3B%20SSL%20Certificates%20that%20will%20be%20expired%20soon%20and%20also%20provide%20an%20email%20notification%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1923617%22%20slang%3D%22en-US%22%3E%3CP%3EHello%2C%3C%2FP%3E%3CP%3EI%20have%20the%20following%20code%20in%20order%20to%26nbsp%3Bmonitor%26nbsp%3B%20SSL%20Certificates%20that%20will%20be%20expired%20soon%20and%20also%20provide%20an%20email%20notification%20at%20the%20end.%3C%2FP%3E%3CP%3ETo%20be%20clear%20i%20have%20found%20that%20code%20from%20this%20link%20%3CA%20href%3D%22https%3A%2F%2Fwww.msnoob.com%2Fpowershell-script-get-certificate-that-will-be-expired-soon.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehttps%3A%2F%2Fwww.msnoob.com%2Fpowershell-script-get-certificate-that-will-be-expired-soon.html%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3EAdd-PSSnapin%20Microsoft.Exchange.Management.Powershell.Admin%20-erroraction%20silentlyContinue%0A%0A%23%23%23%23%23%20Email%20Configuration%20Section%20%23%23%23%23%23%20%0A%0A%24SMTPName%20%3D%20%22%22%0A%24EmailMessage%20%3D%20new-object%20Net.Mail.MailMessage%0A%24SMTPServer%20%3D%20new-object%20Net.Mail.SmtpClient(%24SMTPName)%0A%24EmailMessage.From%20%3D%20%22alexandros.ananikidis%40sag-ag.ch%22%0A%24EmailMessage.To.Add(%22alexandros.ananikidis%40sag-ag.ch%22)%0A%0A%23%23%23%23%23%20Enter%20Serverr%20List%20%23%23%23%23%23%20%0A%24servername%3D%22https%3A%2F%2Fwww.google.com%22%0A%0A%23%23%23%23%23%20Enter%20the%20remaining%20date%20before%20certificate%20is%20expired%20%23%23%23%23%23%23%0A%24daysremain%3D30%0A%20%0A%20%0A%24certlist%3DInvoke-Command%20-ComputerName%20%24servername%20%7BGet-ChildItem%20Cert%3A%5CLocalMachine%5CMy%20-Recurse%20%7C%0A%20%20%20%20Where-Object%20%7B%24_%20-is%20%5BSystem.Security.Cryptography.X509Certificates.X509Certificate2%5D%20-and%20%24_.NotAfter%20-gt%20(Get-Date)%20-and%20%24_.NotAfter%20-lt%20(Get-Date).AddDays(%24daysremain)%7D%0A%20%20%20%20%7D%0A%0A%0Aif%20(%24certlist)%7B%0A%20%20%20%20%23%20Begin%20creation%20of%20the%20HTML%20for%20the%20email%0A%20%20%20%20%24body%20%3D%20%22%22%0A%20%20%20%20%24body%20%3D%20%24body%20%2B%20%22%3CSTYLE%3E%22%0A%20%20%20%20%24body%20%3D%20%24body%20%2B%20%22BODY%7Bbackground-color%3Awhite%3B%7D%22%20%0A%20%20%20%20%24body%20%3D%20%24body%20%2B%20%22TABLE%7Bborder-width%3A%201px%3Bborder-style%3A%20solid%3Bborder-color%3A%20black%3Bborder-collapse%3A%20collapse%3B%7D%22%20%0A%20%20%20%20%24body%20%3D%20%24body%20%2B%20%22TH%7Bborder-width%3A%201px%3Bpadding%3A%200px%3Bborder-style%3A%20solid%3Bborder-color%3A%20black%3Bbackground-color%3Agrey%7D%22%20%0A%20%20%20%20%24body%20%3D%20%24body%20%2B%20%22TD%7Bborder-width%3A%201px%3Bpadding%3A%204px%3Bborder-style%3A%20solid%3Bborder-color%3A%20black%3Bbackground-color%3Awhite%7D%22%20%0A%20%20%20%20%24body%20%3D%20%24body%20%2B%20%22td.green%7Bbackground-color%3A%20green%3B%20color%3A%20black%3B%7D%22%0A%20%20%20%20%24body%20%3D%20%24body%20%2B%20%22td.gray%7Bbackground-color%3A%20gray%3B%20color%3A%20black%3B%7D%22%0A%20%20%20%20%24body%20%3D%20%24body%20%2B%20%22td.silver%7Bbackground-color%3A%20silver%3B%20color%3A%20black%3B%7D%22%0A%20%20%20%20%24body%20%3D%20%24body%20%2B%20%22td.fsdata%7Bbackground-color%3A%20%2387AFC7%3B%20color%3A%20black%3B%7D%22%0A%20%20%20%20%24body%20%3D%20%24body%20%2B%20%22td.red%7Bbackground-color%3A%20red%3B%20color%3A%20black%3B%7D%22%0A%20%20%20%20%24body%20%3D%20%24body%20%2B%20%22H4%7Bbackground-color%3A%20Gold%3B%20color%3A%20black%3B%7D%22%0A%20%20%20%20%24body%20%3D%20%24body%20%2B%20%22H5%7Bcolor%3A%20gray%3B%7D%22%0A%20%20%20%20%24body%20%3D%20%24body%20%2B%20%22%3C%2FSTYLE%3E%22%0A%20%20%20%20%24body%20%3D%20%24body%20%2B%20%22%22%0A%20%20%20%20%24body%20%3D%20%24body%20%2B%20%22%22%0A%20%20%20%20%24body%20%3D%20%24body%20%2B%20%22%3CFONT%20size%3D%22%20%2B%20'%22%202%3D%22%22%3E%22%0A%20%20%20%20%24body%20%3D%20%24body%20%2B%20%22%3CH3%20align%3D%22%20%2B%20'%22%20center%3D%22%22%20id%3D%22toc-hId-1312525005%22%20id%3D%22toc-hId-1312525005%22%20id%3D%22toc-hId-1312525005%22%20id%3D%22toc-hId-1312525005%22%20id%3D%22toc-hId-1312525005%22%20id%3D%22toc-hId-1312525005%22%20id%3D%22toc-hId-1312525005%22%3EWarning%2C%20SSL%20Certificate(s)%20in%20server%20%24servername%20needs%20your%20attention%3C%2FH3%3E%22%0A%20%20%20%20%24body%20%3D%20%24body%20%2B%20%22%3C%2FFONT%3E%22%0A%0A%20%20%20%20foreach%20(%24certificate%20in%20%24certlist)%20%7B%0A%20%20%20%20%20%20%20%20%24body%20%3D%20%24body%20%2B%20%22%3CFONT%20align%3D%22%2B%20'%22%20left%3D%22%22%3ECertificate%20Issued%20To%20%3D%20%22%20%2B%20%20%24certificate.Issuer%20%2B%20%22%3C%2FFONT%3E%3CBR%20%2F%3E%22%0A%20%20%20%20%20%20%20%20%24body%20%3D%20%24body%20%2B%20%22%3CFONT%20align%3D%22%2B%20'%22%20left%3D%22%22%3EExpired%20Date%20%3D%20%22%20%2B%20%20%24certificate.NotAfter%20%2B%20%22%3C%2FFONT%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%22%0A%20%20%20%20%7D%0A%0A%20%20%20%20%24body%20%3D%20%24body%20%2B%20%22%22%0A%0A%0A%20%20%20%20%23%23%23%23%23%20Send%20The%20email%20with%20result%20%23%23%23%23%23%0A%20%20%20%20%24EmailMessage.Subject%20%3D%20%22%5BATTENTION%5D%20There%20is%20SSL%20Certificate(s)%20that%20need%20your%20attention%22%0A%20%20%20%20%24EmailMessage.Body%20%3D%20%24body%0A%20%20%20%20%24EmailMessage.IsBodyHTML%20%3D%20%24true%20%0A%20%20%20%20%24SMTPServer.Send(%24EmailMessage)%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENevertheless%2C%20when%20i%20try%20to%20run%20it%20i%20get%20the%20following%20error%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3EInvoke-Command%20%3A%20One%20or%20more%20computer%20names%20are%20not%20valid.%20If%20you%20are%20trying%20to%20pass%20a%20URI%2C%20use%20the%20-ConnectionUri%20parameter%2C%20or%20pass%20URI%20objects%20instead%20of%20strings.%0AAt%20line%3A18%20char%3A11%0A%2B%20%24certlist%3DInvoke-Command%20-ComputerName%20%24servername%20%7BGet-ChildItem%20Cer%20...%0A%2B%20%20%20%20%20%20%20%20%20%20%20~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%0A%20%20%20%20%2B%20CategoryInfo%20%20%20%20%20%20%20%20%20%20%3A%20InvalidArgument%3A%20(System.String%5B%5D%3AString%5B%5D)%20%5BInvoke-Command%5D%2C%20ArgumentException%0A%20%20%20%20%2B%20FullyQualifiedErrorId%20%3A%20PSSessionInvalidComputerName%2CMicrosoft.PowerShell.Commands.InvokeCommandCommand%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20also%20tried%20with%20the%26nbsp%3B%20-ConnectionUri%20parameter%20instead%20of%26nbsp%3B-ComputerName%20%26nbsp%3Bbut%20still%20it%20fails.%3C%2FP%3E%3CP%3EI%20am%20really%20new%20with%20all%20that%20PowerShell%20commands%20so%20can%20maybe%20someone%20help%20me%20on%20how%20to%20change%20my%20code%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20a%20lot%3C%2FP%3E%3CP%3EAlexandros%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1923617%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1924855%22%20slang%3D%22en-US%22%3ERe%3A%20Monitor%26nbsp%3B%20SSL%20Certificates%20that%20will%20be%20expired%20soon%20and%20also%20provide%20an%20email%20notification%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1924855%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F878526%22%20target%3D%22_blank%22%3E%40Alexandros8888%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHI%2C%26nbsp%3B%3C%2FP%3E%3CP%3Edid%20you%20try%20to%20set%20the%20%24Servername%20variable%20with%20the%20actual%20server%20name%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1924933%22%20slang%3D%22en-US%22%3ERe%3A%20Monitor%26nbsp%3B%20SSL%20Certificates%20that%20will%20be%20expired%20soon%20and%20also%20provide%20an%20email%20notification%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1924933%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F790105%22%20target%3D%22_blank%22%3E%40farismalaeb%3C%2FA%3E%2C%3C%2FP%3E%3CP%3EThank%20you%20for%20your%20reply.%20I%20also%20tried%20yes.%3C%2FP%3E%3CP%3EI%20tried%20like%20that%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3EAdd-PSSnapin%20Microsoft.Exchange.Management.Powershell.Admin%20-erroraction%20silentlyContinue%0A%0A%23%23%23%23%23%20Email%20Configuration%20Section%20%23%23%23%23%23%20%0A%0A%24SMTPName%20%3D%20%22%22%0A%24EmailMessage%20%3D%20new-object%20Net.Mail.MailMessage%0A%24SMTPServer%20%3D%20new-object%20Net.Mail.SmtpClient(%24SMTPName)%0A%24EmailMessage.From%20%3D%20%22alexandros.ananikidis%40sag-ag.ch%22%0A%24EmailMessage.To.Add(%22alexandros.ananikidis%40sag-ag.ch%22)%0A%0A%23%23%23%23%23%20Enter%20Serverr%20List%20%23%23%23%23%23%20%0A%0A%0A%23%23%23%23%23%20Enter%20the%20remaining%20date%20before%20certificate%20is%20expired%20%23%23%23%23%23%23%0A%24daysremain%3D30%0A%20%0A%20%0A%24certlist%3DInvoke-Command%20-ComputerName%20https%3A%2F%2Fwww.google.com%20%7BGet-ChildItem%20Cert%3A%5CLocalMachine%5CMy%20-Recurse%20%7C%0A%20%20%20%20Where-Object%20%7B%24_%20-is%20%5BSystem.Security.Cryptography.X509Certificates.X509Certificate2%5D%20-and%20%24_.NotAfter%20-gt%20(Get-Date)%20-and%20%24_.NotAfter%20-lt%20(Get-Date).AddDays(%24daysremain)%7D%0A%20%20%20%20%7D%0A%0A%0Aif%20(%24certlist)%7B%0A%20%20%20%20%23%20Begin%20creation%20of%20the%20HTML%20for%20the%20email%0A%20%20%20%20%24body%20%3D%20%22%22%0A%20%20%20%20%24body%20%3D%20%24body%20%2B%20%22%3CSTYLE%3E%22%0A%20%20%20%20%24body%20%3D%20%24body%20%2B%20%22BODY%7Bbackground-color%3Awhite%3B%7D%22%20%0A%20%20%20%20%24body%20%3D%20%24body%20%2B%20%22TABLE%7Bborder-width%3A%201px%3Bborder-style%3A%20solid%3Bborder-color%3A%20black%3Bborder-collapse%3A%20collapse%3B%7D%22%20%0A%20%20%20%20%24body%20%3D%20%24body%20%2B%20%22TH%7Bborder-width%3A%201px%3Bpadding%3A%200px%3Bborder-style%3A%20solid%3Bborder-color%3A%20black%3Bbackground-color%3Agrey%7D%22%20%0A%20%20%20%20%24body%20%3D%20%24body%20%2B%20%22TD%7Bborder-width%3A%201px%3Bpadding%3A%204px%3Bborder-style%3A%20solid%3Bborder-color%3A%20black%3Bbackground-color%3Awhite%7D%22%20%0A%20%20%20%20%24body%20%3D%20%24body%20%2B%20%22td.green%7Bbackground-color%3A%20green%3B%20color%3A%20black%3B%7D%22%0A%20%20%20%20%24body%20%3D%20%24body%20%2B%20%22td.gray%7Bbackground-color%3A%20gray%3B%20color%3A%20black%3B%7D%22%0A%20%20%20%20%24body%20%3D%20%24body%20%2B%20%22td.silver%7Bbackground-color%3A%20silver%3B%20color%3A%20black%3B%7D%22%0A%20%20%20%20%24body%20%3D%20%24body%20%2B%20%22td.fsdata%7Bbackground-color%3A%20%2387AFC7%3B%20color%3A%20black%3B%7D%22%0A%20%20%20%20%24body%20%3D%20%24body%20%2B%20%22td.red%7Bbackground-color%3A%20red%3B%20color%3A%20black%3B%7D%22%0A%20%20%20%20%24body%20%3D%20%24body%20%2B%20%22H4%7Bbackground-color%3A%20Gold%3B%20color%3A%20black%3B%7D%22%0A%20%20%20%20%24body%20%3D%20%24body%20%2B%20%22H5%7Bcolor%3A%20gray%3B%7D%22%0A%20%20%20%20%24body%20%3D%20%24body%20%2B%20%22%3C%2FSTYLE%3E%22%0A%20%20%20%20%24body%20%3D%20%24body%20%2B%20%22%22%0A%20%20%20%20%24body%20%3D%20%24body%20%2B%20%22%22%0A%20%20%20%20%24body%20%3D%20%24body%20%2B%20%22%3CFONT%20size%3D%22%20%2B%20'%22%202%3D%22%22%3E%22%0A%20%20%20%20%24body%20%3D%20%24body%20%2B%20%22%3CH3%20align%3D%22%20%2B%20'%22%20center%3D%22%22%20id%3D%22toc-hId--494929458%22%20id%3D%22toc-hId--494929458%22%20id%3D%22toc-hId--494929458%22%3EWarning%2C%20SSL%20Certificate(s)%20in%20server%20%24servername%20needs%20your%20attention%3C%2FH3%3E%22%0A%20%20%20%20%24body%20%3D%20%24body%20%2B%20%22%3C%2FFONT%3E%22%0A%0A%20%20%20%20foreach%20(%24certificate%20in%20%24certlist)%20%7B%0A%20%20%20%20%20%20%20%20%24body%20%3D%20%24body%20%2B%20%22%3CFONT%20align%3D%22%2B%20'%22%20left%3D%22%22%3ECertificate%20Issued%20To%20%3D%20%22%20%2B%20%20%24certificate.Issuer%20%2B%20%22%3C%2FFONT%3E%3CBR%20%2F%3E%22%0A%20%20%20%20%20%20%20%20%24body%20%3D%20%24body%20%2B%20%22%3CFONT%20align%3D%22%2B%20'%22%20left%3D%22%22%3EExpired%20Date%20%3D%20%22%20%2B%20%20%24certificate.NotAfter%20%2B%20%22%3C%2FFONT%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%22%0A%20%20%20%20%7D%0A%0A%20%20%20%20%24body%20%3D%20%24body%20%2B%20%22%22%0A%0A%0A%20%20%20%20%23%23%23%23%23%20Send%20The%20email%20with%20result%20%23%23%23%23%23%0A%20%20%20%20%24EmailMessage.Subject%20%3D%20%22%5BATTENTION%5D%20There%20is%20SSL%20Certificate(s)%20that%20need%20your%20attention%22%0A%20%20%20%20%24EmailMessage.Body%20%3D%20%24body%0A%20%20%20%20%24EmailMessage.IsBodyHTML%20%3D%20%24true%20%0A%20%20%20%20%24SMTPServer.Send(%24EmailMessage)%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%20But%20i%20get%20that%20error%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3EInvoke-Command%20%3A%20One%20or%20more%20computer%20names%20are%20not%20valid.%20If%20you%20are%20trying%20to%20pass%20a%20URI%2C%20use%20the%20-ConnectionUri%20parameter%2C%20or%20%0Apass%20URI%20objects%20instead%20of%20strings.%0AAt%20line%3A18%20char%3A11%0A%2B%20%24certlist%3DInvoke-Command%20-ComputerName%20https%3A%2F%2Fwww.google.com%20%7BGet-Ch%20...%0A%2B%20%20%20%20%20%20%20%20%20%20%20~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%0A%20%20%20%20%2B%20CategoryInfo%20%20%20%20%20%20%20%20%20%20%3A%20InvalidArgument%3A%20(System.String%5B%5D%3AString%5B%5D)%20%5BInvoke-Command%5D%2C%20ArgumentException%0A%20%20%20%20%2B%20FullyQualifiedErrorId%20%3A%20PSSessionInvalidComputerName%2CMicrosoft.PowerShell.Commands.InvokeCommandCommand%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EBest%20regards%2C%3C%2FP%3E%3CP%3EAlexandros%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1929356%22%20slang%3D%22en-US%22%3ERe%3A%20Monitor%26nbsp%3B%20SSL%20Certificates%20that%20will%20be%20expired%20soon%20and%20also%20provide%20an%20email%20notification%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1929356%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F878526%22%20target%3D%22_blank%22%3E%40Alexandros8888%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3Etry%20this%20small%20change%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24certlist%3DInvoke-Command%20-ComputerName%20%24servername%20-scriptblock%20%7BGet-ChildItem%20Cert%3A%5CLocalMachine%5CMy%20-Recurse%20%7C%20Where-Object%20%7B%24_.NotAfter%20-lt%20(Get-Date).AddDays(%24daysremain)%7D%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eand%20make%20sure%20that%20the%20server%20is%20the%20FQDN%20of%20the%20server%20or%20NETBIOS%20name%20such%20as%20%22mylocalserver%22%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

Hello,

I have the following code in order to monitor  SSL Certificates that will be expired soon and also provide an email notification at the end.

To be clear i have found that code from this link https://www.msnoob.com/powershell-script-get-certificate-that-will-be-expired-soon.html 

 

 

Add-PSSnapin Microsoft.Exchange.Management.Powershell.Admin -erroraction silentlyContinue

##### Email Configuration Section ##### 

$SMTPName = ""
$EmailMessage = new-object Net.Mail.MailMessage
$SMTPServer = new-object Net.Mail.SmtpClient($SMTPName)
$EmailMessage.From = "alexandros.ananikidis@sag-ag.ch"
$EmailMessage.To.Add("alexandros.ananikidis@sag-ag.ch")

##### Enter Serverr List ##### 
$servername="https://www.google.com"

##### Enter the remaining date before certificate is expired ######
$daysremain=30
 
 
$certlist=Invoke-Command -ComputerName $servername {Get-ChildItem Cert:\LocalMachine\My -Recurse |
    Where-Object {$_ -is [System.Security.Cryptography.X509Certificates.X509Certificate2] -and $_.NotAfter -gt (Get-Date) -and $_.NotAfter -lt (Get-Date).AddDays($daysremain)}
    }


if ($certlist){
    # Begin creation of the HTML for the email
    $body = "<head>"
    $body = $body + "<style>"
    $body = $body + "BODY{background-color:white;}" 
    $body = $body + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}" 
    $body = $body + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:grey}" 
    $body = $body + "TD{border-width: 1px;padding: 4px;border-style: solid;border-color: black;background-color:white}" 
    $body = $body + "td.green{background-color: green; color: black;}"
    $body = $body + "td.gray{background-color: gray; color: black;}"
    $body = $body + "td.silver{background-color: silver; color: black;}"
    $body = $body + "td.fsdata{background-color: #87AFC7; color: black;}"
    $body = $body + "td.red{background-color: red; color: black;}"
    $body = $body + "H4{background-color: Gold; color: black;}"
    $body = $body + "H5{color: gray;}"
    $body = $body + "</style>"
    $body = $body + "</head>"
    $body = $body + "<body>"
    $body = $body + "<font size=" + '"2"' + " face=" + '"arial black"' + ">"
    $body = $body + "<H3 align=" + '"center"' + ">Warning, SSL Certificate(s) in server $servername needs your attention</H3>"
    $body = $body + "</font>"

    foreach ($certificate in $certlist) {
        $body = $body + "<font align="+ '"left"' +">Certificate Issued To = " +  $certificate.Issuer + "</font><br />"
        $body = $body + "<font align="+ '"left"' +">Expired Date = " +  $certificate.NotAfter + "</font><br /><br />"
    }

    $body = $body + "</body>"


    ##### Send The email with result #####
    $EmailMessage.Subject = "[ATTENTION] There is SSL Certificate(s) that need your attention"
    $EmailMessage.Body = $body
    $EmailMessage.IsBodyHTML = $true 
    $SMTPServer.Send($EmailMessage)
}

 

 

Nevertheless, when i try to run it i get the following error:

 

 

Invoke-Command : One or more computer names are not valid. If you are trying to pass a URI, use the -ConnectionUri parameter, or pass URI objects instead of strings.
At line:18 char:11
+ $certlist=Invoke-Command -ComputerName $servername {Get-ChildItem Cer ...
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (System.String[]:String[]) [Invoke-Command], ArgumentException
    + FullyQualifiedErrorId : PSSessionInvalidComputerName,Microsoft.PowerShell.Commands.InvokeCommandCommand

 

 

I also tried with the  -ConnectionUri parameter instead of -ComputerName  but still it fails.

I am really new with all that PowerShell commands so can maybe someone help me on how to change my code?

 

Thank you a lot

Alexandros

6 Replies

@Alexandros8888 

HI, 

did you try to set the $Servername variable with the actual server name?

Hello @farismalaeb,

Thank you for your reply. I also tried yes.

I tried like that:

 

Add-PSSnapin Microsoft.Exchange.Management.Powershell.Admin -erroraction silentlyContinue

##### Email Configuration Section ##### 

$SMTPName = ""
$EmailMessage = new-object Net.Mail.MailMessage
$SMTPServer = new-object Net.Mail.SmtpClient($SMTPName)
$EmailMessage.From = "alexandros.ananikidis@sag-ag.ch"
$EmailMessage.To.Add("alexandros.ananikidis@sag-ag.ch")

##### Enter Serverr List ##### 


##### Enter the remaining date before certificate is expired ######
$daysremain=30
 
 
$certlist=Invoke-Command -ComputerName https://www.google.com {Get-ChildItem Cert:\LocalMachine\My -Recurse |
    Where-Object {$_ -is [System.Security.Cryptography.X509Certificates.X509Certificate2] -and $_.NotAfter -gt (Get-Date) -and $_.NotAfter -lt (Get-Date).AddDays($daysremain)}
    }


if ($certlist){
    # Begin creation of the HTML for the email
    $body = "<head>"
    $body = $body + "<style>"
    $body = $body + "BODY{background-color:white;}" 
    $body = $body + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}" 
    $body = $body + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:grey}" 
    $body = $body + "TD{border-width: 1px;padding: 4px;border-style: solid;border-color: black;background-color:white}" 
    $body = $body + "td.green{background-color: green; color: black;}"
    $body = $body + "td.gray{background-color: gray; color: black;}"
    $body = $body + "td.silver{background-color: silver; color: black;}"
    $body = $body + "td.fsdata{background-color: #87AFC7; color: black;}"
    $body = $body + "td.red{background-color: red; color: black;}"
    $body = $body + "H4{background-color: Gold; color: black;}"
    $body = $body + "H5{color: gray;}"
    $body = $body + "</style>"
    $body = $body + "</head>"
    $body = $body + "<body>"
    $body = $body + "<font size=" + '"2"' + " face=" + '"arial black"' + ">"
    $body = $body + "<H3 align=" + '"center"' + ">Warning, SSL Certificate(s) in server $servername needs your attention</H3>"
    $body = $body + "</font>"

    foreach ($certificate in $certlist) {
        $body = $body + "<font align="+ '"left"' +">Certificate Issued To = " +  $certificate.Issuer + "</font><br />"
        $body = $body + "<font align="+ '"left"' +">Expired Date = " +  $certificate.NotAfter + "</font><br /><br />"
    }

    $body = $body + "</body>"


    ##### Send The email with result #####
    $EmailMessage.Subject = "[ATTENTION] There is SSL Certificate(s) that need your attention"
    $EmailMessage.Body = $body
    $EmailMessage.IsBodyHTML = $true 
    $SMTPServer.Send($EmailMessage)
}

  But i get that error:

 

Invoke-Command : One or more computer names are not valid. If you are trying to pass a URI, use the -ConnectionUri parameter, or 
pass URI objects instead of strings.
At line:18 char:11
+ $certlist=Invoke-Command -ComputerName https://www.google.com {Get-Ch ...
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (System.String[]:String[]) [Invoke-Command], ArgumentException
    + FullyQualifiedErrorId : PSSessionInvalidComputerName,Microsoft.PowerShell.Commands.InvokeCommandCommand

Best regards,

Alexandros

@Alexandros8888 

try this small change

 

$certlist=Invoke-Command -ComputerName $servername -scriptblock {Get-ChildItem Cert:\LocalMachine\My -Recurse | Where-Object {$_.NotAfter -lt (Get-Date).AddDays($daysremain)}}

 

and make sure that the server is the FQDN of the server or NETBIOS name such as "mylocalserver"

Hello @farismalaeb,

 

Thank you for your  valuable and fast support.

 

I tried to create something even simpler that the code that i have found that works for me.

 

So i ended up with the following more simple code:

 

$Urls = @()
$Urls = "https://google.com",
"https://prod-caesar-service-app.apps.sag.services/"


$Result = @()


$MinimumCertAgeDays = 5000
$ErrorActionPreference= 'silentlycontinue'

Foreach ($url in $Urls)
{
[Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }

$req = [Net.HttpWebRequest]::Create($url)
$req.GetResponse() | Out-Null
#$req.ServicePoint.Certificate.GetExpirationDateString()

$ExpirationDate = $req.ServicePoint.Certificate.GetExpirationDateString()
$ExpDateToDT = [Datetime]::ParseExact($ExpirationDate, "dd/MM/yyyy hh:mm:ss", $null)
$DayCount = ( $( $ExpDateToDT ) - $( Get-Date ) ).Days

If ( $DayCount -le $MinimumCertAgeDays )
 {  
 
  
 $Result += "
   ---Certificate for Host: $url  Expires on ----->  $ExpirationDate
   ---Certificate for Host: $url  Expires in ----->  $DayCount DAYS" 
       

 }

}


Write-Output $Result


# Sender and Recipient Info
$MailFrom = "alexandros.ananikidis@sag-ag.ch"
$MailTo = "alexandros.ananikidis@sag-ag.ch"

# Sender Credentials
$Username = "alexandros.ananikidis@sag-ag.ch"
$Password = "Mypass"

# Server Info
$SmtpServer = "smtp-mail.outlook.com"
$SmtpPort = "587"

# Message stuff
$MessageSubject = "Live your best life now" 
$Message = New-Object System.Net.Mail.MailMessage $MailFrom,$MailTo
$Message.IsBodyHTML = $true
$Message.Subject = $MessageSubject
$Message.Body = $Result 

# Construct the SMTP client object, credentials, and send
$Smtp = New-Object Net.Mail.SmtpClient($SmtpServer,$SmtpPort)
$Smtp.EnableSsl = $true
$Smtp.Credentials = New-Object System.Net.NetworkCredential($Username,$Password)
$Smtp.Send($Message)

 It works fine.

 

The only issue that i need support now is how to make my email response to include some change of lines as the attached image show. Because it is really hard to read. 

 

Can you maybe help on that one?

 

Thank you a lot in advance,

Alexandros 

best response confirmed by Alexandros8888 (Occasional Contributor)
Solution

@Alexandros8888 

I hope I understand the last response correct but try to do this change in the $Result Variable

 

$Result += "
<br>Certificate for Host: $url Expires on -----> $ExpirationDate
<br>Certificate for Host: $url Expires in -----> $DayCount DAYS"

 

The output will be much better and each certificate will be in a separate line

 

@farismalaeb  Thank you very much for your help :)

 

Best regards,

Alexandros