0

I have a PowerShell script that has really good console status and progress information (using Write-Host) when I run it from PowerShell ISE. However, the script will be normally called by a batch command file (.cmd) as part of a larger process.

Currently, I call Script1.ps1 from the Command1.cmd like this:

FOR /f "delims= USEBACKQ" %%i in (`powershell.exe -file "C:\Powershell\Script1.ps1" -_Parm1 "!_Parm1!" -_Parm2 "!_Parm2!"`) DO SET ResponseMessage=%%i 
FOR /f "tokens=1,2 delims=;" %%i in ("!ResponseMessage!") DO SET "ReturnCode=%%i" &SET "ResponseMessage=%%j"
IF !ReturnCode! NEQ 0 (
    @ECHO Error in Script1.ps1. ReturnCode=!ReturnCode!. ResponseMessage=!ResponseMessage!
    EXIT /B !ReturnCode!
)

It works perfectly, but I don't see any of the Write-Host status information from Script1.ps1, except the last one, which I parse into a return code and message.
I would like to see all the Write-Host status information in the command window as it is executing. I suspect Write-Host is not the right cmdlet for what I want to do. But, I haven't been able to get anything to work.

Here is a redacted sample of the Script1.ps1 console output, but it can process many files and hundreds of records within each. I don't need to extract any particular part of it, I would like to have it displayed step-by-step in its entirety.

:::Parms
 [Redacted] 
:::OpenSQL 
:::Backing up DB
 [Redacted] 
:::Processing Files
 [Redacted] 
  File Dev.Script1_Input3.xml 
  NameQuery: Dev.Script1_Input3
  Count: 15
  Updating... 
     10%... 
     20%... 
     30%... 
     40%... 
     50%... 
     60%... 
     70%... 
     80%... 
     90%... 
     100% !
  Recs Added....: 3 
  Recs Updated..: 12 
  Done. 

  All data files processed. 
  Files Processed: 3 
  [Redacted]
RobP
  • 1
  • 2
  • Your first loop walks through the output of `Script1.ps1` and assigns each line to the variable `ResponseMessage`, so after that command the variable holds the last line of the output. I do not get what your second `for` loop is intended for… – aschipfl May 28 '20 at 21:52
  • @aschipfl Sorry. I redacted the last line in an effort to save space. The last line would actually be something like "0;Script Done." the second loop just parses the return code from the message. That is all working fine. I am hoping to find a way to show all the prior "Write-Host" messages as Script1.ps1 is executing. – RobP May 28 '20 at 23:47

0 Answers0