I have a PowerShell script that gets usernames from a .CSV file and uses the username to robocopy that users data from our old server to the new one. The usernames are formatted as "firstname.lastname" within the organisation. I need the log files for each users robocopy to output as "firstname.lastname-dd-MM-HH:mm.txt"
However when I run my PowerShell script that gets the username from CSV, everything after the firstname becomes the file extension, despite PowerShell showing that it was logged as a .txt.
The script:
$users = Get-Content C:\Veritech\RoboCopy\Users\UserTest.csv
$dt = get-date -f dd-MM-HH:mm
foreach ($UserName in $users) {
robocopy "\\*****\profile$\$UserName" "E:\Shares\Profile$\$UserName" /E /COPYALL /ZB /TEE /R:3 /W:5 /L /NS /NJS /NJH /NDL /FP /log+:"C:\*****\RoboCopy\Users\P-$UserName-$dt.txt"
}
PowerShell log, when run manually, not via task scheduler or .ps1 as intended:
Related
I am trying to delete a folder using robocopy mirroring like this:
Start-Process -FilePath "robocopy.exe" -ArgumentList "$emptyDir $sourcePath /mir /e /np /ns /nc /njs /njh /nfl /ndl" -Wait -PassThru -NoNewWindow but still get a line of output for every deleted file
I tried adding >nul 2>&1 as explained in another answer here Start-Process -FilePath "robocopy.exe" -ArgumentList "$emptyDir $sourcePath /mir /e /np /ns /nc /njs /njh /nfl /ndl >nul 2>&1" -Wait -PassThru -NoNewWindow but still get the same output.
Since you're running robocopy in the current console window (-NoNewWindow), synchronously (-Wait), there is no reason to use Start-Process at all - just invoke robocopy directly, which also allows you to use > redirections effectively:
robocopy.exe $emptyDir $sourcePath /mir /e /np /ns /nc /njs /njh /nfl /ndl *>$null
Note:
Direct execution makes a program's stdout and stderr output directly available to PowerShell, via its success and error output streams.
*>$null is a convenient PowerShell shortcut for silencing all output streams - see about_Redirection.
Another benefit of direct invocation is that the external program's process exit code is reported in PowerShell's automatic $LASTEXITCODE variable.
See also:
This answer provides background information.
GitHub docs issue #6239 provides guidance on when use of Start-Process is and isn't appropriate.
As for what you tried:
You fundamentally cannot suppress output from a process launched with Start-Process -NoNewWindow on the PowerShell side.
Trying to silence command output at the source, i.e. as part of the target process' command line with >nul 2>&1, would only work if cmd.exe were the -FilePath argument and you passed a robocopy command to it. > redirections are a shell feature, and robocopy itself isn't a shell.
You can try to pass arguments via splatting, and then use the object pipeline to parse line by line.
In the example below, I'm going to split the arguments into two groups, in case you wanted to change out the options programmatically.
$roboFileArgs = #(
<#
If you're sure your argument is already a string or
a primitive type, there's no need to quote it.
#>
$emptyDir
$sourcePath
)
$roboFlags = "/mir","/e","/np","/ns","/nc","/njs","/njh","/nfl","/ndl"
# We can use splatting to pass both lists of arguments
robocopy.exe #roboFileArgs #roboFlags |
Foreach-Object {
<#
process output line by line and turn it into objects
or pipe to Out-Null if you truly don't care.
#>
}
Trying to use Powershell to backup some large dir -newbie
I can't make this line to work
"C:\Robocopy\RoboCopy.exe" $source $destination "/E /R:10 /W:5 /V /ETA"
Robocopy at best (depending on the " i put here or there..) is executed but it's its GUI that is launched (and nothing more is done).
There's no issue with the $dest and $source (I manage to log into a txt file and his is working)
Thank you
Use this:
& "C:\Robocopy\RoboCopy.exe" $source $destination /E /R:10 /W:5 /V /ETA
The & (call) operator is required if you want PowerShell to run a quoted string as a command.
In this specific case, the quotes are not needed because the executable's path and filename don't contain spaces, so you can just write this instead:
C:\Robocopy\RoboCopy.exe $source $destination /E /R:10 /W:5 /V /ETA
But is robocopy.exe really sitting in C:\Robocopy? Do you have that directory name? Robocopy.exe is a tool that comes with the OS and should already be in the path. Why not just this?
robocopy $source $destination /E /R:10 /W:5 /V /ETA
I'm working in Sterling B2B Integrator and I have to create a Business Process to collect only the files from "yesterday" (the previous date) The problem is that B2Bi doesn't have a service to do that and the colection directory has over than 7000 files, so I can't use a GetDocInfo service to collect the dates into tags because the Sterling may colapse.
So, I decided to use the Command Line Adapter to invoke a script that would do that for me. The problem is that the script doesn't work either:
set var1=%1 /* UNC File Path */
set var2=%2 /* Source directory */
set var3=%3 /* "yesterday" date */
set var4=%4 /* save the list of files into a .txt*/
set var5=%5 /* copy the files from yesterday into this directory */
PUSHd **%var1%** &
forfiles /p **%var2%** /s /C " cmd /c echo #path #FDATE | findstr /m **%var3%**" > %var4% &
for /f %%a in (**%var4%**) do copy %%a **%var5%** &
Function: The script should collect the files from yesterday and save them into a specific directory.
Example:
PUSHd "\\emea\e801\Public" &
forfiles /p _AppData\CAMS\PDFS\Digital\CertificadoCancelado /s /C " cmd /c echo #path #FDATE | findstr /m "27/07/17"" > _Shared\_AppData\MFT\BackupSterling\temp_puente_PRO\Lista_DIGCRT02\ficherosAyer.txt &
for /f %%a in (_Shared\_AppData\MFT\BackupSterling\temp_puente_PRO\Lista_DIGCRT02\ficherosAyer.txt) do copy %%a _Shared\_AppData\MFT\BackupSterling\temp_puente_PRO\Lista_DIGCRT02\DIGCRT02 &
Why is this script not working?
The script is not working because it is not syntactically correct. What are the asterisks doing around the variable names.
Here is a brief PowerShell script that is the core of what you need to do. It needs to have a Parms() block. When you are satisfied that it will copy the files correctly, remove the -WhatIf from the Copy-Item command.
Please note that this does not maintain the subdirectory structure from the src_dir. This will not work well if you have selected files with the same name in different subdirectories.
$src_dir = 'C:\src\t' #var2
$the_date = '2017-07-21' #var3
$log_file = 'C:\src\xxx' #var4
$dest_dir = 'C:\src\xxx' #var5
if (Test-Path $log_file) { Remove-Item $log_file }
Get-ChildItem -Path $src_dir -File -Recurse |
ForEach-Object {
if ((Get-Date $_.LastWriteTime -Format yyyy-MM-dd) -eq $the_date) { $_.FullName }
} |
Tee-Object -FilePath $log_file -Append |
Copy-Item -Destination $dest_dir -WhatIf
If you -must- do this from a .bat script, put the script above into a filename with a .ps1 extension such as Move-FilesDated.ps1. Then, call it from the .bat script.
powershell -NoProfile -File "Move-FilesDated.ps1"
I am using Robocopy to write to log files:
robocopy "C:\source" "E:\destination" /e /l /njs /njh /log:C:\folder1\reconcile1.txt
When I use this in my batch file the console shows: Log File : C:\folder1\reconsile1.txt
I do no want anything echoed from this command. Is there a way to keep this away from appearing on the console?
You could redirect all output to nul:
robocopy "C:\source" "E:\destination" /e /l /njs /njh /log:C:\folder1\reconcile1.txt > nul
I am trying to run a robocopy script in PowerShell so I can insert it into my other PowerShell script:
$SERVERPATH = "E:\Cisco Config Backups"
$NETWORKPATH = "\\SERVER\Cisco Config Backups"
robocopy $SERVERPATH $NETWORKPATH /E /XC /XN /XO /R:0 /W:0 /COPY:T /LOG:C:\logs\robocopy_logs\config_copy.log /TEE /NP
However when I run this code I see robocopy working where it gives me the following output:
ROBOCOPY :: Robust File Copy for Windows
Started : Thursday, June 30, 2016 1:52:56 PM
Source : E:\Cisco Config Backups
Dest : \\SERVER\Cisco Config Backups
Files : *.*
Options : *.* /TEE /S /E /DCOPY:D /COPY:T /NP /XO /XN /XC /R:0 /W:0
But nothing gets copied over, I want to use robocopy for the logging and so I can have it skip files that already exist.
Please let me know if I am doing anything wrong.
The /COPY:T criteria is specifying to only copy file timestamps, so it is not copying the actual data of the file. However, since there are no files on the destination to copy the timestamps to, nothing happens.
You need to specify at least /COPY:DT so that the file data will be copied, but you can probably just remove the /COPY switch completely as you likely want the file attributes as well, which will get copied by default along with the file data and timestamps.
From robocopy /?:
/COPY:copyflag[s] :: what to COPY for files (default is /COPY:DAT).
(copyflags : D=Data, A=Attributes, T=Timestamps).
(S=Security=NTFS ACLs, O=Owner info, U=aUditing info)
You also need to review all of the switches you are using as some are conflicting--not sure what robocopy does in those cases. As Matt points out in the comment below, you have /E and /S. Do you want to copy empty subdirectories or not? /E specifies to copy empty directories, but /S specifies to NOT copy empty directories.