PowerShell logging stops when appending

I have a very weird issue. I wrote a script where I'm searching for additional email boxes in the registry. After I find that (just the names of the people), I want to add that to my log file so the person using the script can see it. However, when I try to append my finds to my log file, not only it doesn't add it but also stops logging the rest of the script.

Here are the things I have tried and the outcome is the same. Logging stops.

  • Getting the values from the registry as string append it to my log file

  • Get the values, convert it to string and append.

  • Get the values, export it to a text file, do a get-content at the beginning and the end of my script and append.
  • Get the values, export it to a text file, do Add-Content at the beginning and at the end of my script.

My sample script is below. I've changed it so many times trying different variations of it with the same result.

$key = "HKCU:\Software\Microsoft\Office\15.0\Outlook\Profiles\Mail\"
$val = "001f3001"

foreach ($subkey in $key) {
    try {
        $result = [System.Text.Encoding]::Unicode.GetString((gp $key)."001f3001") # tried changing this part to different variations to get the data
        if (($result -match "Microsoft Exchange*") -or ($result -match "Outlook Address Book")) {
            Out-Null
        } else {
            $result | Out-File -Append "C:\temp\r.txt" 
            # tried appending the result to my log file directly here with same result.
        }
    } catch {
        Out-Null
    }
}

"Additional email boxes: "
$mb = Get-Content "C:\temp\r.txt" # tried piping directly to the log file with same result. 
$mb | Out-File -Append $logfile -Force

My log file shows everything till I log append the result into it and stops.

.....log starts.....
.....
.....
.....
Additional email boxes: 
======= log files stop =====

my r.text file has all the values I'm looking for.

UPDATE: I noticed that if I save the file as ".log" I don't see anything. Any other extension .txt, .csv, etc. I can see the contents but still can't append it to my log file.

1 answer

  • answered 2018-01-12 00:33 Olaf

    If you don't explicitly provide the parameter name -FilePath you have to place the FilePath parameter to the first position. Please read the help for Out-File.