mardi 17 avril 2018

Powershell script only looks at the first variable per file

I have been working on creating a powershell script that looks for lines that contain the "expiry-epoch" for certificate expiration compare the values to a date 30 days from now and then send the hostname. It looks for these lines through multiple files in 1 folder. The lines are listed like:

set shared certificate TEST1 expiry-epoch 1533121779

set shared certificate TEST2 expiry-epoch 1525700960

The problem is if a file has multiple lines containing expiry-epoch, and the first line has a certificate that expires in some date longer than 30 days, such as 6 months or 3 months, then I get no output. Below is my code, please let me know if there is something I am missing, since I am pretty much stumped.

$c = Get-Date (Get-Date).ToUniversalTime() -UFormat %s
$epochroundedtimes=[math]::Round($c)
$epochtimes=$epochroundedtimes + 2592000
Get-ChildItem -Path "C:\PA-CERT1" -File -Recurse | 
ForEach-Object {$epochtimes } {
$epochMatch = $_ | Select-String -pattern "expiry-epoch"
if($epochMatch){
$certexp = ([double]($_ | Select-String -pattern "expiry-epoch\         
(\d+)$").Matches.Groups[1].Value)
if($certexp -le $epochtimes)
{
$_.Name
}
}
}

Aucun commentaire:

Enregistrer un commentaire