Powershell script: works when written by hand, doesn't work when generated. -
i have disk operation automate powershell script called test.ps1 looks this:
diskpart /s diskstuff.txt the file diskstuff.txt looks this:
list disk select disk 0 list partition when create diskstuff.txt hand using notepad.exe, , run this:
ps c:\> powershell.exe .\test.ps1 it runs fine, giving me information partitions. want 1 file deal with. tried script in test2.ps1:
$text1 = 'list disk' $text2 = 'select disk 0' $text3 = 'list partition' $text1 > 'diskstuff2.txt' $text2 >> 'diskstuff2.txt' $text3 >> 'diskstuff2.txt' diskpart /s diskstuff2.txt when ran script with:
ps c:\> powershell.exe .\test2.ps1 it didn't return useful diskpart. inspected diskstuff2.txt , diskstuff.txt in notepad.exe, , looked same.
i looked around 'net bit, , found better way inspect files. first dump of handcoded file (diskstuff.txt).
ps c:\> get-content diskstuff.txt -encoding byte the last few lines of result were
116 105 111 110 13 10 then dump of generated file (diskstuff2.txt)
ps c:\> get-content diskstuff.txt -encoding byte the last few lines of result were:
116 0 105 0 111 0 110 0 13 0 10 0 this showed script generate diskstuff2.txt somehow inserting zeros between every character. there way can script generate diskstuff2.txt without of zeros, can execute test2.ps1 intended?
redirection operators > , >> use utf-16 encoding files. if need different encoding, should use out-file or set-content/add-content cmdlets. have -encoding parameter specify encoding.
--------update jay----------- correct approach. final answer test2.ps1 was:
$text1 = 'list disk' $text2 = 'select partition 0' $text3 = 'list partition' set-content -path diskstuff2.txt -value $text1 -encoding utf8 add-content -path diskstuff2.txt -value $text2 -encoding utf8 add-content -path diskstuff2.txt -value $text3 -encoding utf8 diskpart /s diskstuff2.txt
Comments
Post a Comment