How can I get the screen output of a cmdlet into a text file? I have tried multiple options for what I thought would be simple redirection and am stumped. I think this may be a newbie problem, but I could really use some feedback to point me in the right direction.
Here is an example of my script and commented out are the redirection methods I’ve tried with the vendor provided cmdlet “Backup-PortalStore” that is not logging the screen output.
I’ve also tried start-transcript and stop-transcript, wrapping up the commands into a function and still the only output captured into the log file in all of these attempts is:
Server : GRANTTEST-CF
Store : granttest
IsEnabled : True
PortalVersion :
FrameworkVersion :
MigrationVersion :
Status :
VirtualRoot :
What I want to capture in my log file is what I see when I run my script or cmdlet in a PS window which is:
Backup store ‘granttest’ to ‘\anthill\click\granttest-cf\backups\granttest-2020.10.28.14.17’
Backing up store database ‘granttest9’ to ‘\anthill\click\granttest-cf\backups\granttest-2020.10.28.14.17_incomplete\Database.bak’ with compression and copyOnly=False
10% completed
20% completed
30% completed
40% completed
50% completed
60% completed
70% completed
80% completed
90% completed
100% completed
Skip corpus backup on user request.
Backing up WebrCommon files.
Zipping path ‘C:\ClickPortal\DataServer\InetPub\Common\granttest’
ArchiveWebrCommon completed successfully
Backing up Webr runtime files.
Backing up Website files.
Backing up store configuration.
Writing backup details.
Saving backup details to store configuration
Enforcing maximum number of backups to keep 3
Found 3 old backups, Deleting 1 backup(s)
Deleting old backup \anthill\click\granttest-cf\backups\granttest-2020.10.28.13.55
Backup of store completed successfully
I suspect that Backup-PortalStore (or a cmdlet it depends on) is using Write-Host which is a bad practice. Write-Host just writes to the console so you can’t do anything with the output, like pipe it to another command or redirect it to a file.
I think what you’ll have to do is call your script and redirect the output of that, but then you won’t see it on the screen:
When I run the script and redirect on the ps cmdline, I get the same result.
PS C:\options\scripts> .\backup-test-v2.ps1 > .\test.txt
Backup store ‘granttest’ to ‘\anthill\click\granttest-cf\backups\granttest-2020.10.29.11.57’
Backing up store database ‘granttest9’ to ‘\anthill\click\granttest-cf\backups\granttest-2020.10.29.11.57_incomplete\Database.bak’ with compression and copyOnly=False
10% completed
20% completed
30% completed
40% completed
50% completed
60% completed
70% completed
80% completed
90% completed
100% completed
Skip corpus backup on user request.
Backing up WebrCommon files.
Zipping path ‘C:\ClickPortal\DataServer\InetPub\Common\granttest’
ArchiveWebrCommon completed successfully
Backing up Webr runtime files.
Backing up Website files.
Backing up store configuration.
Writing backup details.
Saving backup details to store configuration
Enforcing maximum number of backups to keep 3
Found 3 old backups, Deleting 1 backup(s)
Deleting old backup \anthill\click\granttest-cf\backups\granttest-2020.10.28.14.09
Backup of store completed successfully
PS C:\options\scripts> Get-Content .\test.txt
Server : GRANTTEST-CF
Store : granttest
IsEnabled : True
PortalVersion :
FrameworkVersion :
MigrationVersion :
Status :
VirtualRoot :
You may also want to have a look at Start-Transcript. In theory, anything going to StdOut also goes to the log file you define. Here is a what I use to create the log file in %TEMP% for the user running the script.
To redirect other streams just add the stream to your > operator i.e. .\script.ps1 6> script.log or .\script.ps1 *> script.log for all streams. See this doc for more info: about Redirection - PowerShell | Microsoft Docs
I’ve also tried start-transcript and stop-transcript, wrapping up the commands into a function and still the only output captured into the log file in all of these attempts is:
Server : GRANTTEST-CF
Store : granttest
IsEnabled : True
PortalVersion :
FrameworkVersion :
MigrationVersion :
Status :
VirtualRoot :
I’m struggling to duplicate this issue. Can you verify a couple of things? In your previous post it looks like you commented out the lines with #, so did the actually run? Can you try send the script output stream to a file instead of individual commands like this?
Can you please run it as powershell.exe .\backup-test-v2.ps1 > .\test.txt
I think you need to call PowerShell again as a new session is required to capture the output. That should at least prove if the cmdlet is using Write-Host or perhaps even Out-Default.
Thank you everyone for your feedback and thank you Matt. Running within PS as administrator and calling a new session, I am (thank you) getting the desired output to the text file. This confirms the cmdlet is using write-host?
I’ve updated the “payload” of my script to be:
<p style=“padding-left: 40px;”>powershell.exe Backup-PortalStore -Store $storename -Destination $storedest -NoCopyOnly -MaxFiles $maxbackup -NoCorpus *> $logdest"_$((Get-Date).ToString(‘yyyy.MM.dd_HH.mm.ss’))$hostname-backup.log"</p>
<hr />
Test Results
<p style=“padding-left: 40px;”>PS COMMAND: PS C:\options\scripts> powershell.exe .\backup-test-v2.ps1 > .\test.txt</p>
<p style=“padding-left: 40px;”>Here’s what’s in test.txt</p>
<p style=“padding-left: 80px;”>Backup store ‘granttest’ to ‘\anthill\click\granttest-cf\backups\granttest-2020.10.29.15.35’
Backing up store database ‘granttest9’ to ‘\anthill\click\granttest-cf\backups\granttest-2020.10.29.15.35_incomplete\Database.bak’ with compression and copyOnly=False
10% completed
20% completed
30% completed
40% completed
50% completed
60% completed
70% completed
80% completed
90% completed
100% completed
Skip corpus backup on user request.
Backing up WebrCommon files.
Zipping path ‘C:\ClickPortal\DataServer\InetPub\Common\granttest’
ArchiveWebrCommon completed successfully
Backing up Webr runtime files.
Backing up Website files.
Backing up store configuration.
Writing backup details.
Saving backup details to store configuration
Enforcing maximum number of backups to keep 3
Found 3 old backups, Deleting 1 backup(s)
Deleting old backup \anthill\click\granttest-cf\backups\granttest-2020.10.28.14.17
Backup of store completed successfully</p>
<p style=“padding-left: 80px;”>
Server : GRANTTEST-CF
Store : granttest
IsEnabled : True
PortalVersion :
FrameworkVersion :
MigrationVersion :
Status :
VirtualRoot :</p>
Awesome! Thank you both. For your feedback Laura, and for your solution Matt.
I just tested it, this time I could reproduce. I was then able to see that Matt’s solution is working great.
See below. The first Get-Content .\output.txt shows there is nothing in the output text file. The second time, the console output is redirected into the text file as we are creating a new session, as per Matt’s solution.
So yes, it does feel like Write-Host is being used in your custom Backup-PortalStore cmdlet.
I learned something new today (i.e. you can redirect - kind of - the output of Write-Host by creating a new session)
Glad that worked for you. I certainly think that points to the script using Write-Host.
I’d be inclined to log a ticket with the vendor about it. They may just give a response to say that you’re not supposed to script with their cmdlets, just use them, but even so there are well documented, better ways to provide output.