Logging Powershell Job Results - powershell

I have created a powershell interface which essentially collects information and then sends that info to an external PS script via Start-Job. I have a timer that triggers recieve-job every second, and updates an output box with STDOUT from the job I created.
External PS Script:
This script has many functions (which utilize many cmdlets), and will be printing messages via Write-Verbose to a log. The issue is, sometimes in these functions I print messages and/or also return a value.
As we all know, anything that a function returns is placed into standard output. So anytime a function returns a value, that value is printed into the Output Rich Textbox control. I don't want that. Also, if I am using VERBOSE, the message is not being printed in the control (however, it is being printed to the console)
What I want:
Print only messages to my rich text box AND my log file, with no function return values in the stream (unless they are apart of a message itself).
Note: I am re-writing this application from a VB.NET app, to a Powershell Studio app... and I noticed previously this was being done by streaming STD Output into the text box, using a similar external powershell script--except that script was using WRITE-HOST. That doesn't make sense to me, as write-host isn't even supposed to be put into the stream.
Alternative Approach:
I had a working example of letting the Job add to the log itself, and then having the UI run Get-Content -tail 1... This WOULD WORK, except that some of my messages will be printed on more than one line, and it wouldn't pull everything as expected. If anyone has a way for me to get any X number of new lines in the log at all times, let me know. That might be easier than my current approach.

