0
votes

I'm writing files with PowerShell to store specific information in it. I also want to create windows event log entries to check if the file which was "newly" created is really there.

New-EventLog -LogName System -Source "Files I store information in"
Write-EventLog -LogName System -Source "Files I store information in" -EntryType Information -EventId 1 -Message "Information written to file script started"
$FilePath = "C:\Path\Files"
command.exe -Out-File $FilePath\${env:computername}_$(get-date -f dd-MM-yyyy-hhmm).file

Basically I'm searching for a way to verify if the above command.exe created a file. I'm not sure how to do that when I'm using the "get-date" option to append this to the file name. If the file was created I want to create a successful event log entry. If it wasn't created I want to create a non successful event log entry.

Anyone a hint on this ?

1
I don't know how your command handles errors but that might be one way. You could for instance store the name of the file you're about to create in a variable first (such as $fileName = "$FilePath\${env:computername}_$(get-date -f dd-MM-yyyy-hhmm).file"), run your command with its output (command.exe -Out-File $fileName) and then check if the file exists using Test-Path -Path $fileName.notjustme

1 Answers

0
votes

Try catch, would work

try{
$FilePath = "C:\Path\Files"
command.exe -Out-File $FilePath\${env:computername}_$(get-date -f dd-MM-yyyy-hhmm).file
       
--write below successfull log entry
you also could add more checks like below
 if (test-path "$FilePath\${env:computername}_$(get-date -f dd-MM-yyyy-hhmm)"){
write here successfull log entry
}
else
{
 ---write here unsuccessfull log entry
}     
catch{
---write here unsuccessfull log entry
}