Powershell robocopy logging - powershell

How can you log to a text file only new files that have been robocopied from source to destination. I've tried the robocopy /LOG:file command however that logs everything

You can use the Out-File function in powershell in order to output logs to text file instead of outputting them in the shell.
$execution = #some robocopy function to run
$execution| Out-File -LiteralPath "your_path"

Related

Start-Transcript and Logging Batch File Output

I have a function in a PowerShell module which creates a log file and starts a transcript using that file (see below). When running a PowerShell script, this works great and captures all output.
When running a PowerShell script which calls a batch file (which we do quite often while migrating from CMD > PowerShell), the batch file output shows on the console in the same window as the PowerShell script, but the transcript log file shows only 1 blank line where the call to the batch file is.
09:53:25 AM [Success] Zip file already up to date, no need to download!
09:53:25 AM [Note ] Calling 1.bat
10:07:55 AM [Note ] Calling 2.bat
I'm calling the batch files from .ps1 scripts with only the ampersand '&'.
What's strange is that sometimes the batch file output is captured in the log (usually the first batch file called). However I can't find anything special about these files.
What's also strange is that sometimes we call external programs (WinSCP) and the output from those commands only sometimes show in the transcript. Possibly relevant.
For reference, here is the function I use to create a transcript of our processes.
Function Log_Begin()
{
<#
.SYNOPSIS
Starts the process for logging a PowerShell script.
.DESCRIPTION
Starts the process for logging a PowerShell script. This means that whenever
this function is called from a PowerShell script, a folder called 'Logs' will
be created in the same folder, containing a full transcript of the script's output.
.EXAMPLE
C:\PS> Log_Begin
#>
Process
{
$ScriptLoc = $MyInvocation.PSCommandPath
$WorkDir = Split-Path $ScriptLoc
If (!(Test-Path "$WorkDir\Logs")) {mkdir "$WorkDir\Logs" | Out-Null}
$LogPath = "$WorkDir\Logs"
$ScriptName = [io.path]::GetFileNameWithoutExtension($ScriptLoc)
$LogDate = Get-Date -format "yyyy-MM-dd"
$LogName = "$ScriptName $LogDate.log"
$global:Log = $LogPath + "\" + $LogName
$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue"
# Create file and start logging
If (!(Test-Path $Log)) {
New-Item -Path $Log -ItemType File | Out-Null
}
Start-Transcript -Path $Log -Append
}
}
Does anyone have any ideas on how I can capture the batch file output? Preferably I wouldn't have to change every call to a batch file from the script, and make something in the module.

I need a powershell script to automate the task of saving data into a text file with out replacing old files in a iterative process

I have a .ps1 file that is executed whenever TFS calls it and it's output will be saved to particular file (D:/Deploytest/output/output.txt). If the script is triggered for the second time then the output.txt file is getting replaced with the new content. How can I keep the old file in the folder and add output to a new file?
I was using following command in .ps1 file. How to modify to achieve my task.
Invokesqlcmd -Inputfile "path" | out-file "D:/Deploytest/output/output.txt"
Thanks.
#create path output file horodated
$Outfilename="D:/Deploytest/output/output_{0:yyyyMMddhhmmssffff}.txt" -f (get-date)
#out you command into this file
invoke-sqlcmd -Inputfile "path" | out-file $Outfilename

open log file at end of Powershell script

I have a Powershell script that writes to a log file (log.txt). What command(s) will work at the end of the script to open this log file automatically on the computer?
This would work:
Invoke-item log.txt
Here are a few options
You can just call the file using relative path
.\log.txt
using notepad.exe to open the file
notepad.exe .\log.txt
use Invoke-Item
ii .\log.txt
Invoke-Item .\log.txt
or if you want the content to write to the host you're running the script in
# You may also use 'Clear-Host'
Clear
Get-Content ".\log.txt"
shorthand:
cls
gc .\log.txt

Write commands (i.e. input not output) to a file

The Out-File lets us write output to a file. What if we want to write input to a file?
I want a list of all the commands that I wrote during a PowerShell session. The file should look something like this:
dir
cd
ni myFile.txt -t file
cd..
We want to keep track of what commands we use in PowerShell.
Try this:
Get-History |
select -ExpandProperty commandline |
Add-Content commandhistory.txt
You could make use of Start-Transcript and Stop-Transcript
Technet links about each command: start-transcript and stop-transcript
Usage example: Start-Transcript -Path C:\Folder\Transcript.txt

Powershell: After using Out-File, Robocopy will append a bunch of gibberish to the same log file

I need to write to a particular log file, and append the results of multiple robocopy commands to the end of that file.
The trouble is, when the robocopy is preceded by an out-file command, the robocopy writes a bunch of random characters to the log. My guess is that the log is still in use by the previous out-file command, but from what I could find about it, out-file is supposed to create, open, and close files automatically.
Here is a slimmed down example of what I'm trying to do:
"lala" | Out-File -filepath log.txt
robocopy .\source .\destination 1.txt /log+:log.txt
In the above example, the log is created, and the contents look like this:
lala
਍ⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭഭ †佒佂佃奐††㨠›††潒畢瑳䘠汩⁥潃祰映牯圠湩潤>獷†††††††††††††††਍ⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭഭഊ 匠慴瑲摥㨠䴠湯䴠牡>
etc.
Commenting out the first line and using only the robocopy command works just fine, but if I add the first line, the log gets messed up again. Anyone have any ideas/wisdom for me?
Thanks for reading.
d:- D
Just encode in the same manner of robocopy log:
"lala" | Out-File -filepath log.txt -encoding utf8 -append
then you can append robocopy logs.