so basically you want to use powershell backwards? functions should only return a value that you want in your actual output stream, anything that should not be returning a value to the stream should either be captured in a variable or sent to Out-Null, if want to provide information on what the command is doing that is what write-verbose is for. and Write-Verbose is working as intended, just like write-host it is not put into the stream.
To answer your question what you should do is rewrite your functions so that they return a single value(or no value at all if that is appropriate), for functions that cannot be rewritten you can suppress their output in your script by either assigning them to a variable(if they must be used later) or simply piping them to Out-Null. anything that is currently being captured in the verbose stream that needs to be a part of your output should also be moved to the standard stream and written with Write-Output, potentially with `If statements wrapped around them if you only want that data on certain runs, however if you really want to combine the verbose stream with the standard stream you can check out the info here - Redirect two or more Powershell streams other than output stream to the same file

Related

Having Powershell Autofill command line prompt

There is an old command line tool my company uses to deploy log files to various servers.... whoever wrote it made it very very repetitive.
There is a lot of prompting that happens and I want to automate this process. We have a long term goal of replacing this .exe file down the line but for now automation works for the short term..
Example
./logdeploy.exe
Enter the destination folder:
I would like the powershell script to just automatically enter the folder, since its literally the same folder. because this exe is going to ask for it at least 20 times throughout this process, so copy paste just gets anyoing.
Is this even possible to do?
If there really is no way around simulating interactive user input in order to automate your external program, a solution is possible under the following assumption:
Your external program reads interactive responses from stdin (the standard input stream).
While doing so is typical, it's conceivable that a given program's security-sensitive prompts such as for passwords deliberately accept input from the terminal only, as so to expressly prevent automating responses.
If the first assumption holds, the specific method that must be used to send the response strings via stdin depends on whether the external program clears the keyboard buffer before each prompt.
(a) If it does not, you can simply send all strings in a single operation.
(b) If it does, you need to insert delays between sending the individual strings, so as to ensure that input is only sent when the external program is actively prompting for input.
This approach is inherently brittle, because in the absence of being able to detect when the external program is read to read a prompt response, you have to guess how much time needs to elapse between sending responses - and that time may vary based on many runtime conditions.
It's best to use longer delays for better reliability, which, however, results in increased runtime overall.
Implementation of (a):
As zett42 and Mathias R. Jessen suggest, use the following to send strings C:\foo and somepass 20 times to your external program's stdin stream:
('C:\foo', 'somepass') * 20 | ./logdeploy.exe
Again, this assumes that ./logdeploy.exe buffers keyboard input it receives before it puts up the next prompt.
Implementation of (b):
Note: The following works in PowerShell (Core) 7+ only, because only there is command output being sent to an external program properly streamed (sent line by line, as it becomes available); unfortunately, Windows PowerShell collects all output first.
# PowerShell 7+ only
# Adjust the Start-Sleep intervals as needed.
1..20 | ForEach-Object {
Start-Sleep 1
'C:\foo'
Start-Sleep 2
'somepass'
} | ./logdeploy.exe

Writing custom Powershell Cmdlet to accept table of data

I need to write a custom powershell cmdlet in C# that would accept data and be able to enumerate over it. I have followed some examples online and have some working code.
The problem I have can be demonstrated as follows:
I want a custom powershell cmdlet to take that output of another command as input to a parameter, so for example Get-Process
I want my custom cmdlet to accept data in table format
Enumerate over the data
The problem I have is that I am not sure how to setup a parameter to take a table of data in
Not sure how to output data from my cmdlet in table form.
Can anyone provide any examples?
I have no idea what you mean with "table format". In PowerShell you should work with objects (or, of course, lists of objects).
What you want to do, to create a cmdlet which takes the output of one command as it's input, i.e. accepts pipeline input, is to mark one of your parameter to take the value from the pipeline. You can find good instructions on how to do this at the MSDN section on Adding Parameters that Process Pipeline Input.

Powershell script formatting issue

When I run Get-Recipient | ft Name in the console everything looks good,
but when I try it in a script the output is just a list of class names;
Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
How do I fix this? Thankful for any and all help!
//David
The Format-* cmdlets should only be used to control display of data. They should rarely if ever be used inside "library" type scripts or functions, expect maybe when displaying status messages or something. The actual output of a Format-* cmdlet is a bunch of magic objects (the FormatEntryData guys) that direct the Powershell engine on how to do display formatting.
It works in the console because you are not capturing your data, you are just letting it display to the screen.
In your script, you should just return the data as-is, and let the caller decide how to format it, if (s)he wants to. If you want to return back only the Name field, use Select-Object Name to pare away the other fields.

Powershell - MS Exchange E-mail Autoresponder

We've currently got an issue where we're receiving a lot of bounced e-mails (from an auto generated e-mail) back from people where a specified e-mail address is not valid (failure notice). I need to identify certain messages in the mailbox and respond automatically to them - as a newbie to Powershell I'm struggling a bit! I think I understand how to check for the occurrence of a string but I don't know how to iterate through an inbox to look at/get a handle on each message in turn and I don't know how to extract the subject or body text in order to analyse the contents and perform a string comparison. I fear this should be easy - but I can't find anything on the web that might do the job - can anyone help?
So just to clarify what you're looking for.
Mailbox A receives a large number of failure notice/bounce messages.
You'ld like your powershell script to search Mailbox A for every instance where the Subject line (or message body) contains "String X" and if there is a match, take some action?
Also, what version of Exchange are you using? You need to be at least on 2007 to use Exchange Command Shell. You'll then want to look over the Command Shell commands that can be run.
Look at the Exchange Message Tracking Log, and Pipe the results from one command you run to the next. Think of it like this...
(Run a command) | (Run another command on the results of the first command) | (Run a last command on the results of the second).
You can view an example on my website at:
http://www.technoctopus.com/?p=223
While not exactly the same, it might get you moving in the right direction.

Pipe multiple tagged messages to program from within mutt

I understand how to pipe the contents of a single message through |. Also I understand how to tag messages in multiple different ways, as described in the help accessible through ?.
What I don't know:
How can you pipe the contents of a list of tagged messages to a program?
Is there a way to pipe a single message to a single program call?
Is there also a way to pipe the concatenated contents of all messages to a single program call?
From the documentation:
pipe-message (default: |)
Asks for an external Unix command and pipes the current or tagged message
(s) to it. The variables $pipe_decode, $pipe_split, $pipe_sep and $wait_key
control the exact behavior of this function.
So, depending on the 3 settings ($pipe_decode, $pipe_split and $pipe_sep), you can accomplish your use cases.
actually, on my mutt (1.10.1), the | pipes ONLY the current message, not all tagged. I tink, thats the same problem the thread starter had