I have a lot of PowerShell script. One main, that calls other, child ones. Those PS scripts in their turn call windows CMD scripts, bash scripts and console applications. All these scripts and applications write messages to console. PowerShell scripts, for example, are using Write-Host scriptlet for this purpose.
Question: how can I easely redirect (send) all this console output to some file, while not deafening (canceling) this console output? I want to be able to see whats going on from console output and also have history of messages in log file.
Thanks.
You can use the tee equivalent of PowerShell : Tee-Object
PS: serverfault.com and/or superuser.com are more suitable for a question like this.
You can try Start-Transcript and Stop-Transcript. It has a couple of limitations like not capturing native exe output. It is also global to PowerShell session.
I've found script for grabbing console output: http://gallery.technet.microsoft.com/scriptcenter/e8fbffde-7d95-42d9-81de-5eb3d9c089e0. Script returns HTML to preserve colors.
The only big downside - you must call it at the end of your script to capture all console output it have made.
You'd probably need to write a custom host to do this. It's not a terribly hard thing to do, but it's does require some managed code.
Related
I am trying to modify several values of a PowerShell script template prior to execution. The approach I took was to use the Get-Content command to read the template file, then to use the replace operand to replace the content with a content of my choosing, and then the Set-Content command to update the file, then execute the script. However, according to the error messages that I encounter, it seems that the modified file is not running, but the template one.
The thing I find hard to understand is why, as I use the Get-Content once again and print the result prior to execution, where I witness that the file did change.
I use simple PowerShell scripting with no threads or so ever, the script is being executed on an Azure VM via the Run-Command feature. I wonder why it happens.
Can anyone please explain?
I'm not a native English speaker as such pardon some discrepancy in my question. I'm looking at a way to Automate option selection in Programs/Scripts running via PowerShell.
For example:
Start-Process -FilePath "velociraptor.exe" -ArgumentList "config generate -i"
In the above snipper PowerShell will run Velociraptor and initiate the configuration wizard from a Ps1 file. The Wizard has few options. After running it will generate some Yaml files.
As such what would be the way to have PowerShell Script automate the option selection process? I know what the option should be. I looked around but I don't know proper terms to find what I need. Nor am I sure this can be done with PowerShell.
The end goal is to have the Ps1 download Exe, run the config command and continue with choosing the selection based on predefined choices. So far I gotten download and lunching of the velociraptor.exe working. But not sure how to skip the window in screenshot and have PowerShell script do it instead.
I couldn't find a CLI reference for velociraptor at https://www.velocidex.com/, but, generally speaking, your best bet is to find a non-interactive way to provide the information of interest, via dedicated _parameters_ (possibly pointing to an input file).
Absent that, you can use the following technique to provide successive responses to an external program's interactive prompts, assuming that the program reads the responses from stdin (the standard input stream):
$responses = 'windows', 'foo', 'bar' # List all responses here
$responses | velociraptor.exe config generate -i
I think that my question is something too easy that you guys will solve in 1 minute.
I'm trying to run a script that have multiple lines of code. But, when I write the first line and hits SHIFT+ENTER it runs the code. I need to write a new line, instead of running what I've wrote.
Anybody knows what should I do (instead killing myself because I'm too dumb) ?
In powershell console there are a few ways to make a new line
A. Shift + Enter : Use this at any point to make a new line
B. The opening of a string " or ' until the closing of the string " or ' : use this when you have a string that you wish to span many lines
C. A pipe | : Use this if you have output that you would like to pass to another command
D. The Back tick (escape char) ` : use this to separate lines for a new command or splitting a command into other lines
If you are new to powershell, I would suggest using Powershell ISE. If its installed you can go to the powershell console and type ISE or go to start and type Powershell ISE. This will be a good place to run scripts and debug as you can add breakpoints to your scripts.
The easiest and best way to do this would be to create the script inside of the PowerSheell ISE program. You can then reference this script and run it in the console by preceding it with a .\script.ps1.
If needed you can create script on the command line by creating and writing to the file from the console.
Open the PowerShell console
Run the following command to create a file New-Item script.ps1
Run the next command as many times as it takes to populate the file Add-Content script.ps1 "My code line here"
Run the code using the script run command .\script.ps1
Now let it be known that the ISE is a much better tool because it allows for debugging of files and testing them on demand. The only downside is it will cache whatever it uses or creates (such as variables or references). If you aren't getting the expected result trying closing and reopening to clear the cache run it from the console in tandem. One last thing to note is that if you use the ISE and it successfully runs there that doesn't mean it will run in the console. Be sure to test thoroughly.
We have build scripts for cmd. For example here is output of one of them.
When I run same commands in PS it outputs everything in red (probably because it's for err output).
Can I keep colors somehow?
At first, I didn't think it was possible.
But, after a little searching I found this blog post, Colorized capture of console screen in HTML and RTF.
You should be able to use the PowerShell script from there to create what you need.
I'm not positive this will work, but if you don't mind redirecting stderr to stdout, PowerShell won't try to color everything red. To do this, just add 2>&1 when you run your cmd script, e.g.
build.cmd 2>&1
I am trying to write the output of a powershell command to a file. I have completed this:
powershell psake "Dev08, Deploy-things" >> C:\Deployments\DeploymentLog.txt 2>&1
This is dumping all the screen output to a File... GREAT!!
NOW, I also want it to dump to the console screen too. I have used the argument >CON
but it will dump everything to the screen, but not the file.
powershell psake "Dev08, Deploy-things" >> C:\Deployments\DeploymentLog.txt 2>&1 >CON
Maybe >CON is not the way to go.. Here is a test I am using:
dir > c:\test\directory.txt 2>&1
Anyone got an idea on how to do this?????
Instead of having CMD output the file, let your Powershell command do it with the Tee-Object cmdlt.
So for your case, something like:
powershell ^& "'C:\Program Files\7-Zip\7z.exe'" ^| tee B:\test.log
Note that the ^ is required before | so that way it passes it to Powershell and isn't interpreted literally.
Well, as it turns out, I cannot find anything that says you can write to 2 different outputs in Command Prompt. Once you redirect the output to a file, you redirect it. That's it. I cannot find any way to write to the screen as well using commands. SO, the solution that I used was to download "TailXP". Install it on the box and configure it to point to the file i am writing. This will read the file as it's being generated and write it to it's own console screen. It served our purpose and I have it all wrapped up in a .cmd file.