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?

2
  • 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. Commented Dec 30, 2015 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. Commented Dec 30, 2015 at 17:06

1 Answer 1

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)
}
Sign up to request clarification or add additional context in comments.

8 Comments

That same post has a shorter version.
@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.
$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.
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.
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!
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.