前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >生成ANSI格式的.ps1

生成ANSI格式的.ps1

原创
作者头像
Windows技术交流
修改2024-04-12 17:56:25
960
修改2024-04-12 17:56:25
举报
文章被收录于专栏:Windows技术交流Windows技术交流

需求:生成ANSI格式的.ps1,实现检查开机的时候windows time服务是否启动状态,不是的话启动它。

代码语言:powershell
复制
if((get-service w32time).status -ne 'Running'){start-service w32time -EA 0}

powershell -command xxx

如果 Command 的值为脚本块,则脚本块必须用括号({})括起来

如果是在cmd中,则还需要代码块加双引号,例如"代码块"或者"& ({代码块})"

同样的代码在cmd和powershell中可能有不一样的效果

代码语言:powershell
复制
这句命令在powershell中可以,在cmd中不行
powershell -command ({if((get-service w32time).status -ne 'Running'){start-service w32time -EA 0}})

这句命令在powershell中可以,在cmd中也可以
powershell.exe -Command "if((get-service w32time).status -ne 'Running'){start-service w32time -EA 0}"

这句命令在powershell中可以,在cmd中也可以
powershell -command "& ({if((get-service w32time).status -ne 'Running'){start-service w32time -EA 0}})"

打算创建开机计划任务检查windows time服务是否Running状态,不是的话启动它

代码语言:powershell
复制
cmd.exe /c echo %date% %time%>c:\w32time.ps1
powershell.exe -NoProfile -Command "Add-Content -Path 'C:\w32time.ps1' -Value 'if((get-service w32time).status -ne ''Running''){start-service w32time -EA 0}'"
这样创建的文件是ANSI编码,但是第一行有日期时间,需要注释
代码语言:powershell
复制
#cmd.exe /c echo "#%date% %time%">c:\w32time.ps1
cmd.exe /c echo "#" "%date% %time%">c:\w32time.ps1
#注意>前后没有空格,有空格则达不到(生成的文件是ANSI编码)效果
powershell.exe -NoProfile -Command "Add-Content -Path 'C:\w32time.ps1' -Value 'if((get-service w32time).status -ne ''Running''){start-service w32time -EA 0}'"

#像上面2句创建的文件是ANSI编码,且第一行有日期时间信息
schtasks.exe /create /tn "w32time" /ru SYSTEM /rl highest /sc ONSTART /tr "powershell -ExecutionPolicy Unrestricted -windowstyle hidden -File 'C:\w32time.ps1'" /f

schtasks.exe /run /tn w32time

很多操作文本信息的命令会篡改文件编码,要特别注意,比如cmd.exe /c type nul>c:\w32time.ps1,本来用cmd.exe /c echo %date% %time%>c:\w32time.ps1创建的文件是ANSI编码,第一行有日期时间,需要注释或清空,结果使用cmd.exe /c type nul>c:\w32time.ps1后,ANSI变UTF-8了。于是用cmd.exe /c echo"#""%date% %time%">c:\w32time.ps1来规避,这样生成的文件第一行是#打头的日期时间,后面改内容的话,用Add-Content不会改变文件格式。

if((get-service w32time).status -ne'Running'){start-service w32time -EA 0}代码健壮性再提升下

代码语言:powershell
复制
while(1){
if((get-service w32time).status -ne ''Running''){start-service w32time -EA 0};
if((get-service w32time).status -eq ''Running''){exit};
}
代码语言:powershell
复制
cmd.exe /c echo "#" "%date% %time%">c:\w32time.ps1
powershell.exe -NoProfile -Command "Add-Content -Path 'C:\w32time.ps1' -Value 'while(1){if((get-service w32time).status -ne ''Running''){start-service w32time -EA 0};if((get-service w32time).status -eq ''Running''){exit};}'"
schtasks.exe /create /tn "w32time" /ru SYSTEM /rl highest /sc ONSTART /tr "powershell -ExecutionPolicy Unrestricted -windowstyle hidden -File 'C:\w32time.ps1'" /f
schtasks.exe /run /tn w32time

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

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

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

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

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

http://www.vxiaotou.com