我正在尝试使用模块
PSFramework中的
Write-Progress
将 Write-PSFMessage
的所有输出捕获到日志文件中,而无需重写所有当前脚本。
我已经成功地对
Write-Verbose
和Write-Host
使用了这个方法,但是当将下面的代码应用到Write-Progress
时,它会挂起...
function Write-MyProgress {
param (
[Parameter(Mandatory)]
[string]$Activity,
[string]$Status = 'Processing'
)
$params = @{
Activity = $Activity
Status = $Status
}
Write-Progress @params
$LogMessage = "PROGRESS: $Activity - $Status"
Write-PSFMessage -Level SomewhatVerbose -Message $LogMessage
}
Set-Alias Write-Progress -Value Write-MyProgress
通过模块限定名称或引用引用内置 cmdlet,以避免在别名生效后递归调用自己:
function Write-MyProgress {
param (
[Parameter(Mandatory)]
[string]$Activity,
[string]$Status = 'Processing'
)
$params = @{
Activity = $Activity
Status = $Status
}
# module-qualified command name
Microsoft.PowerShell.Utility\Write-Progress @params
$LogMessage = "PROGRESS: $Activity - $Status"
Write-PSFMessage -Level SomewhatVerbose -Message $LogMessage
}
此外,您可能希望使用
$PSBoundParameters
自动变量作为splatting 目标 - 这样您不仅可以传递绑定 -Activity
和 -Status
参数,还可以传递通用参数参数(-Debug
、-OutVariable
)
、-ProgressAction
等):
function Write-MyProgress {
param (
[Parameter(Mandatory)]
[string]$Activity,
[string]$Status = 'Processing'
)
# module-qualified command name
Microsoft.PowerShell.Utility\Write-Progress @PSBoundParameters
$LogMessage = "PROGRESS: $Activity - $Status"
Write-PSFMessage -Level SomewhatVerbose -Message $LogMessage
}