将实时 CMD 输出发送到函数
Send live CMD output to function
我正在尝试 运行 一个 PowerShell 脚本 运行s CMD 命令并将实时流发送回 PowerShell。
从流中返回的每一行,我都试图发送给一个函数;但是,它仅在 cmd 运行 的末尾而不是在它期间传递给函数。
重要的是,如果我将流传递给 'out-host',我可以看到结果。
你能帮忙吗?
function get-ValidateProgress
{
param ([Parameter(Mandatory,HelpMessage='Value Passed from ValidateLDS.exe',Position=0,ValueFromPipeline)]$ValidateLine)
IF($ValidateLine -like 'Proccessing Active Users record*' )
{
$Current=$ValidateLine -replace 'Proccessing Active Users record ','' -replace 'of.*','' -replace ' ',''
$TotalValue=$ValidateLine -replace "Proccessing Active Users record $Current of ",''
[INT]$Progress=(($Current/$TotalValue)*100)
Write-Progress -Id 1 -Activity ValidateDBLDS -Status 'Proccessing LDS User records' -PercentComplete $Progress -CurrentOperation 'Active Users'
IF($Current -eq $TotalValue){write-host 'Finished procccsing Active Users' -ForegroundColor Green}
}
ELSEIF($ValidateLine -like 'Proccessing Deleted Users record*' )
{
$Current=$ValidateLine -replace 'Proccessing Deleted Users record ','' -replace 'of.*','' -replace ' ',''
$TotalValue=$ValidateLine -replace "Proccessing Deleted Users record $Current of ",''
[INT]$Progress=(($Current/$TotalValue)*100)
Write-Progress -Id 1 -Activity ValidateDBLDS -Status 'Proccessing LDS User records' -PercentComplete $Progress -CurrentOperation 'Deleted Users'
IF($Current -eq $TotalValue){write-host 'Finished procccsing Deleted Users' -ForegroundColor Green}
}
}
Try{
$cmdOutput = cmd.exe /c "cd /d D:$Campus\Code\Utilities && ValidateDBLDS.exe /viewonly:false" 2>&1 | get-ValidateProgress
Write-Host "Successfully finished LDS Validation." -ForegroundColor Green
sleep -Seconds 2
}
Catch{
Write-host "An Error occured during validating LDS. See full expection below.`nExecption:"$Error[0].Exception"`nTargetObject:"$Error[0].TargetObject"`nInvocationInfo:"$Error[0].InvocationInfo -ForegroundColor Red
}
为了让您的函数通过管道接收 streaming 输入 - 即输出对象(在外部程序的情况下是 lines) 因为它们是由上游命令发出的 - 它必须使用process
块.
使用一个简化的例子:
function foo {
# A process block is executed for each pipeline input object.
# In the case of *external programs*, each *line* of output constitutes
# an input object.
process {
"[$_]"
}
}
cmd /c 'echo 1 & timeout 2 >NUL & echo 2 ' | foo
执行此操作将回显 [1 ]
在 2 秒睡眠 timeout.exe
调用启动之前,证明 cmd /c
输出已处理以流式传输的方式。
缺少 process
块的函数的处理方式就好像其主体包含在 end
块中一样,即在所有管道输入完成后 调用的块收到.
我正在尝试 运行 一个 PowerShell 脚本 运行s CMD 命令并将实时流发送回 PowerShell。
从流中返回的每一行,我都试图发送给一个函数;但是,它仅在 cmd 运行 的末尾而不是在它期间传递给函数。
重要的是,如果我将流传递给 'out-host',我可以看到结果。
你能帮忙吗?
function get-ValidateProgress
{
param ([Parameter(Mandatory,HelpMessage='Value Passed from ValidateLDS.exe',Position=0,ValueFromPipeline)]$ValidateLine)
IF($ValidateLine -like 'Proccessing Active Users record*' )
{
$Current=$ValidateLine -replace 'Proccessing Active Users record ','' -replace 'of.*','' -replace ' ',''
$TotalValue=$ValidateLine -replace "Proccessing Active Users record $Current of ",''
[INT]$Progress=(($Current/$TotalValue)*100)
Write-Progress -Id 1 -Activity ValidateDBLDS -Status 'Proccessing LDS User records' -PercentComplete $Progress -CurrentOperation 'Active Users'
IF($Current -eq $TotalValue){write-host 'Finished procccsing Active Users' -ForegroundColor Green}
}
ELSEIF($ValidateLine -like 'Proccessing Deleted Users record*' )
{
$Current=$ValidateLine -replace 'Proccessing Deleted Users record ','' -replace 'of.*','' -replace ' ',''
$TotalValue=$ValidateLine -replace "Proccessing Deleted Users record $Current of ",''
[INT]$Progress=(($Current/$TotalValue)*100)
Write-Progress -Id 1 -Activity ValidateDBLDS -Status 'Proccessing LDS User records' -PercentComplete $Progress -CurrentOperation 'Deleted Users'
IF($Current -eq $TotalValue){write-host 'Finished procccsing Deleted Users' -ForegroundColor Green}
}
}
Try{
$cmdOutput = cmd.exe /c "cd /d D:$Campus\Code\Utilities && ValidateDBLDS.exe /viewonly:false" 2>&1 | get-ValidateProgress
Write-Host "Successfully finished LDS Validation." -ForegroundColor Green
sleep -Seconds 2
}
Catch{
Write-host "An Error occured during validating LDS. See full expection below.`nExecption:"$Error[0].Exception"`nTargetObject:"$Error[0].TargetObject"`nInvocationInfo:"$Error[0].InvocationInfo -ForegroundColor Red
}
为了让您的函数通过管道接收 streaming 输入 - 即输出对象(在外部程序的情况下是 lines) 因为它们是由上游命令发出的 - 它必须使用process
块.
使用一个简化的例子:
function foo {
# A process block is executed for each pipeline input object.
# In the case of *external programs*, each *line* of output constitutes
# an input object.
process {
"[$_]"
}
}
cmd /c 'echo 1 & timeout 2 >NUL & echo 2 ' | foo
执行此操作将回显 [1 ]
在 2 秒睡眠 timeout.exe
调用启动之前,证明 cmd /c
输出已处理以流式传输的方式。
缺少 process
块的函数的处理方式就好像其主体包含在 end
块中一样,即在所有管道输入完成后 调用的块收到.