-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_python_tee.ps1
More file actions
55 lines (48 loc) · 1.77 KB
/
Copy pathrun_python_tee.ps1
File metadata and controls
55 lines (48 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Executa script Python com saida em tempo real no console e append no log.
param(
[string]$PythonExe = 'python',
[Parameter(Mandatory = $true)][string]$WorkingDirectory,
[Parameter(Mandatory = $true)][string]$LogFile,
[Parameter(Mandatory = $true)][string]$Script,
[string]$ScriptArgLine = ''
)
$ErrorActionPreference = 'Continue'
Set-Location -LiteralPath $WorkingDirectory
$ScriptArgs = @()
if (-not [string]::IsNullOrWhiteSpace($ScriptArgLine)) {
$ScriptArgs = $ScriptArgLine.Split(' ', [System.StringSplitOptions]::RemoveEmptyEntries)
}
$escapedArgs = @('-u', $Script) + $ScriptArgs | ForEach-Object {
if ($_ -match '\s') { '"' + ($_ -replace '"', '\"') + '"' } else { $_ }
}
$psi = New-Object System.Diagnostics.ProcessStartInfo
$psi.FileName = $PythonExe
$psi.Arguments = ($escapedArgs -join ' ')
$psi.WorkingDirectory = $WorkingDirectory
$psi.UseShellExecute = $false
$psi.RedirectStandardOutput = $true
$psi.RedirectStandardError = $true
$psi.StandardOutputEncoding = [System.Text.UTF8Encoding]::new($false)
$psi.StandardErrorEncoding = [System.Text.UTF8Encoding]::new($false)
$psi.CreateNoWindow = $true
$proc = New-Object System.Diagnostics.Process
$proc.StartInfo = $psi
$emit = {
param([string]$Line)
if ([string]::IsNullOrEmpty($Line)) { return }
$stamp = Get-Date -Format 'HH:mm:ss'
$formatted = "[$stamp] $Line"
Write-Host $formatted
Add-Content -LiteralPath $LogFile -Value $formatted -Encoding utf8
}
$null = $proc.Start()
while (-not $proc.StandardOutput.EndOfStream) {
$line = $proc.StandardOutput.ReadLine()
if ($null -ne $line) { & $emit $line }
}
while (-not $proc.StandardError.EndOfStream) {
$line = $proc.StandardError.ReadLine()
if ($null -ne $line) { & $emit $line }
}
$proc.WaitForExit()
exit $proc.ExitCode