Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Powershell have an automatic log, or do I have to write log functions to my scrips?

Tags:

powershell

I have mainly been writing syntax for statistics software such as SPSS, STATA and SAS, and I am fairly new to coding in PowerShell. I am used to beeing able to scan a log file for finding out what my script does, for example what values are assigned to a variable or which iteration of a loop is running.

But when I run my PowerShell script (using PowerShell ISE), I dont get any message at all unless there is a fatal error in the script. I get no errors, but the script doesn't execute the rigth action, so I know there is an error somewhere. When I write the code sentence by sentence to the console, I get outputs telling me what goes on, but once I run the enitre script, there is no output to keep track on. Is there a way to view a log from the script, or do I have to implicitly write log functions into the script? From my googling, this seems to be the case, but I would hate to spend hours writing log-code just to find that I could have just openede x folder and find what I was looking for

like image 482
Ullsokk Avatar asked Feb 03 '26 17:02

Ullsokk


2 Answers

in addition to @Vesper's reply, you can use start-transcript cmdlet to record each command and response to a logfile. ie

start-transcript -path c:\logs\logfile.txt

and set-psdebug could be a great help too

like image 55
Loïc MICHEL Avatar answered Feb 05 '26 06:02

Loïc MICHEL


start-transcript is mainly based on recording screen output (Write-Host) which is often questionable where it concerns variables with unexpected contents. Besides, there are no time stamps and other features you might expect from script logging.
Therefore I have written my own Log-Entry framework.

like image 45
iRon Avatar answered Feb 05 '26 07:02

iRon