A user asked if it was possible to see live output when running a PowerShell script as a script action. Another user suggested using a function to achieve this, while clarifying that ControlUp Script Actions do not stream output in real time. The first user mentioned seeing live output in other scripts and questioned if it was because they were forcing the output. It was confirmed by another user that this is not the case.
Read the entire ‘Real-Time Output for PowerShell Script Actions’ thread below:
I have a PowerShell script that is a script action, but when I run it I see no live output as its running. Once its finished, then I see the output. In the script I have tried Write-Host and Write-Output, is there another way so that I can see the live output?
yes there is, give me a few minutes and I will get what you need
can you send me the script that you are running? is it a machine or a session script
Session Script. here is the script.
===== Config =====
$diagExe = "C:\Program Files\ProfileUnity[Client.Net](http://Client.Net)\LwL.ProfileUnity.Client.Diagnostic.exe"
$outputShare = "\Servername\Share\LiquidwareDiag"
$logRoot = "C:\Windows\Temp\LiquidwareDiag"
$tempPath = $env:TEMP
$logRoot = Join-Path $tempPath "LiquidwareDiag"
$hostname = $env:COMPUTERNAME
$user = $env:USERNAME
Ensure log directory
if (!(Test-Path $logRoot)) {
New-Item -Path $logRoot -ItemType Directory -Force | Out-Null
}
$logFile = Join-Path $logRoot "$hostname-$user.log"
function Write-Log {
param([string]$Message)
$timestamp = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss")
try {
"$timestamp – $Message" | Out-File -FilePath $logFile -Append -Encoding utf8
} catch {
Write-Logt "LOGGING ERROR: $Message"
}
}
Write-Log "==== Starting Liquidware Diagnostic ===="
Write-Progress "Starting diagnostic for $user on $hostname"
Validate EXE
if (!(Test-Path $diagExe)) {
Write-Log "ERROR: Diagnostic EXE not found"
Write-Host "ERROR: Diagnostic EXE not found"
exit 1
}
$startTime = Get-Date
Write-Log "Start time: $startTime"
Run diagnostic
try {
Write-Log "Launching diagnostic…"
Write-Host "Launching diagnostic"
$proc = Start-Process -FilePath $diagExe -PassThru
Start-Sleep -Seconds 2
# Suppress popup
Get-Process | Where-Object {
$_.ProcessName -like "Diagnostic"
} | ForEach-Object {
try { $_.CloseMainWindow() | Out-Null } catch {}
}
# Don’t rely solely on WaitForExit
Write-Log "Process started, monitoring for output…"
} catch {
Write-Log "ERROR launching diagnostic: $_"
Write-Host "ERROR launching diagnostic"
exit 1
}
Find ZIP
Wait for ZIP file creation (up to 20 minutes)
$timeoutMinutes = 20
$pollIntervalSeconds = 10
Write-Log "Waiting for diagnostic ZIP to be created…"
Write-Host "Waiting for diagnostic to complete (this can take ~15 minutes)…"
$foundFiles = $null
$elapsed = 0
while ($elapsed -lt ($timeoutMinutes * 60)) {
$foundFiles = Get-ChildItem -Path $tempPath -Filter "LwL_ProfileUnity_Client_Diagnostic_*.zip" -ErrorAction SilentlyContinue |
Where-Object { $_.LastWriteTime -ge $startTime }
if ($foundFiles) {
Write-Log "ZIP file detected"
break
}
Write-Host "Still waiting… $([math]::Round($elapsed/60,1)) minutes elapsed"
Start-Sleep -Seconds $pollIntervalSeconds
$elapsed += $pollIntervalSeconds
}
if (!$foundFiles) {
Write-Log "ERROR: Timed out waiting for ZIP file"
Write-Host "ERROR: Timed out waiting for diagnostic ZIP"
exit 1
}
$newFiles = $foundFiles
Write-Output "Found $($newFiles.Count) diagnostic file(s)"
Write-Log "==== Completed ===="
Write-Host "Completed successfully"
ControlUp Script Actions do not stream stdout to the console in real time. Output is collected on the agent during execution and sent to the console only when the script completes. Switching between Write-Host and Write-Output will not change that.
you should be able to do this with a function though
I will test
@member
I know that we have other scripts that are updating during the run, is that only happening because we are forcing it to output
As far as I’m aware, Nazy is spot on.
Continue reading and comment on the thread ‘Real-Time Output for PowerShell Script Actions’. Not a member? Join Here!
Categories: All Archives, ControlUp Scripts & Triggers
