为什么这个Powershell函数返回的不仅仅是真/假?
问题描述:
Function lyncNotFocused {
# We need to check if the Lync window (conversation?) has focus or not.
$hwnd = [WhichWindowActive]::GetForegroundWindow()
$proc = [IntPtr]::Zero
[WhichProcessActive]::GetWindowThreadProcessId($hwnd, [ref] $proc);
Write-Host "DEBUG: Focused - hwnd $hwnd, process $proc"
if (Get-Process -ID $proc -ErrorAction "SilentlyContinue" | Where { $_ -notmatch "lync" }) { return $true }
else { return $false }
}
它返回类似“12412 True”或“9867 False”的内容。不要只是简单的布尔值。这些数字与我正在查找的PID相对应,看起来Get-Process cmdlet在这里放着床。我如何让它闭嘴?为什么这个Powershell函数返回的不仅仅是真/假?
答
听起来像你需要在[WhichProcessActive]
代码行上执行| out-null
。它可能会返回PID并将其设置在参数中。
所以,除非你的方法的返回值赋值给一个变量,或一方[void]
或Out-Null
丢弃的PowerShell将发送都记录下来的管道,它也将被退回。
谢谢@Andy ...我应该添加一些这样的解释。 – 2013-04-23 00:44:28