Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell-编码UTF8_Powershell_Utf 8 - Fatal编程技术网

Powershell-编码UTF8

Powershell-编码UTF8,powershell,utf-8,Powershell,Utf 8,我有一个脚本,可以对backup.txt文件进行更改,并保存一个新的.txt文件,用于上传到Cisco Web界面。使用我的原始脚本,我将收到一个错误: 上载的文件无效 这是原始脚本: $name = Read-Host 'What is the SX20 name?' if ($name -notlike "SX20NAME[a-z , 0-9]*") { Read-Host 'Please begin naming conventions with "SX20NAME

我有一个脚本,可以对backup.txt文件进行更改,并保存一个新的.txt文件,用于上传到Cisco Web界面。使用我的原始脚本,我将收到一个错误:

上载的文件无效

这是原始脚本:

$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}
我将
-Encoding UTF8
添加到Out文件中,使其现在看起来像这样:
Out文件$env:USERPROFILE\Desktop\$name.txt-Encoding UTF8}
。现在接口将接受该文件,但我收到一条新的错误消息:

某些命令被拒绝:卷:“50”


“卷”恰好是备份文件的第一行。如何调整以解决此问题

问题在于,它将UTF-8用于BOM,因此它将其填充为零。如果您在十六进制编辑器中打开该文件,您可以看到它正在运行

我以前在Powershell中遇到过这种情况,不幸的是,修复程序不是很漂亮。给出了正确的公式

编辑:更适合您的代码,我认为这会起作用。注意:我使用
%
别名代替
ForEach对象
(它们是相同的)


无法在此处显示此内容(因为细节将丢失),但如果比较新旧文件,第一行有何不同?您很可能需要一个十六进制编辑器来比较两个文件的前几行。
-Encoding UTF-8
将BOM表粘贴在文件的开头。您的服务可能不喜欢这样。您可以尝试将其剥离或手动将字符串编码为UTF-8,并使用
-编码字节
将其写入文件。同一篇文章的版本较短。@Matt,您指的是
[IO.file]::WriteAllines($filename,$content)
?我基本上是在自学Powershell,所以我不确定如何实现它。
$content=(Get content C:\Users\SX20_Backup.txt)-替换'SX20NAME[a-z,0-9]*',$name-替换'SetAlias',$description$filename=“$env:USERPROFILE\Desktop\$name.txt”;[IO.File]::writeAllines($filename,$content)
。注释中的代码很糟糕。用新行替换分号@布莱克
-replace
用作数组运算符,可以链接。尝试设定时避免使用别名。尤其是当用户是新用户时。请看我上面的评论。我给出了一个相当简洁的ops代码版本和您建议的修复方案。起初我没有意识到
[IO.File]
正在替换
Out File
,所以我一直在努力将其融入脚本中。谢谢马特帮我整理台词。我刚刚做了一个测试,一切都正常。谢谢你们两位的帮助!
$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)
}