Export Logs of all PS functions during operation.

Ask your Windows 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.
Locked
User avatar
chrissmallwood
Posts: 6
Joined: Wed Sep 27, 2017 12:59 pm

Export Logs of all PS functions during operation.

Post by chrissmallwood » Thu Aug 02, 2018 2:12 pm

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: 5475
Joined: Thu Aug 18, 2011 4:56 am

Re: Export Logs of all PS functions during operation.

Post by davidc » Fri Aug 03, 2018 7:03 am

[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
Joined: Wed Sep 27, 2017 12:59 pm

Re: Export Logs of all PS functions during operation.

Post by chrissmallwood » Wed Aug 08, 2018 3:33 pm

Thanks Davidc for the suggestion. Can you give me any insight or detail on how to accomplish your suggestion?

User avatar
davidc
Posts: 5475
Joined: Thu Aug 18, 2011 4:56 am

Re: Export Logs of all PS functions during operation.

Post by davidc » Wed Aug 08, 2018 3:47 pm

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
Joined: Wed Sep 27, 2017 12:59 pm

Re: Export Logs of all PS functions during operation.

Post by chrissmallwood » Tue Aug 14, 2018 2:11 pm

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?

User avatar
jvierra
Posts: 12899
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Export Logs of all PS functions during operation.

Post by jvierra » Tue Aug 14, 2018 2:31 pm

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: 5475
Joined: Thu Aug 18, 2011 4:56 am

Re: Export Logs of all PS functions during operation.

Post by davidc » Tue Aug 14, 2018 2:54 pm

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.

Locked