1

I have the below PowerShell script to check if a file is locked - how do I also output the 0 or 999 to a text file, for example C:\stuff\PSOutput.txt?

$file = "\\xxxxx\xxxxxx\xxxxxxx\test.log"
try {
    [IO.File]::OpenWrite($file).Close();
    exit 0
} catch {
    exit 999
}
$exit | Out-File -FilePath "C:\stuff\PSOutput.txt"
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Michael
  • 2,507
  • 8
  • 35
  • 71

4 Answers4

2

Don't exit the script. Save your exit value into a variable and write it to a file.

$file = "\xxxx\xxxxx\xxxxxxx\test.log"
try { [IO.File]::OpenWrite($file).close(); $exit = 0 } catch { $exit = 999}
$exit | Out-File -FilePath "C:\Path\to\myFile.txt"

exit stops your script and returns your exit value. To do more stuff with your script (like saving the value into a file), you should not use exit.

Sabuncu
  • 5,095
  • 5
  • 55
  • 89
TobyU
  • 3,718
  • 2
  • 21
  • 32
  • This doesn't work I'm afraid. It doesn't create the txt file. I then created a blank txt file and it doesn't write to it. – Michael Nov 29 '18 at 10:09
  • nothing - the script still executes. I have full read\write access to the folder in question. – Michael Nov 29 '18 at 10:53
  • Have a look at my second line of code and yours. It's different. Yours: `exit 0`, mine: `$exit = 0` and the same with `exit 999` compared to `$exit = 999` – TobyU Nov 29 '18 at 11:52
  • Still doesn't work I'm afraid. I'm calling this via a Process Task in SSIS. Its ok though, I'm just going to use the logging from within SSIS. Thanks for your help though. – Michael Nov 29 '18 at 12:42
0

You will have to edit the path for your file in first line of script:

$fileName = "test.txt"                                                           
$file = New-Object -TypeName System.IO.FileInfo -ArgumentList $fileName
[System.IO.FileStream] $fs = $file.OpenWrite(); 
if (!$?) {
"0" | Out-File "C:\PSOutput.txt"
}
else {
$fs.Dispose()
"999" | Out-File "C:\PSOutput.txt"
}
Ironwing
  • 121
  • 1
  • 15
  • This script forms part of a SSIS package - will the 0 or 999 still be fed back to SSIS in the above example? – Michael Nov 29 '18 at 11:06
  • I'm not that familiar with SSIS packages, if those are XML, and are already generated, you can try to Append the file, with: "0" Out-File -Encoding Ascii -append "C:\PSOutput.txt" But remember that Append adds to the end of the file. – Ironwing Nov 29 '18 at 11:15
0

This should work:

$file = "\\xxxx\xxxxx\xxxxxxx\test.log"
$exitCode = 0
if (Test-Path -Path $file -PathType Leaf) {
    $fileInfo = New-Object System.IO.FileInfo $file
    try {
        $stream = $fileInfo.Open([System.IO.FileMode]::Open, [System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::None)
        if ($stream) { $stream.Close() }
    } 
    catch {
        $exitCode = 999  # the file is locked by a process.
    }
}
$exitCode | Out-File -FilePath "C:\stuff\PSOutput.txt"
Theo
  • 57,719
  • 8
  • 24
  • 41
0

The exit statements in your code cause your script to exit before it gets to the point where $exit would be written to the output file. But even if it got there it wouldn't actually record the exit code, because the exit keyword doesn't magically fill the (undefined) variable $exit with the exit code. You may be confusing it with the automatic variable $LastExitCode, which gets updated on the caller side after the callee exited.

What you actually want to do is define a custom function that properly sets the exit code and does everything else you want to do before actually exiting:

function ExitWithCode($exitcode) {
    $host.SetShouldExit($exitcode)
    $exit | Out-File -FilePath 'C:\stuff\PSOutput.txt'
    exit $exitcode
}

then use it in your code like this:

try {
    [IO.File]::OpenWrite($file).Close()
    ExitWithCode 0
} catch {
    ExitWithCode 999
}

Using $host.SetShouldExit() in addition to exit ensures that your script always sets the proper exit code, regardless of how it's invoked.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328