question

JamesAFlores-3656 avatar image
0 Votes"
JamesAFlores-3656 asked JamesAFlores-3656 answered

New-TimeSpan : Cannot bind parameter

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

("Path`tSubject`tIssuer`tThumbprint`tFriendly Name`tValid From`tValid 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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

smfx avatar image
0 Votes"
smfx answered

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 {
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

smfx avatar image
0 Votes"
smfx answered smfx edited

Hey, @JamesAFlores-3656 . 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 {

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JamesAFlores-3656 avatar image
0 Votes"
JamesAFlores-3656 answered

Hi @smfx, 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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JamesAFlores-3656 avatar image
0 Votes"
JamesAFlores-3656 answered

Woo Hoo! And, there was much rejoicing! yayyyyy! That worked, thank you so much for your help @smfx!!

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.