How to read powershell in a text file after getting files contents using powershell

149 Views Asked by At

I am trying to make a script that takes a list of file paths from a .txt file and check if the file is in the desired location, and if it isn't then send me an email. When I hardcode the file paths into the script to read from a variable it works. However when I read the file paths from a .txt file which contain date formatting it reads the filepath as if the formatting is a part of the path rather than script formatting to allow for a new date to be checked e.g. \\Serverlocation\foldername\Filename$(Get-Date -Format 'yyyyMMdd').csv

Below is the code I am using

$dateFormat = 'ddMMyyyy'
$1dayAgo = (Get-Date).AddDays(-1)
$FileList = '\\serverName\FileChecks\FileCheckList.txt'
#Example 1 of file with formatting \\Serverlocation\foldername\Filename$(Get-Date -Format 'yyyyMMdd').csv
#Example 2 of file with formatting \\linapp04\Applications\Data\LP_Holdings\LP_Holdings_$(Get-Date -Date $1dayAgo -Format $dateFormat).csv
$Checks = Get-content $FileList
$location = 'The following File has not been generated: '
$PSEmailServer

foreach ($Check in $Checks){
 if(Test-Path -Path $Check -PathType Leaf ){
    Write-Output "Truee"
    }
 else{
  "The following File has not been generated " + $Check
   Send-MailMessage -From '[email protected]' -To '[email protected]' -SmtpServer 'eu-smtp-outbound-1.mimecast.com'-port '25' -Subject "File Missing" -Body $location$Check
  }
}
   

Please Let me know if there is something i'm missing or if this is not possible in powershell

Thanks

1

There are 1 best solutions below

1
nimizen On BEST ANSWER

When reading fileCheckList.txt, each line is a string. So the path:

\\Serverlocation\foldername\Filename$(Get-Date -Format 'yyyyMMdd').csv

is a string, and:

$(Get-Date -Format 'yyyyMMdd')

will not be enumerated to a date. You would need to convert that string back into code, for example:

foreach($Check in $Checks)
{
    $fileDateSuffix = '$' + [system.io.path]::GetFileNameWithoutExtension(($check.Split('$',2)[1]))
    $scriptBlock = [scriptblock]::Create($fileDateSuffix)
    $fileDateSuffix = $(Invoke-Command -ScriptBlock  $scriptBlock)
    $fileDateSuffix
}

Perhaps instead, build the file path in the script. It's difficult to advise as I don't know for which file types you're searching. If they're all CSV files and follow the format of the example, then you could populate filechecklist.csv with filenames like:

\\Serverlocation\foldername\Filename

And then build the file path to search on in your script:

foreach($Check in $Checks)
{
    $filePath = $($check + $(get-date -Format 'yyyyMMdd')) + '.csv'
    if(Test-Path $filePath...

If the files in filechecklist.csv have different extensions, you could put a placeholder in the file path and replace it when the script executes. So for a file path of:

\\Serverlocation\foldername\Filename:date:.csv

foreach($Check in $Checks)
{
    $filePath = $($check).replace(':date:',$(Get-Date -Format yyyyMMdd))
    if(Test-Path $filePath...