如何抑制PowerShell中Stop-Service的“等待服务停止”警告信息?
屏蔽PowerShell停止服务时的"等待服务停止"警告
有几种方法能屏蔽这类警告,按需选择即可:
给
Stop-Service加专用参数
直接在Stop-Service后加上-WarningAction SilentlyContinue参数,只针对当前停止服务的操作屏蔽警告,精准且不影响其他命令的输出:Get-Service *fewserviceswithmatchingprefixes* | Where-Object {$_.Name -notmatch 'somethinghere' -and $_.Name -ne 'alsosomethinghere' -and $_.Name -ne 'somethinghereprobably'} | Stop-Service -WarningAction SilentlyContinue临时修改全局警告偏好
执行命令前先设置$WarningPreference = 'SilentlyContinue',之后所有命令的警告都会被屏蔽,用完记得改回默认值$WarningPreference = 'Continue',适合需要连续执行多个屏蔽警告的操作场景:$WarningPreference = 'SilentlyContinue' Get-Service *fewserviceswithmatchingprefixes* | Where-Object {$_.Name -notmatch 'somethinghere' -and $_.Name -ne 'alsosomethinghere' -and $_.Name -ne 'somethinghereprobably'} | Stop-Service $WarningPreference = 'Continue'重定向警告流到空
PowerShell中警告属于第3号输出流,直接把这个流重定向到$null就能丢弃警告:Get-Service *fewserviceswithmatchingprefixes* | Where-Object {$_.Name -notmatch 'somethinghere' -and $_.Name -ne 'alsosomethinghere' -and $_.Name -ne 'somethinghereprobably'} | Stop-Service 3>$null
内容的提问来源于stack exchange,提问作者Kazic
相关产品推荐
相关产品推荐

