r/PowerShell • • 11d ago

Question Robocopy log question

Within the company I work for we use a batch file with a robocopy command to "install" a specific piece of software on user's laptops. This was chosen because the software is particularly difficult to setup right. It now only requires us to do one proper installation, after which we manually copy the software to a directory on the company network. From there, robocopy copies it to the user's hard drive.

The command looks like this (I had to remove company and software names):

ROBOCOPY I:\xxx-xxxxxx\X\apl\SOFTWARE\00-Admin\Admin-xxx\Install\Software%VERSION%\ C:\Users\%USERNAME%\AppData\Local\Software%VERSION%\ /S /LOG+:install%VERSION%.log

As can be seen, a logfile is extended each time a user runs the batch file and installs the software. Not only can we see if the install was succesfully, but it also allows us to track who has installed the software and should be informed of any changes or updates.

In the past (I'm talking about 2022 and earlier), not one but two log files were present. In the first file, named install%VERSION%.log, the full log could be found. The second file, named install%VERSION%_Users.log, only contained the start date & time, source paths, destinations and user's full names. This made it incredibly easy to quickly find out who uses the software. At some point after 2022, this particular file stopped being updated. The robocopy command itself however, has not changed once since the batch file was originally created in 2016.

Was this an automatic function of the robocopy /log+ command? Is there a way to bring this back? I cannot find any information about this online at all. Almost as if it never has existed. But yet, somehow, for 6-or-so years we had two log files updating simultaneously.

It is not something we particularly need, but it used to be quite a lot easier to keep track of the users using this second (and shorter) log file.

1 Upvotes

14 comments sorted by

7

u/BlackV 11d ago

Ignoring robocopy is not PowerShell

It's probably helpful to see ALL your code as robocopy it's self does not do that natively

1

u/Quefox_ 10d ago

Ignoring robocopy is not PowerShell

I am far from an expert on this topic. I was searching for solutions on Google and stumbled upon this subreddit. I have also posted it on r/Batch if that is a better place.

It's probably helpful to see ALL your code as robocopy it's self does not do that natively

I will have to find some time to make a copy of the batch file with all company information removed. Give me some time.

1

u/BlackV 10d ago

Ya understood, code is king, I think someone else also mentioned should just a simple line to add to the batch

Or convert to powershell script for that matter

1

u/da_chicken 10d ago

Was this an automatic function of the robocopy /log+ command?

No. The log options are very basic. There also were not any changes to the command in 2016. There were several additions for Windows 11 and Server 2022 in that year, but none of them related to logging. The last change before that was in 2008 to add the multithreading option.

I agree that there must have been a second script.

Are you using %VERSION% to obfuscate your code? Is that literally the file name? Or is this actually a batch script and you have variable expansion?

1

u/Quefox_ 10d ago

I agree that there must have been a second script.

I have checked the full batch file but apart from this robocopy command it is just "if else" statements checking for and deleting old versions of the software prior to installing the latest version. I could not find any commands writing to this specific file.

And if there were, it should still be there. The only changes made to the batch file was adding additional "if else" statements checking for old version as there have been new versions of the software a couple times now.

1

u/da_chicken 10d ago

Given your language here and the fact that you still didn't answer my question, I'm forced to assume you're not using Powershell at all and you actually are using a .bat or .cmd batch script.

So the next thing to check is what else is changing the value of the VERSION environment variable.

1

u/Quefox_ 10d ago

Sorry, I completely missed your question somehow.

Or is this actually a batch script and you have variable expansion?

Yes, it is a batch script and %VERSION% a variable. The value is set at the beginning of the code to whatever the latest available version of the software is.

I am far from an expert on this topic. I was searching for solutions on Google and stumbled upon this subreddit, but have since found out from replies that Powershell and Batch are very different things. I have also posted my question on r/Batch if that is a better place.

1

u/da_chicken 10d ago edited 10d ago

Sorry, I completely missed your question somehow.

That's okay, my post got partially cut off when I first submitted it due to a copy/paste mistake. I thought I edited it quickly because it doesn't even show as edited, but maybe that's what you saw.

But the only possibilities are:

  1. The script changed.

  2. There's a second script or something else writing the second set of log files.

  3. The value of your environment variable is being changed by the script or set in the calling environment prior to this script being called. That might include some weird delayed expansion going on. Or there's something system vs user environment variable scopes going on.

  4. The timestamp is lying to you.

I don't see any other possibility.

If you want, you could add setlocal enabledelayedexpansion and then replace %VERSION% with !VERSION!, but that should only be necessary if you're using for or if in batch... but we can't see your code.

You should also consider the idea that the script could have been overwritten (edit: actually, deleted and replaced) by a copy of an old version of the script before the changes that had the different behavior, and the timestamp was reset. You don't even need robocopy to do that. If you copy a file from one folder to another on the same volume, typically the LastWriteTime does not change.

Alternately, someone might have manually set the timestamp on the script file directly. (Get-Item .\MyFile.txt).LastWriteTime = '1/1/2030' works just fine.

Ultimately, I wouldn't worry about it. I would instead:

  1. Put your script into version control. You just learned why it's important!

  2. Fix the script to work the way you want. The old version is gone. Just let it go.

  3. I would also consider switching to Powershell and using signed scripts, even if you're just using it to call robocopy.

3

u/dero1010 11d ago

It would seem there is another script out there. The existing Robo copy command would not write to that log file.

1

u/gowinder 10d ago

/LOG+ appends on purpose; /LOG overwrites and /TEE also echoes to the console. But do not read the log to decide whether the copy worked. Robocopy sets ERRORLEVEL 0-7 for success (copied, extras, mismatched) and 8+ for failure, so in the batch: if %ERRORLEVEL% GEQ 8 (echo failed & exit /b 1).

If that log contains lines this command cannot produce, something else is writing to it — look for a wrapper script or a scheduled task pointed at the same file.

0

u/fecklesspleb_tools 11d ago

Robocopy itself never did that. Its only log switches are /LOG, /LOG+, /UNILOG, /UNILOG+ and /TEE, and each one writes a single file. So the _Users.log was coming from something else: most likely an extra line in an older copy of the batch file, a logon script, or a GPO. If the share has Previous Versions or backups, the pre-2023 .bat is worth a look.

To bring it back, add one line right after the robocopy call:

>>"install%VERSION%_Users.log" echo %DATE% %TIME% %USERNAME% "I:\...\Software%VERSION%" "%LOCALAPPDATA%\Software%VERSION%"

The batch only knows the username, so I'd resolve full names afterwards, e.g. with the RSAT AD module: Get-ADUser <username> | Select-Object Name.

One more thing: both log paths in your command are relative, so they land in whatever the working directory is when the batch runs. If that changed at some point, the old file may just be getting written somewhere else.

(AI-assisted answer; checked against Microsoft's robocopy docs, not run on Windows.)

1

u/Quefox_ 10d ago

So the _Users.log was coming from something else: most likely an extra line in an older copy of the batch file

I have checked side by side the newest version of the batch file to the latest version in which I know this has worked. The only differences were additional "if else" statements checking for old software versions.

The batch only knows the username, so I'd resolve full names afterwards

Thanks for the suggestion, I will try adding that to the batch file. As far as names go, the username is fine enough for us. We can easily find people by their username through Teams and Outlook, so no need to know the full names.

One more thing: both log paths in your command are relative

Yes that is correct. It was already this way before I took over the responsibility for this software. The batch file is within the network folder where we also store the software. Users run the batch file from a link we send out by e-mail. It may not be the cleanest solution, but it works as intended.

1

u/BlackV 11d ago

One more thing: both log paths in your command are relative

ya that's the first thing I saw too /LOG+:install%VERSION%.log is a relative path

The batch only knows the username, so I'd resolve full names afterwards, e.g. with the RSAT AD module:

the batch is run as the user, on the users machine (by the looks of it)
installing the RSAT module on each machine would not be reasonable, especially when user variables exist for this already (being %userdomain% and %username% in batch land)

personally also I'd use %LOCALAPPDATA%\Software%VERSION% vs C:\Users\%USERNAME%\AppData\Local\Software%VERSION%