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

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.

Djimi
Jan 23, 2004

I like digital data
I've been tasked to get some daily orders from a web site. Because they won't send the orders to an email address without clicking a link (logging in) and downloading the file (good security), I am attempting to use PS and its New-WebServiceProxy to connect and request the previous days' sales and then combine it with a report with order details (concatenating/merging) as a csv file. I'm not worried about that, I'm doing that quite a bit already (thanks to you here that helped me a couple years ago).


The site has ColdFusion / SOAP and I can get "in" with credentials, within PS, but I don't know how to use the Web service proxy object and build commands that return results. Do I need to pass a big <Body>...</Body> blob, envelope.
Other (older) method is Invoke-WebRequest but trying to stick to NWS-Proxy.

I can get-members and methods, but I it's my lack of knowledge of building a SOAP request that I think is tripping me up.

I have only found a couple web sites that show simplistic namespace class (which apparently isn't necessary), examples - like calculator.
Thanks MS -> https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/new-webserviceproxy?view=powershell-5.1

Also, I'm not having luck trying things out in the SoapUI that I am using on a 14 day trial. I will admit, I haven't done much with xml/json API / GET / POST stuff in the past, and that's hindering me now.

The URI for the Orders is:
https://webservices.vin65.com/V301/OrderService.cfc?wsdl

If anybody could point me to a site (or a book) that shows SOAP w/ Powershell wrapping or something similar - thank you.

I'm surprised there's not more on this just searching Google for the last 30 minutes. Or if anyone could rough out a simple example on how I would construct a request to return all of the previous days orders from the complexType name="Order" namespace or SearchOrders schema...(check the url above) - that would be super helpful.

Thanks Goons, in advance.
:tipshat:

The Fool
Oct 16, 2003


I wouldn't use powershell for this, there are purpose built libraries in Python or C#

Aunt Beth
Feb 24, 2006

Baby, you're ready!
Grimey Drawer
That could also be a job for PHP but everyone hates PHP nowadays

The Fool
Oct 16, 2003


PHP is actually fine these days, but it took a hard reputation hit and never really recovered

Adbot
ADBOT LOVES YOU

Djimi
Jan 23, 2004

I like digital data

The Fool posted:

I wouldn't use powershell for this, there are purpose built libraries in Python or C#
Is it really that cumbersome in PowerShell?
I can do it in Python I suppose. Probably the most versatile solution.

Djimi fucked around with this message at 07:46 on May 19, 2024

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