Reputation: 13
I have written some code that parses warranty information about a license from a website into a .txt file. My goal (and problem) is to compare the warranty expiry date from a .txt file with the current date, in order to know if warranty has expired.
I am a powershell beginner so my code might not be very logical, but this is what I have so far:
#the Invoke-WebRequest is up here which outputs into $output_file
#files
$output_file = ‘c:\warrantyinfo.txt’
$warranty_file = 'c:\warrantydate.txt'
#At this point all the warranty information is in $output_file
#With the code below I do a select string to get the information I want and
#get rid of everything else
$warrantyUntil = Get-Content $output_file
$warrantyUntil | Select-String '("toCustomerDate":)["]\d{4}\-(0?[1-
9]|1[012])\-(0?[1-9]|[12][0-9]|3[01])*' -AllMatches | Foreach-Object
{$_.Matches} | Select value > $warranty_file
#I now have "toCustomerDate":"yyyy-mm-dd in a new .txt file
#Below I try to grab just the date in the format yyyy-mm-dd in order to
#compare with todays date. I think this is where I go wrong.
$warrantyDate=Select-String -Path $warranty_file -Pattern "\d{4}\-(0?[1-
9]|1[012])\-(0?[1-9]|[12][0-9]|3[01])*" -AllMatches | Foreach-Object
{$_.Matches} | Select value
#Current date in the format I want
$currentDate=Get-Date -UFormat "%Y-%m-%d"
#Compare warrantyDate and currentDate to decide if warranty has expired or
#not
if ($currentDate -lt $warrantyDate) {
Write-Host "Warranty is valid"
} else {
Write-Host "Warranty has expired"
}
Upvotes: 1
Views: 1649
Reputation:
With a file warrantyinfo.txt
:
"toCustomerDate":"2018-04-22"
"toCustomerDate":"2018-04-24"
and this script:
$currentDate=Get-Date
$output_file = ‘.\warrantyinfo.txt’
$warrantyUntil = Get-Content $output_file |
Select-String '(?<="toCustomerDate":)"\d{4}\-(0?[1-9]|1[012])\-(0?[1-9]|[12][0-9]|3[01])"' -AllMatches
$warrantyUntil
$warrantyUntil.matches.value
$warrantyUntil.matches.value | ForEach {
if ($currentDate -lt [datetime]$_.trim('"')) {
Write-Host "Warranty $_ is valid"
} else {
Write-Host "Warranty $_ has expired"
}
}
You'll get this sample output:
"toCustomerDate":"2018-04-22"
"toCustomerDate":"2018-04-24"
"2018-04-22"
"2018-04-24"
Warranty "2018-04-22" has expired
Warranty "2018-04-24" is valid
The script uses a different RegEx with a positive LookBehind assertion for (?<="toCustomerDate":)
which isn't part of matched value.
Upvotes: 0
Reputation: 3236
This is happens because of data type mismatch.
First of all your $currentDate
is a string type, while you probably wanted it to be datetime
type, this is happening because of performed specific formatting. Also $warrantyDate
is string or even an array of strings. You need to take that in mind too.
You would want to explisitly set data type of initializtion variable by placing proper class acceleration before variable name. Like so:
[DateTime]$currentDate=Get-Date -UFormat "%Y-%m-%d"
[DateTime]$warrantyDate=Select-String -Path $warranty_file -Pattern "\d{4}\-(0?[1-
9]|1[012])\-(0?[1-9]|[12][0-9]|3[01])*" -AllMatches | Foreach-Object
{$_.Matches} | Select value
To see what is variable type use this:
$currentDate.GetType().Name
Upvotes: 1