The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

deisner (5) [Avatar] Offline
#1
I must be missing something basic. I have a simple script that calls an external command (subinacl):

[No indenting allowed in this forum?]
---snip---
$homeMapFile = "C:VOLADdataad_user_homes.csv"

$prefix = "M:"

Import-CSV -path $homeMapFile |

foreach-object {
$homeDir = $prefix + $_.HOME_DIR
& subinacl /file $homeDir /display=owner
}
---snip---

The script is saved in "set_permissions.ps1" Now I want to redirect all the output to a log file. I believe this should do what I want:

.set_permissions.ps1 > output.txt 2>&1

But what happens is this:

1. The output is still displayed to the console.
2. The output is also placed in output.txt, but with a space between every character, like so:

+ F i l e M : H O M E O F F I C E J O A N Y U A N

/ o w n e r = b u i l t i n a d m i n i s t r a t o r s


Hmm, upon further examination, the spaces are actually three null bytes. So the file really looks like ... 'F', '', '', '', 'i', '', ...

What's going on? Is this some type of encoding issue? I'd be happy with UTF-8 by default.

Thanks again.

-David
franck (17) [Avatar] Offline
#2
Re: Redirecting Output of Script
Hi David

I think that the best solution is the cmdlet 'out-file' with the option '-encoding'.

Perhaps:
$homeDir = $prefix + $_.HOME_DIR
& subinacl /file $homeDir /display=owner | out-file xxxx.txt -encoding ascii

In standard mode, Powershell works with unicode character set.

The firsts two/four bytes in file show you witch character set is used.

EF BB BF mean UTF-8
FE FF mean UTF-16 - big-endian
FF FE mean UTF-16 - low-endian
00 00 FE FF mean UTF-32 - big-endian etc

The standard by WinTel is : FF FE - UTF-16 - low-endian.

best Regards
Franck

P.S. If you write a bad encoding value, all the valides values will be appear in the error message ( | out-file xxxx.txt -encoding dummy)