首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >powershell重定向换行问题

powershell重定向换行问题

原创
作者头像
Windows技术交流
修改2025-10-29 16:40:23
修改2025-10-29 16:40:23
1240
举报
文章被收录于专栏:Windows技术交流Windows技术交流
代码语言:txt
复制
执行这个powershell生成的.bat内容用记事本打开是连成一行的,用notepad++打开又是显示4行的,如何调整代码让前者也显示4行

powershell:

@"
net start w32time 2>nul
w32tm /resync
w32tm /resync
w32tm /resync
"@ | Out-File -FilePath "C:\w32time.bat" -Encoding ASCII

【最终方案】

推荐:

代码语言:txt
复制
$lines = @(
    "net start w32time 2>nul",
    "w32tm /resync",
    "w32tm /resync", 
    "w32tm /resync"
)
$lines | Out-File -FilePath "C:\w32time.bat" -Encoding ASCII

备选:

代码语言:txt
复制
$content = @"
net start w32time 2>nul
w32tm /resync
w32tm /resync
w32tm /resync
"@

$content -replace "`n", "`r`n" | Out-File -FilePath "C:\w32time.bat" -Encoding ASCII -NoNewline
代码语言:txt
复制
设置可靠的腾讯云校时

reg add "HKLM\SYSTEM\CurrentControlSet\Services\W32Time\Config" /v UtilizeSslTimeData /t REG_DWORD /d 0 /f

cmd.exe /c 'sc.exe config w32time start= auto'

net stop w32time
net start w32time
w32tm /config /manualpeerlist:"time.windows.com,0x8 time.tencent.com,0xa ntp.tencent.com,0xa ntpupdate.tencentyun.com,0xa time1.tencentyun.com,0xa time2.tencentyun.com,0xa time3.tencentyun.com,0xa time4.tencentyun.com,0xa time5.tencentyun.com,0xa" /syncfromflags:manual /reliable:yes /update  
w32tm /query /configuration
w32tm /query /source
w32tm /query /peers
w32tm /resync /rediscover /nowait
start-sleep 2
w32tm /resync /rediscover /nowait
start-sleep 2
w32tm /resync /rediscover /nowait


((Get-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\Parameters").NtpServer -split " ") | ForEach-Object { ($_ -split ",")[0] }

$lines = @(
    "net start w32time 2>nul",
    "w32tm /resync",
    "w32tm /resync", 
    "w32tm /resync"
)
$lines | Out-File -FilePath "C:\w32time.bat" -Encoding ASCII
schtasks /create /tn "\w32tm_resync" /ru SYSTEM /rl highest /tr "cmd.exe /c C:\w32time.bat" /sc onstart /delay 0000:05 /f
schtasks /change /tn "\w32tm_resync" /st 00:00 /sd 2000/01/01

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档