Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Hughmoris
Apr 21, 2007
Let's go to the abyss!
Thanks for the ideas. I'm phone posting so forgive me.

What I need is an "on the fly" ad-hoc way of saying "I want to log to a custom CSV the command/script I'm about to execute". I don't have a need to log other stuff I run.

Maybe I can figure out a way to retroactively parse history for the info?

Adbot
ADBOT LOVES YOU

Happiness Commando
Feb 1, 2002
$$ joy at gunpoint $$

Are you capturing the output of that script, or just that you ran the script?

If the script outputs something, just set a variable to be the called script like so (the script just does get-date)
code:
PS C:\Users\HC> $foo = .\getdate.ps1
PS C:\Users\HC> $foo

Monday, April 15, 2024 1:57:37 PM


PS C:\Users\HC>
Or to log that you run the script, use the transcript (this script just does get-date but dumps it to a file):
code:
PS C:\Users\HC> Start-Transcript -Path .\log.txt
Transcript started, output file is .\log.txt
PS C:\Users\HC> .\getdate.ps1
PS C:\Users\HC> Stop-Transcript
Transcript stopped, output file is C:\Users\HC\log.txt
PS C:\Users\HC> cat .\date.txt

Monday, April 15, 2024 2:03:58 PM


PS C:\Users\HC> cat .\log.txt
**********************
Windows PowerShell transcript start
Start time: 20240415140352
Username: BEEFY\HC
RunAs User: BEEFY\HC
Configuration Name:
Machine: BEEFY (Microsoft Windows NT 10.0.22631.0)
Host Application: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Process ID: 36432
PSVersion: 5.1.22621.2506
PSEdition: Desktop
PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.22621.2506
BuildVersion: 10.0.22621.2506
CLRVersion: 4.0.30319.42000
WSManStackVersion: 3.0
PSRemotingProtocolVersion: 2.3
SerializationVersion: 1.1.0.1
**********************
Transcript started, output file is .\log.txt
PS C:\Users\HC> .\getdate.ps1
PS C:\Users\HC> Stop-Transcript
**********************
Windows PowerShell transcript end
End time: 20240415140401
**********************
PS C:\Users\HC>
I guess if you're really into the CSV, you could do some string manipulation on the transcript and wrap that all up in a custom function that you load as part of your profile

Happiness Commando fucked around with this message at 21:07 on Apr 15, 2024

Hughmoris
Apr 21, 2007
Let's go to the abyss!
Thanks for the idea! I learned something new with that Start-Transcript.

It's a weird short-term thing that isn't totally necessary, just something that can save me a bit of manual data entry. I'm mainly poking at it to see if I can automate it.

At this point I'm thinking I can add a comment (e.g. ##LogThis!) at the end of each relevant command execution, then write a simple script that parses History and finds those comments, writing those commands to a CSV along with their execution time.

Wizard of the Deep
Sep 25, 2005

Another productive workday
I'm still not seeing what exactly you're trying to accomplish. Do you need to submit some kind of report of when you do a specific task? In that case, I'd honestly just auto-start a transcript as part of your profile.ps1, then grab the relevant data whenever you need to submit the report. Combine that with a Posh theme that auto-timestamps your prompt and you're probably golden.

If you're trying to capture specific output from a command/script, adding | export-csv -NoClobber (or -Append) -NoTypeInformation -Path '$path\command-{$get-date}.csv' or similar code directly into your script is probably the right way to go.

Wizard of the Deep fucked around with this message at 09:24 on Apr 16, 2024

Pile Of Garbage
May 28, 2007



Hughmoris posted:

Thanks for the idea! I learned something new with that Start-Transcript.

It's a weird short-term thing that isn't totally necessary, just something that can save me a bit of manual data entry. I'm mainly poking at it to see if I can automate it.

At this point I'm thinking I can add a comment (e.g. ##LogThis!) at the end of each relevant command execution, then write a simple script that parses History and finds those comments, writing those commands to a CSV along with their execution time.

Is this potentially related to fulfilling some security thing (e.g. ACSC Essential Eight)? If so look at transcription/script block logging/module logging settings in Group Policy (Note you can enable these on a single machine via local Group Policy, aka gpedit.msc): https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_group_policy_settings?view=powershell-5.1

If you're looking to have a kind of running-log of your PowerShell session's execution history you could add a PSConsoleHostReadLine function to your Profile.ps1. There are various automatic variables that should allow you to pull out the relevant parts of what's about to be executed and write it to a file. Note that you can break your console with this if you do something like:

code:
function PSConsoleHostReadLine { $_ }
You could also maybe do the same thing by defining a custom prompt: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_prompts?view=powershell-5.1

Adbot
ADBOT LOVES YOU

Hughmoris
Apr 21, 2007
Let's go to the abyss!

Wizard of the Deep posted:

I'm still not seeing what exactly you're trying to accomplish. Do you need to submit some kind of report of when you do a specific task? In that case, I'd honestly just auto-start a transcript as part of your profile.ps1, then grab the relevant data whenever you need to submit the report. Combine that with a Posh theme that auto-timestamps your prompt and you're probably golden.

If you're trying to capture specific output from a command/script, adding | export-csv -NoClobber (or -Append) -NoTypeInformation -Path '$path\command-{$get-date}.csv' or similar code directly into your script is probably the right way to go.

Pile Of Garbage posted:

Is this potentially related to fulfilling some security thing (e.g. ACSC Essential Eight)? If so look at transcription/script block logging/module logging settings in Group Policy (Note you can enable these on a single machine via local Group Policy, aka gpedit.msc): https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_group_policy_settings?view=powershell-5.1

If you're looking to have a kind of running-log of your PowerShell session's execution history you could add a PSConsoleHostReadLine function to your Profile.ps1. There are various automatic variables that should allow you to pull out the relevant parts of what's about to be executed and write it to a file. Note that you can break your console with this if you do something like:

code:
function PSConsoleHostReadLine { $_ }
You could also maybe do the same thing by defining a custom prompt: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_prompts?view=powershell-5.1

Thanks for the ideas. I was attending an informal training session where certain actions needed to be logged for review. I ended up just keying in ,or copy/pasting, what was needed. I have learned more about the transcripts and profiles after reading up on your suggestions.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply