我正在使用称为Logging的PowerShell模块。我使用其他Jobs来做一些工作,但不幸的是,我在其中写入的日志没有被Receive-Job命令接收。
function Get-Topic {
[CmdletBinding(DefaultParameterSetName = "Normal")]
[OutputType(ParameterSetName = "Normal")]
[OutputType(ParameterSetName = "AsJob")]
param (
[Parameter(Mandatory = $true, ParameterSetName = "Normal")]
[Parameter(Mandatory = $true, ParameterSetName = "AsJob")]
[string]
$ResourceGroupName,
[Parameter(Mandatory = $true, ParameterSetName = "Normal")]
[Parameter(Mandatory = $true, ParameterSetName = "AsJob")]
[string]
$Namespace,
[Parameter(Mandatory = $true, ParameterSetName = "Normal")]
[Parameter(Mandatory = $true, ParameterSetName = "AsJob")]
[string]
$Topic,
[Parameter(Mandatory = $true, ParameterSetName = "AsJob")]
[switch]
$AsJob
)
if ($AsJob) {
$PSBoundParameters.Remove('AsJob') | Out-Null
return Start-ThreadJob -ScriptBlock {
param(
[string]$myFunction,
[System.Collections.IDictionary]$argTable,
[System.Collections.Concurrent.ConcurrentDictionary[string, hashtable]] $loggingTargets
)
$loggingTargets.Keys | ForEach-Object { Add-LoggingTarget -Name $_ -Configuration $loggingTargets[$_] }
$cmd = [scriptblock]::Create($myFunction)
& $cmd @argTable
} -ArgumentList $MyInvocation.MyCommand.Definition, $PSBoundParameters, (Get-LoggingTarget)
}
$topicObj = Get-AzServiceBusTopic `
-ResourceGroupName $ResourceGroupName `
-Namespace $Namespace `
-Name $Topic
Write-Log -Message "Received topic $($topicObj.Name)" -Level INFO
return $topicObj
}
是否可以将输出重定向到父级Powershell会话?我看到Write-Host可以工作,但是带有Console目标的记录器却不能。有任何解决方法吗?
答案 0 :(得分:0)
解决方案如下:
return Start-ThreadJob -StreamingHost (Get-Host) -ScriptBlock {
param(
[string]$myFunction,
[System.Collections.IDictionary]$argTable,
[System.Collections.Concurrent.ConcurrentDictionary[string, hashtable]] $loggingTargets
)
$loggingTargets.Keys | ForEach-Object { Add-LoggingTarget -Name $_ -Configuration $loggingTargets[$_] }
$cmd = [scriptblock]::Create($myFunction)
& $cmd @argTable
} -ArgumentList $MyInvocation.MyCommand.Definition, $PSBoundParameters, (Get-LoggingTarget)
感谢上帝,cmdlet Start-ThreadJob允许这种功能,基本上-StreamingHost参数解决了该问题