Get-ChildItem, help to get right result that I expeded.

Welcome Forums General PowerShell Q&A Get-ChildItem, help to get right result that I expeded.

Viewing 1 reply thread
  • Author
    Posts
    • #210066
      Participant
      Topics: 1
      Replies: 0
      Points: 12
      Rank: Member

      Hello. I am a beginner of the powershell.

      I need to check the expiry date by powershell, So I googled it!

      Here’s the script that I copied from somewhere.

      $daysremain=30
      $certs = Get-ADComputer -Filter {Enabled -eq $True -and OperatingSystem -like “*Server*” -and Name -like “test*”} -SearchBase “OU=testgroup, OU=Server, OU=inhous, DC=test, DC=com” |% {Invoke-Command -ComputerName $_.name -ScriptBlock {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)| Select-Object PSComputerName, FriendlyName, NotAfter, Subject, Issuer}}}
      Write-Output $certs
      

      And then, I could not get the right result that I expected. Only I could see these objects

      “Thumbprint, Subject, PSComputerName”.

      I have no Idea what I have to do to figure it out.

      Can you help me??

      I’m sorry for not being fluent in English. If you don’t understand, please reply.

       

       

      • This topic was modified 2 months, 1 week ago by kvprasoon. Reason: code formatting
    • #210210
      Participant
      Topics: 5
      Replies: 321
      Points: 436
      Helping Hand
      Rank: Contributor

      If I am reading correctly, you are looking for certificates that expire in 30 days, correct?

      # Get list of servers
      $certs = Get-ADComputer -Filter {
      Enabled -eq $True -and OperatingSystem -like “*Server*” -and Name -like “test*”
      } -SearchBase “OU=testgroup, OU=Server, OU=inhous, DC=test, DC=com”
      
      # Get certs that expire in 30 days
      $result =
      Invoke-Command -ComputerName $certs.Name -ScriptBlock {
          Get-ChildItem cert:\LocalMachine\my -Recurse | 
          Where-Object {$_.NotAfter -eq (Get-Date).AddDays(30)} |
          Select-Object FriendlyName, NotAfter, Subject, Issuer
      }
      
      $result | Select-Object PSComputerName,FriendlyName,
      NotAfter,Subject,Issuer
      
Viewing 1 reply thread
  • You must be logged in to reply to this topic.