Save output of PowerShell script (original) (raw)
I want to run a PowerShell script and based on the output, do “something”. I originally attempted to save the output of the script to a variable, but a coworker said I can’t do that. I then tried saving the output to a text file, but that failed too…the text file is just blank. How can I do what I want?
What I want to do is run a script. Based on the results, if the output is “allow”, send an email. If the output is “disable”, do nothing and move on.
Thanks
I think I have this working. I had to log the output using “Start-transcript”, then look through that output for what I want, but it works
adminofthings (AdminOfThings) June 10, 2025, 2:12pm 3
More context is needed to determine what options are viable. A script can output to a variable and a text file. The part where you “send an email” is what is unknown here.
moorebeers (MooreBeers) June 10, 2025, 2:36pm 4
I agree with @adminofthings and would need to know what you’re trying to achieve.
Could you give us a sanitized version of what you’re trying to do since you may need to only out put a success at certain points instead of going through the entire transcript of the process?
This is what I was trying to do…there was no output saved to the variable
adminofthings (AdminOfThings) June 10, 2025, 3:12pm 6
If that output wasn’t saved to a variable or a file, then you were likely not using the success stream to write to the console. If you use something like Write-Host, it uses the information stream and therefore by default isn’t stored in a variable.
moorebeers (MooreBeers) June 10, 2025, 4:21pm 7
I know I’m not the most versed in PS or scripting, but I think what your trying to do is get the SSH output to be the variable answer?
Since you’re running it against a remote machine I don’t know if that’s how you’d get that output into your variable.
Let me know if I’m thinking this wrong, but would you need to do something like
$Value = (C:\scripts\script.ps1 -vip DataHere -profilename ssh -clusterName ClusterHere).output
Like I said I’m not sure if this is anywhere near right, but I don’t think you can put the value of a remote machine into a variable without some type of PassThru command or option.
Edit: Okay so I did some reading and you can’t get another computer’s memory space put into your computer the way you have it written.
What was said is that you’d need to capture the result on the remote machine first to put into a variable at that machine then bring it to you local machine. So basically you’d do an Invoke-Command on the remote machine and put the result of your ps1 file into a RVariableandthenhavethatresultantobjectasyourlocalRVariable and then have that resultant object as your local RVariableandthenhavethatresultantobjectasyourlocalVariable.
This may help: Invoke-Command to Local Variable - #5 by carl-trevett - PowerShell Help - PowerShell Forums