0

I have a script that makes changes to a backup .txt file and saves a new .txt that I can use to upload to Cisco Web interface. Using my original script, I would receive an error:

Invalid file uploaded.

This is the original script:

$name = Read-Host 'What is the SX20 name?'
if ($name -notlike "SX20NAME[a-z , 0-9]*")
    {
        Read-Host 'Please begin naming conventions with "SX20NAME".'
        }
        else {
        $description = Read-Host 'What is the SX20 alias?'
        (Get-Content C:\Users\SX20_Backup.txt)|

Foreach-Object {$_-replace 'SX20NAME[a-z , 0-9]*' , $name}|
Foreach-Object {$_-replace 'SetAlias' , $description}|

Out-file $env:USERPROFILE\Desktop\$name.txt}

I added -Encoding UTF8 to the Out-File so that it now looks like this: Out-file $env:USERPROFILE\Desktop\$name.txt -Encoding UTF8}. So now the interface will accept the file, but I get a new error message:

Some commands were rejected: Volume:"50"

"Volume" happens to be the very first line in the backup file. What can I adjust to resolve this issue?

Blake
  • 29
  • 6
  • Wont be able to show this here (as the detail would be lost) but if you compare both the old and new file how is the first line different? You most likely need a hex editor to compare the two files first few lines. – Matt Dec 30 '15 at 17:06
  • 1
    `-Encoding UTF-8` sticks a BOM on the start of the file. Your service might not like that. You can try stripping that off or encoding your string as UTF-8 manually and using `-Encoding Byte` to write it to the file. – Etan Reisner Dec 30 '15 at 17:06

1 Answers1

2

The problem is it is using UTF-8 with BOM, so it pads it with zeroes adds some extra characters at the beginning (most likely 0xFFFE). If you opened the file in a hex editor, you can see this in action.

I've run into this before with Powershell, and unfortunately the fix isn't very pretty. This answer gives the correct formula.

EDIT: More tailored to your code, I think this would work. Note I use the % alias in place of ForEach-Object (they are the same).

$name = Read-Host 'What is the SX20 name?'

if ($name -notlike "SX20NAME[a-z0-9]*") {
    Read-Host 'Please begin naming conventions with "SX20NAME".'
} else {
    $description = Read-Host 'What is the SX20 alias?'
    $newContent = (Get-Content C:\Users\SX20_Backup.txt) | %{ $_ -replace 'SX20NAME[a-z,0-9]*', $name } | %{$_ -replace 'SetAlias', $description}
    $filename = $env:USERPROFILE\Desktop\$name.txt
    [IO.File]::WriteAllLines($filename, $newContent)
}
Community
  • 1
  • 1
romellem
  • 3,144
  • 1
  • 22
  • 50
  • That same post has a shorter version. – Matt Dec 30 '15 at 17:08
  • @Matt, are you referring to `[IO.File]::WriteAllLines($filename, $content)` ? I'm basically teaching myself Powershell as I go, so I'm not sure how I would implement this. – Blake Dec 30 '15 at 17:30
  • `$content = (Get-Content C:\Users\SX20_Backup.txt) -replace 'SX20NAME[a-z , 0-9]*', $name -replace 'SetAlias', $description; $filename = "$env:USERPROFILE\Desktop\$name.txt"; [IO.File]::WriteAllLines($filename, $content)`. Code in comments sucks. Replace the semicolons with new lines. @Blake. `-replace` works as an array operator and can be chained. – Matt Dec 30 '15 at 17:40
  • Avoid using aliases when trying to teach. Especially when the user is new. Also look at my comment above. I give a pretty terse version of the ops code and your suggested fix. – Matt Dec 30 '15 at 18:00
  • I didn't realize at first that `[IO.File]` was replacing `Out-File`, so I was struggling with how it fit into the script. Thank you Matt for cleaning up my lines. I just ran a test and everything appears to be working properly. Thank you both for your help! – Blake Dec 30 '15 at 18:21
  • @Matt, I agree, but only if you don't explain what the aliases are. Learning the aliases is useful, so long as they know what the alias translates to. @Blake, run the [`Get-Alias`](https://technet.microsoft.com/en-us/library/ee176839.aspx) command from Powershell to see a list of all available aliases. – romellem Dec 30 '15 at 19:02
  • »UTF-8 with BOM, so it pads it with zeroes« ... uhm ... no? It prepends U+FEFF, nothing more. There are no zeroes whatsoever in there. – Joey Dec 30 '15 at 23:05
  • Huh, I always thought the zeroes padded around the bytes was the BOM. [But it looks like you're right](https://en.wikipedia.org/wiki/Byte_order_mark). Then these screenshots, [BOM](http://i.imgur.com/7Bx646S.png) vs [NO BOM](http://i.imgur.com/aKamC9H.png), is that because of UTF-8 vs UTF-16? I guess when I ran into this problem, I more noticed the zeroes than the first two `0xFFFE` characters – romellem Dec 31 '15 at 15:28