New-TimeSpan : Cannot bind parameter

James A Flores 21 Reputation points
2022-05-11T20:48:15.127+00:00

Greetings, thanks for any help debugging this script.

Here is the link where I found the script.

PowerShell: Create a Certificate Expiration Warning
https://www.incworx.com/blog/powershell-create-a-certificate-expiration-warning-system


Here is the whole script listed out.


Variable to store the location of the CSV report

$reportPath = "C:\Temp\CertificatesReport.CSV"

Create a tab delimited header row to label each property

("PathtSubjecttIssuertThumbprinttFriendly NametValid FromtValid To`tDays Until Expiration") | Out-File -FilePath $reportPath

Store today's date as a variable

$now = Get-Date

Iterate through each certificate and export the properties to the CSV report

Get-ChildItem Cert:\LocalMachine -Recurse | ForEach-Object {
$daysUntilExpiration = (New-TimeSpan -Start $now -End $.NotAfter).Days;
($
.PSPath + "t" +
$.Subject + "t" +
$
.Issuer + "t" +
$.Thumbprint + "t" +
$
.FriendlyName + "t" +
$.NotBefore + "t" +
$
.NotAfter + "t" +
$daysUntilExpiration) | Out-File -FilePath $reportPath -Append -NoClobber
}

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,362 questions
0 comments No comments
{count} votes

Accepted answer
  1. K. J. Skinner 181 Reputation points
    2022-05-12T02:42:17.733+00:00

    Ahh! Sorry I was using context that's supported by PowerShell Core. For Windows PowerShell, use Where-Object { -not $_.PSIsContainer } instead. PwSh 7 gave us some more shortcuts I was abusing:

     Get-ChildItem Cert:\LocalMachine -Recurse | Where-Object { -not $_.PSIsContainer } | ForEach-Object {
    
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. K. J. Skinner 181 Reputation points
    2022-05-11T21:42:26.233+00:00

    Hey, @James A Flores . You are probably getting errors where the "NotAfter" is null. This would be the case for any certificate folder and perhaps where some certs didn't specify an expiration (it'd have to be some dev system). So the easiest thing is to remove those entries that are 'folders' but making sure the property PSIsContainer is $false or at least -not $true.

    Just change the first line to

    Get-ChildItem Cert:\LocalMachine -Recurse | Where-Object -not PSIsContainer | ForEach-Object {  
    
    0 comments No comments

  2. James A Flores 21 Reputation points
    2022-05-11T23:15:47.697+00:00

    Hi @K. J. Skinner , thank you for responding to my issue, the change did not work, any other suggestions?

    I tried your suggestion and changed the first line to:
    Get-ChildItem Cert:\LocalMachine -Recurse | Where-Object -not PSIsContainer | ForEach-Object {

    And I got a new error:
    Where-Object : Parameter cannot be processed because the parameter name 'not' is ambiguous. Possible matches include: -NotLike -NotMatch -NotContains -NotIn.
    At line:12 char:58

    • Get-ChildItem Cert:\LocalMachine -Recurse | Where-Object -not PSIsCon ...
    • ~~~~
    • CategoryInfo : InvalidArgument: (:) [Where-Object], ParameterBindingException
    • FullyQualifiedErrorId : AmbiguousParameter,Microsoft.PowerShell.Commands.WhereObjectCommand

    I tried this line:
    Get-ChildItem Cert:\LocalMachine -Recurse | Where-Object -NotIn PSIsContainer | ForEach-Object {

    And got a different error:
    Where-Object : The specified operator requires both the -Property and -Value parameters. Provide values for both parameters, and then try the command again.
    At line:12 char:45

    • ... rt:\LocalMachine -Recurse | Where-Object -NotIn PSIsContainer | ForEa ...
    • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : InvalidArgument: (:) [Where-Object], PSArgumentException
    • FullyQualifiedErrorId : ValueNotSpecifiedForWhereObject,Microsoft.PowerShell.Commands.WhereObjectCommand
    0 comments No comments

  3. James A Flores 21 Reputation points
    2022-05-12T03:40:51.997+00:00

    Woo Hoo! And, there was much rejoicing! yayyyyy! That worked, thank you so much for your help @K. J. Skinner !!

    0 comments No comments