前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >powershell pipe output -encoding ascii 重定向输出.bat .cmd .vbs .txt文件时一定要注意编码是ascii

powershell pipe output -encoding ascii 重定向输出.bat .cmd .vbs .txt文件时一定要注意编码是ascii

原创
作者头像
Windows技术交流
修改2024-01-26 10:21:04
8380
修改2024-01-26 10:21:04
举报
文章被收录于专栏:Windows技术交流Windows技术交流

powershell 重定向输出字符串到.bat 、.cmd、 .vbs等文本性质的可执行文件时,一定要注意编码

powershell默认生成的文件的编码是UTF-16 LE BOM

而.bat 、.cmd、 .vbs的编码默认是ANSI

这样生成的.bat 、.cmd、 .vbs在cmd命令行执行会报错

参考https://til.secretgeek.net/powershell/out-file-encoding.html

注意加-encoding ascii

例如制作sysprep镜像、在执行sysprep命令之前需要执行下面的powershell代码,代码里输出重定向到.cmd文件要加 -encoding ascii

mkdir c:\windows\setup\scripts -force

del "C:\windows\setup\scripts\SetupComplete.cmd" 2>$null 1>$null

write-host > C:\windows\setup\scripts\SetupComplete.cmd

"sc.exe config cloudbase-init start= auto" | Out-File -Append C:\windows\setup\scripts\SetupComplete.cmd -encoding ascii

"sc.exe start cloudbase-init" | Out-File -Append C:\windows\setup\scripts\SetupComplete.cmd -encoding ascii

"net user administrator /active:yes" | Out-File -Append C:\windows\setup\scripts\SetupComplete.cmd -encoding ascii

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云服务器
云服务器(Cloud Virtual Machine,CVM)提供安全可靠的弹性计算服务。 您可以实时扩展或缩减计算资源,适应变化的业务需求,并只需按实际使用的资源计费。使用 CVM 可以极大降低您的软硬件采购成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档