4

I have the below PowerShell script called via a SSIS Process Task to check if a file is locked - how do I modify so it checks if the file exists first.

  • If it does not exist, then exit with 999

  • If it does exist but is locked, then exit with 999

  • If it does exist and is not locked, then exit with 0

    $file = "\\xxxxxx\xxxx\xxxxx\xxxxxxxxx\task_status.log"    
    try { [IO.File]::OpenWrite($file).close();exit 0 } catch { exit 999}
    
Michael
  • 2,507
  • 8
  • 35
  • 71

1 Answers1

5
$file = "\\xxxxxx\xxxx\xxxxx\xxxxxxxxx\task_status.log"
if (Test-Path -path $file)
{ 
    try { [IO.File]::OpenWrite($file).close();return 0 } catch { return 999}
}
else
{
    return 999
}
Oggew
  • 366
  • 1
  • 9