0

I am using the expdp command for backup and redirect the output into a text file. While this is in process, the console is blank. I would want to write a powershell script to display a status message as "In progress" until expdp command execution is completed. I tried using start-job command, however it fails to redirect the output into a text file as it is a background job. Any help here is appreciated. Below is the code used for start-job

$job = Start-Job -ScriptBlock {expdp username/password@database schemas="" directory="" dumpfile="" logfile="" REUSE_DUMPFILES=yes filesize=32G *> PROJECT.txt} -Name ExpdpJob

while ((Get-Job -Name ExpdpJob).State -eq "Running") 
{ 
    Write-Output "Expdp command is running"
}
Vikram Hosakote
  • 3,528
  • 12
  • 23
  • Why not `write-output "In Progress"`, then start the job, then `write-output "Job complete"` if the job succeeds? – Baodad Oct 17 '18 at 15:34

1 Answers1

0

To capture standard output you would have to use the receive-job command. I have tested the below code with PowerShell 5.1 and it works. It will not work in lower versions since the the information stream (#6) is not supported. The below snippet pretends that the expdp runs for 10 seconds and waits to display "running" message every 5 seconds till completion, then captures the output to file with Receive-Job and removes the job.

$job = Start-Job -ScriptBlock {Start-Sleep -Seconds 10; Write-Host "test";} -Name ExpdpJob

while ((Get-Job -Name ExpdpJob).State -eq "Running") 
{ 
    Write-Output "Expdp command is running"
    Start-Sleep -Seconds 5
}

Write-Output "Expdp command completed"
((Receive-Job $job) 6>&1) >> C:\temp\test123.txt

Remove-Job $job

Get-Content C:\temp\test123.txt