locked
exception calling ParseExact RRS feed

  • Question

  •  I'm trying to read my log file to detect if records have stopped populating with this script.

    Data in Log file:

    Files Processed on 06/12/2019 08:37:01

    $File="\\srv1\Sistemi1\sistem.log"
    
    $Line=Get-Content -Path $File | Select-Object -last 1
    
    $Date=$Line.replace("Files Processed on","").Substring(1,19)
    
    $Date
    
    $dtechecker = [datetime]::ParseExact($Date,"ddMMyyyy_HHmmss",$null)
    
    $check = (Get-Date).ToString('MM/dd/yyyy hh:mm:ss')
    
    $check
    
    
    if ($dtechecker -lt $check)
    {
        Write-Host ' Something Wrong with file Process -- Last File Received was-->' $Date   #.Substring(1,16)
    }
    Else
     {
      Write-Host 'Receiving Data'
      }
     I get the error:
    Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."

     Any help much appreciated.

     Thanks.


     

    Wednesday, June 12, 2019 12:44 PM

Answers

  • Then use a RegEx to extract the date.

     [datetime]($line -replace '.*(\d+/\d+/\d+\s+\d+:\d+:\d+).*','$1')


    \_(ツ)_/

    • Marked as answer by hart60 Wednesday, June 12, 2019 5:53 PM
    Wednesday, June 12, 2019 1:16 PM

All replies

  • $line='Files Processed on 06/12/2019 08:37:01'
    $date = [datetime]($line -replace 'Files Processed on ')
    $date
    You don't need to do all of that validation with computer generated datetime strings.  It is pointless.

    \_(ツ)_/


    • Edited by jrv Wednesday, June 12, 2019 1:01 PM
    Wednesday, June 12, 2019 1:00 PM
  •  There is Text after string that I want to exclude so I use the substring

    Data in Log file:

    Files Processed on 06/12/2019 08:37:01 E:\inv53321.csv


     Thanks.

    Wednesday, June 12, 2019 1:09 PM
  • Then use a RegEx to extract the date.

     [datetime]($line -replace '.*(\d+/\d+/\d+\s+\d+:\d+:\d+).*','$1')


    \_(ツ)_/

    • Marked as answer by hart60 Wednesday, June 12, 2019 5:53 PM
    Wednesday, June 12, 2019 1:16 PM
  •   

     This is what I finished with and is working. Do you see any issues?  Thanks again.

    $File="c:\sistem.log"
    
    $Line=Get-Content -Path $File | Select-Object -last 1
    
    $Date=[datetime]($line -replace '.*(\d+/\d+/\d+\s+\d+:\d+:\d+).*','$1')
    
    $lastprocess = $Date.ToString('MM/dd/yyyy hh:mm:ss')
    
    
    $check = (Get-Date).ToString('MM/dd/yyyy hh:mm:ss')
    
    
    $TimeDiff = New-TimeSpan $lastprocess $check
    
    
    
    if ($TimeDiff.Hours -lt 4)
    {
        Write-Host ' Something Wrong with file Process -- Last File Received was-->' $lastprocess   #.Substring(1,16)
    }
    Else
     {
      Write-Host 'Receiving Data'
      }

    Wednesday, June 12, 2019 5:53 PM
  • Why do you keep converting the date back and forth.  It is completely unnecessary.

    First we don't use caps on local variables. We use what is called "lower camelCase" or "pascal case".  This ks by long time convention.

    There is no need to make dates into strings.  It will eventually lead you to failures you can't understand.

    $file =  'c:\sistem.log'
    $line = Get-Content -Path $File | Select-Object -last 1
    $date = [datetime]($line -replace '.*(\d+/\d+/\d+\s+\d+:\d+:\d+).*', '$1')
    $timeDiff = New-TimeSpan $date [datetime]::Now
    if($timeDiff.Hours -lt 4){
        Write-Host ' Something Wrong with file Process -- Last File Received was-->' $lastprocess #.Substring(1,16)
    }else{
        Write-Host 'Receiving Data'
    }

    Take time to do the following links:

    PowerShell Style Guidelines

    Microsoft Virtual   Academy - Getting Started with Microsoft PowerShell


    \_(ツ)_/

    Wednesday, June 12, 2019 6:11 PM