Get-WindowsUpdateLog流重定向

时间:2019-09-12 10:27:59

标签: powershell

有人注意到Get-WindowsUpdateLog cmdlet无法重定向到任何流吗?

此外,将输出存储到变量中,对其进行管道传输或任何类型的重定向都会导致仅 执行该cmdlet。

任何帮助重定向/静音此命令输出的帮助。

我尝试过的事情:

Get-WindowsUpdateLog | Out-Null

Get-WindowsUpdateLog > $null

$sink = Get-WindowsUpdateLog

3 个答案:

答案 0 :(得分:2)

我发现的所有内容都无法抑制CmdLet Get-WindowsUpdateLog的输出。如您所说,控制台中显示的信息未正确遵循PowerShell中的输出流。

我发现的唯一解决方法是使用Jobs

$Job = Start-Job -ScriptBlock {Get-WindowsUpdateLog}
$Job | Wait-Job | Remove-Job

这样,所有输出都在作业内处理,我们不检索结果。也不必检索它,因为结果只是放在-Path参数中的文本文件。

答案 1 :(得分:0)

作为DarkLite答案的附录,我们还可以利用以下代码检查Get-WindowsUpdateLog cmdlet是否正常运行:

# Generate a unique filename.            
$o_filename = "C:\temp\" + "WindowsUpdateLog" + "_" + ( Get-Date -UFormat "%H_%M_%S" ) + ".txt"

$o_job = Start-Job -ScriptBlock { Get-WindowsUpdateLog -LogPath "$( $args[0] )" } `
                   -ArgumentList $o_filename -ErrorAction SilentlyContinue

Start-Sleep -Seconds 5

$o_job | Remove-Job -ErrorAction SilentlyContinue

if( ! ( Test-Path $o_filename ) ) {
#       Return an exception
}
else {
#       Do some work on the generated log file  
}

答案 2 :(得分:0)

我认为这里的课程是不要在脚本中使用Out-Default。 https://keithga.wordpress.com/2018/04/03/out-default-considered-harmful/在powershell 6中看不到该命令,但在powershell 7中却有此命令!