Besiktas
Besiktas

Reputation: 331

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.

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.

Upvotes: 0

Views: 120

Answers (1)

Olaf
Olaf

Reputation: 5252

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:

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/out-file?view=powershell-5.1

Upvotes: 1

Related Questions