PowerShell Tee-Object command with -Append - powershell

I run into an issue with the PowerShell 4.0 Tee-Object command (alias tee) and the command that I'm using is as follows:
powershell "cmd /c dir | Tee-Object -filepath C:\1.txt -Append"
When 1.txt is an empty file, it writes the output as you see on the screen (as-is). But if 1.txt has some existing content before you run the command, it appends the output with no new lines and the files looks completely messed up.
I'm surprised to see this behavior and any thoughts would help me to proceed further. Note that I have to use the Tee-Object command as I would like to see the output on the screen when a command is running and append it to the existing log file that has some contents.

Related

"wmic memorychip get | clip" in cmd

Hello i am not able to run this command:
wmic memorychip get | clip
This command does not returns any output or error.
I am using windows 7.
Kindly tell me what to do?
Thanks
This happens, as the output is piped to clip.exe. In other words, the output is there, but is sent to clipboard instead of printing it into the console.
As a side note, consider using Set-Clipboard instead of clip.exe, if you have Powershell 5.1 or newer.
If there's a need to send output to clipboard and see it too, use Tee-Object like so,
wmic memorychip get | tee-object -variable foo | set-clipboard; $foo
As vonPryz answered that the ouput is there but is redirected to clip.exe(A program that redirects output of command line tools to the Windows clipboard).but as you have described that you have windows 7,vonPryz solution won't work.you can use wmic memorychip to show output on console but it will mess up the output. you should echo the output in a file to see it(i.ewmic memorychip >>output.txt) or use the following batch file:
#echo off
wmic memorychip >>output.txt
notepad output.txt
pause >nul

Right usage of 2>&1 | tee or | tee with powershell

I want to see the output of the command in the console and save it in a file so I have these two options:
When I use command | tee output.txt somehow it generates no output file at all but it works as usual in the console.
When I use command 2>&1 | tee output.txt it generates a fine output file but the text in the console appears in red.
Is there any way to either fix the first option or let the text appear as usual in the second one? I am using Windows PowerShell (Windows 10) and the Programm I am using this for is liquibase 3.5.5. just for the case that this is important.
In PowerShell, redirecting stderr lines from an external program to PowerShell's success stream via 2>&1 wraps those lines in [System.Management.Automation.ErrorRecord] instances in case they are captured for further processing.
In Windows PowerShell, these captured instances render like PowerShell errors, which is why you're seeing the red output (by contrast, without the redirection, the stderr lines would be passed through to the console, without coloring).
A simple workaround is to convert these objects to strings explicitly, which outputs the original lines (PSv3+ syntax; built-in alias % for ForEach-Object used for brevity):
... 2>&1 | % ToString | Tee-Object output.txt
Note: This workaround is no longer necessary in the install-on-demand, cross-platform PowerShell (Core) 7+ edition, where even captured stderr lines now consistently render just as strings.
If you are in DOS then you can leverage powershell (from DOS) and use tee-object do more or less a tee like in Linux.
C:\> powershell some_command ^| tee-object -FilePath some_outputfile
The ^ escapes the pipe so that the pipe applies to powershell and not DOS.
Powershell and tee-object should come with Windows as standard so nothing to install!

Powershell write output of a program to a console live and pipe to a file

I am running a fairly complex python script from a powershell script. I would like to both display the stdout and stderr streams of the .python script in the console in real time (ie as the .py script writes to them) and also write them to a file.
My current solution looks like this:
Do-PreliminaryStuff
log = & ".\myPyScript.py" -myArgument "\this\that\thingy.txt" 2>&1 $log
Write-Output $log
$log | Out-File "$logDir\pyScriptLog.log"
Do-RestOfScript
This has the problem that the text is only printed out after the .py script has finished, making it much harder to watch the progress of the .py script.
Is there a way to somehow ..sample.. the pipeline as object go through it?
You are probably looking for the Tee-Object cmdlet.
Use Tee-Object - it splits the pipeline into a filestream before continuing:
& ".\myPyScript.py" -myArgument "\this\that\thingy.txt" 2>&1 |Tee-Object -FilePath $log

One of the Powershell Command is not working in a script but works if run on the command prompt

I have the following 2 commands.
Command 1:
Get-QCSyncHistoryStep -Workflow "CSV2AD" -Name "Provision from CSV to 31AD" -Count 1 | ForEach {$_.SuccessOperations} | Out-File c:\scripts\temp1.txt
Command 2:
Get-QCSyncHistorySummaryRun * | Out-File c:\scripts\temp2.txt
Command 1 works only on powershell prompt but not as a script. Where as command 2 works on both prompt and when used in script file.
If I run both the command in a script file, the command 1 is not writing anything into the file where as command 2 dose.
Need help in deciphering the issue.
Thanks.
Your problem may be that Out-File overwrites the contents of the destination file unless you specify the -Append switch, which can still be used if the file doesn't already exist. I'm not familiar with the Get-QCSyncHistory* cmdlets or their output (are they your own functions?), but I suppose that if you pipe the objects in the output through a foreach loop and the final object has a null-valued SuccessOperations parameter, then you'll effectively just wipe the file you have been trying to write to.

Capture snmpwalk output via powershell

I would like to run a program and save its output into a file. The program starts, I see a cmd window pop-up, but the file is alway empty. I tryed these versions:
start snmpwalk.exe "parameters will be here" >text.txt
start snmpwalk.exe "parameters will be here" 2>&1 | out-file text.txt
Can you help me how to solve the problem?
Thanks
You don't need to use start, you should be able to just use the command as normal from within powershell.
A slightly more powershell way would be to use invoke-expression
invoke-expression "snmpwalk [parameters]" | out-file text.text