You need to enable JavaScript to run this app. 如何修改PowerShell的Write-Host输出:去首尾单引号并加双引号
优惠活动
大模型
产品
解决方案
定价
更多

如何修改PowerShell的Write-Host输出:去首尾单引号并加双引号

解决PowerShell脚本输出格式问题

直接使用以下修改后的脚本即可实现你要的效果:

$Paths = Get-ChildItem -Path "C:\KBUPDATE2016" | Select-Object -ExpandProperty Name
$counter = 0
Write-Host "!!! Run below commands on Windows Server 2016 machines"
foreach($path in $Paths){
    $logfilename = "wusa" + $counter
    Write-Host "Start-Process -FilePath `"C:\Windows\System32\wusa.exe`" -ArgumentList 'C:\KBUPDATE2016\$path /quiet /norestart /Log:C:\KBUPDATE2016\$($logfilename).log' -Wait"
    $counter ++
}

关键改动说明:

  1. 移除首尾单引号:原脚本中Write-Host的内容被外层单引号包裹,这是输出首尾出现单引号的直接原因,删除这对单引号即可解决。
  2. 转义双引号实现路径包裹:在PowerShell的双引号字符串中,要输出双引号必须用**反引号()**转义,因此把C:\Windows\System32\wusa.exe处理为"C:\Windows\System32\wusa.exe"`的转义形式,确保输出时路径被双引号包裹。

修改后运行脚本,输出将完全符合你的期望。

内容的提问来源于stack exchange,提问作者Arbelac

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 06:02:12