PowerShell:检测转录本(transcription)是否处于活动状态

问题描述 投票:0回答:1

如何在PowerShell中检测转录(transcription)是否处于活动状态?

powershell detect transcription
1个回答
0
投票

也许下面的代码对你们中的一些人有用。享受吧!

选项 A:停止并重新启动转录

#
# Option A: Stop and restart transcript
#
try {
$OldTranscriptFullName = (Stop-Transcript).Path
Start-Transcript -LiteralPath $OldTranscriptFullName -Append -Force
} catch {
$OldTranscriptFullName = $null
}
if ($OldTranscriptFullName) {
Write-Host "Transcript is active: '$($OldTranscriptFullName)'"
} else {
Write-Host 'Transcript is not active.'
}

选项B:使用反射

#
# Option B: Use reflection
# Based on:
#   https://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs#L667
#   https://x.com/SeeminglyScienc/status/933461048329371648
#   https://github.com/dataplat/dbatools/issues/2722
#
function GetActiveTranscriptPaths {
[CmdletBinding()]
param()
end {
$flags = [System.Reflection.BindingFlags]'Instance, NonPublic'
$transcriptionData = $Host.Runspace.GetType().
GetProperty('TranscriptionData', $flags).
GetValue($Host.Runspace)
$transcripts = $transcriptionData.GetType().
GetProperty('Transcripts', $flags).
GetValue($transcriptionData)
if (-not $transcripts) {
return
}
$returnArray = @()
foreach ($transcript in $transcripts) {
$returnArray += $transcript.GetType().
GetProperty('Path', $flags).
GetValue($transcript)
}
return $returnArray
}
}
$ActiveTranscriptPaths = GetActiveTranscriptPaths
if ($ActiveTranscriptPaths) {
Write-host "Number of active transcripts: $($ActiveTranscriptPaths.Count)"
Write-Host "Last active transcript: '$($ActiveTranscriptPaths[-1])'"
} else {
Write-Host 'Transcript is not active.'
}
© www.soinside.com 2019 - 2024. All rights reserved.