Export Logs of all PS functions during operation.

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
chrissmallwood
Posts: 6
Last visit: Fri Jan 05, 2024 7:34 am

Export Logs of all PS functions during operation.

Post by chrissmallwood »

I'm sure that this has been asked before though my limited searching has not yielded anything useful.

I am looking to export to a log file all functions and operations that happen within the application.

I have tried using "transcripts" though that is very limited and will only sometimes net exceptions.

I'd like to see essentially a verbose log of either successes or failures.

How can I accomplish this?

EDIT: To be clear, I want the application to be live writing to an external .txt file while the application is running and operating.
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Export Logs of all PS functions during operation.

Post by davidc »

[TOPIC MOVED TO THE POWERSHELL FORUMS BY MODERATOR]

The product doesn't have a feature that does this, but you can probably accomplish this by creating a logging function and call it from all your functions or redirect the verbose stream to a file:
  1. 4>mylogfile.txt
David
SAPIEN Technologies, Inc.
User avatar
chrissmallwood
Posts: 6
Last visit: Fri Jan 05, 2024 7:34 am

Re: Export Logs of all PS functions during operation.

Post by chrissmallwood »

Thanks Davidc for the suggestion. Can you give me any insight or detail on how to accomplish your suggestion?
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Export Logs of all PS functions during operation.

Post by davidc »

I recommend reading the about_Redirect topic:

https://docs.microsoft.com/en-us/powers ... rshell-5.1
David
SAPIEN Technologies, Inc.
User avatar
chrissmallwood
Posts: 6
Last visit: Fri Jan 05, 2024 7:34 am

Re: Export Logs of all PS functions during operation.

Post by chrissmallwood »

Thanks David,

Unless I'm mistaken, wouldn't I need to append every line of code with the export to text file?

I tried using the PSLogging from (https://gallery.technet.microsoft.com/E ... 85#content) and while it works great in PowerShell and ISE it will not function within PSS.

Any suggestions?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Export Logs of all PS functions during operation.

Post by jvierra »

The easiest way to do this is to create a log function and place it throughout your code.

Code: Select all

function log{
      Param(
          [string]LogMsg,
          [string]$Type = 'Info',
      )
      $msg = '{0},{1},"{2}"' -f [datetime]::Now.ToString('s'), $Type, $LogMsg
      $msg | Out-File $FilePath -Append
}
#examples
log 'Copy a file'
# error message
log 'An Error occurred' -Type Error
Add any bells and whistles you need.
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Export Logs of all PS functions during operation.

Post by davidc »

Looks like you would want to do it at the script file level or write a function as James suggests.

Code: Select all

.\script.ps1 *> script.log
Although the PSLogging module is useful, it is using a hack (Reflection) to intercept the streams and doesn't support hosts other than that of the Console or ISE's internal host.

You could use the "Run in Console" commands to use the module.
David
SAPIEN Technologies, Inc.
This topic is 5 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked