【发布时间】:2020-12-13 00:37:34
【问题描述】:
考虑以下代码:
Function ShowSave-Log {
Param ([Parameter(Mandatory=$true)][String] $text)
$PSDefaultParameterValues=@{'Out-File:Encoding' = 'utf8'}
$date=[string](Get-Date).ToString("yyyy/MM/dd HH:mm:ss")
Tee-Object -InputObject "$date $text" -FilePath $LOG_FILE -Append
#Write-Host "$date $text"
}
Function Is-Installed {
Param ([parameter(Mandatory=$true)][String] $app_name, [String] $app_version)
$apps = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*" |
Select-Object DisplayName, DisplayVersion
$apps += Get-ItemProperty -Path "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*" | Select-Object DisplayName, DisplayVersion
$apps = $apps.Where{$_.DisplayName -like "$app_name"}
if ($apps.Count -eq 0) {
ShowSave-Log "`'$app_name`' not found in the list of installed applications."
return $false
} else {
ShowSave-Log "`'$app_name`' is installed."
return $true
}
}
$LOG_FILE="$Env:TEMP\LOG.log"
if (Is-Installed "Notepad++ (64-bit x64)") {Write-Host "TRUE"}
我希望在 ShowSave-Log 函数中看到来自 Tee-Object 命令的消息,但是它从未在终端中显示。我猜这是因为它是从“if”语句中调用的。如何将 Tee-Object 输出到终端屏幕?它被保存到日志文件中。 BTW Write-Host 命令正确地将消息输出到终端。 我正在使用 PowerShell ISE、Visual Studio 代码和 PowerShell 终端。 PowerShell 5.1 版
【问题讨论】:
-
在我的测试中显示得很好。
-
您使用什么版本的 PowerShell 进行测试?
-
PSVersion 5.1.19041.610PSEdition DesktopPSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}BuildVersion 10.0.19041.610CLRVersion 4.0.30319.42000
标签: powershell console