Traverse samba shares during robocopy and exclude some - powershell

I use Robocopy for backup Samba shares into local drive and vice versa.
To sync remote share with local folder I use PS script
$Path = Split-Path "\\192.168.1.100\asuscloud\*"
$Dest = "E:\back_fold\asuscloud\"
robocopy $Path $Dest /B /MT:64 /IS /Z /FP /MIR /TEE /NP /FP /NDL
To exclude some subfolder one can use /XD switch
$Path = Split-Path "\\192.168.1.100\asuscloud\*"
$Dest = "E:\back_fold\asuscloud"
robocopy $Path $Dest /B /MT:64 /IS /Z /FP /MIR /TEE /NP /FP /NDL /XD "\\192.168.1.100\asuscloud\subdir"
However, this syntax doesn't work for the host and shares of this host
$Path = Split-Path "\\192.168.1.100\*"
$Dest = "E:\back_fold\"
robocopy $Path $Dest /B /MT:64 /IS /Z /FP /MIR /TEE /NP /FP /NDL /XD "\\192.168.1.100\asuscloud\"
It throws
------------------------------------------------------------------------------>-
ROBOCOPY :: Robust File Copy for Windows
Started : Saturday, September 1, 2018 8:46:23 PM
Source : D:\back_fold\
Dest -
Files : .
Exc Dirs : \192.168.1.100\asuscloud
Options : . /FP /NDL /TEE /DCOPY:DA /COPY:DAT /Z /NP /IS /MT:64 /R:1000000 /W:30
ERROR : No Destination Directory Specified.
I want to exclude two shares of a host and sync others into local folder.
How to achieve this?
UPDATE: nbtstat -A 192.168.1.100
C:\Windows\system32>nbtstat -A 192.168.1.100
Ethernet:
Node IpAddress: [0.0.0.0] Scope Id: []
Host not found.
Ethernet 2:
Node IpAddress: [0.0.0.0] Scope Id: []
Host not found.
Bluetooth Network Connection:
Node IpAddress: [0.0.0.0] Scope Id: []
Host not found.
Wi-Fi:
Node IpAddress: [192.168.1.105] Scope Id: []
NetBIOS Remote Machine Name Table
Name Type Status
---------------------------------------------
PASCAL <00> UNIQUE Registered
PASCAL <03> UNIQUE Registered
PASCAL <20> UNIQUE Registered
☺☻__MSBROWSE__☻<01> GROUP Registered
WORKGROUP <1E> GROUP Registered
WORKGROUP <00> GROUP Registered
WORKGROUP <1D> UNIQUE Registered
WORKGROUP <1B> UNIQUE Registered
MAC Address = 00-00-00-00-00-00
Local Area Connection* 3:
Node IpAddress: [0.0.0.0] Scope Id: []
Host not found.
NET VIEW \\192.168.1.100
C:\Windows\system32>NET VIEW \\192.168.1.100
Shared resources at \\192.168.1.100
Pascal
Share name Type Used as Comment
-------------------------------------------------------------------------------
asuscloud Disk data's asuscloud in External USB3.0
backup Disk data's backup in External USB3.0
downloads Disk data's downloads in External USB3.0
dropbox Disk data's dropbox in External USB3.0
entware Disk data's entware in External USB3.0
os Disk data's os in External USB3.0
The command completed successfully.

This does not dynamically find the shares on the remote host, but it does allow you to specify the SHARENAMEs from which to copy.
I think your basic misunderstanding is that a hostname (or IP address) alone is not a path. It has to also have a SHARENAME.
If you save this as Do-It.ps1, then to see the verbose messages, invoke it with .\Do-It.ps1 -Verbose
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true, position=0)]
[string]$computername
,[Parameter(Mandatory=$true, position=1)]
[string[]]$sharenames
,[Parameter(Mandatory=$false, position=2)]
[string]$dest = 'E:/back_fold/asuscloud'
)
$excludeshares = #('SHARE1', 'OTHERSHARE')
foreach ($sharename in $sharenames) {
write-Verbose $sharename
if ($excludeshares -notcontains $sharename.ToUpper()) {
$Path = "//$computername/$sharename"
$cmdline = { robocopy $Path $Dest /B /MT:64 /IS /Z /FP /MIR /TEE /NP /FP /NDL }
Write-Verbose $cmdline.ToString()
& $cmdline
}
}
This can be use with a file containing a list of SHARENAMEs.
.\Do-It.ps1 -computername '192.168.1.100' -sharenames $(Get-Content -Path .\sharelist.txt)

remember \ is an escape character. Two \ equals 1 . Try using single quotes ' on your variable strings instead of double quotes "
That's why it's looking for \192.168.1.100\ instead of \192.168.1.100\
I'd also include share names if needed.
Single quotes will allow you to not need to escape. Example:
$Path = Split-Path '\\192.168.1.100\asuscloud\*'
$Dest = 'E:\back_fold\asuscloud\'
Using double quotes might need escaping. Example:
$Path = Split-Path "\\\\192.168.1.100\\asuscloud\\*"
$Dest = "E:\\back_fold\\asuscloud\\"
Please note you might need to escape the asterisk as well.
Including sharename (if needed). Example:
$Path = Split-Path '\\192.168.1.100\c$\asuscloud\*'

Related

Execute seamlessly Robocopy within a Power shell script Gui is launched(!)

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

Userprofile Robocopy Backup

Hi I need to backup the userprofile of multiple PCs, can I use some global commands like %Userprofile% for my Code to backup the logged in User. Also why isn't my script properly backing up the folders which I've told him. The output is currently not accesable it just says you need more rights to open these folders.
$Destination=Read-Host "Please type the path directory you want to copy the backup files" #destination
$Folder=Read-Host "Please type the root name folder" #name of backup folder
$validation=Test-Path $Destination #validate the destination if it has the privileges
New-PSDrive -Name "Backup" -PSProvider FileSystem -Root $Destination #temporary folder for the backup
if ($validation -eq $True){
Set-Location Backup:
}
else{
Write-Host "Error!Run Script Again"
break
}
robocopy "C:\Users\user\desktop" $Destination\$Folder *.* /mir /sec
robocopy "C:\Users\user\pictures" $Destination\$Folder *.* /mir /sec
robocopy "C:\Users\user\documents" $Destination\$Folder *.* /mir /sec
Function Pause{
Write-Host "Backup Sucessfull!!! `n"
}
Pause
The equivalent of %Userprofile% in PowerShell is $Env:UserProfile.
robocopy "$env:UserProfile\desktop" $Destination\$Folder *.* /mir /sec
robocopy "$env:UserProfile\pictures" $Destination\$Folder *.* /mir /sec
robocopy "$env:UserProfile\documents" $Destination\$Folder *.* /mir /sec
If your script is complaining about rights, It's likely that you are running at it via an account that does not have permission to the users folder. User folders are secured to the specific user by ACLs by default. You can likely get around this by running your script with Administrator rights.

Robotcopy with log + report date

I am running a Robocopy command and I am now getting a strange error where it once worked. I did look at several other StackExchange articles, but they do not deal with what I am dealing with.
Talks about C# code
Command for Robocopy to log created date
Create File with current Date in Filename + Robocopy logging
Robocopy: ERROR : Invalid Parameter #4
I created some code that adds a date to the end of the robocopy log.
$ReportDate = (Get-Date).tostring("dd-MM-yyyy-hh-mm-ss")
$RoboCopyLog = New-Item -itemType File -Path C:\Results -Name $("RoboCopyLog_" + $ReportDate + ".txt")
I have logs where this was working, but after some Windows updates and not touching it for a few weeks, I am now getting this error.
-------------------------------------------------------------------------------
ROBOCOPY :: Robust File Copy for Windows
-------------------------------------------------------------------------------
Started : Tuesday, March 21, 2017 7:24:56 AM
Source - C:\MyTests\
Dest - C:\NewTests\
Files :
Options : /S /E /DCOPY:DA /COPY:DAT /R:1000000 /W:30
------------------------------------------------------------------------------
ERROR : Invalid Parameter #4 : "/L:C:\Results\RoboCopyLog_21-03-2017-07-24-56.t
xt"
Simple Usage :: ROBOCOPY source destination /MIR
source :: Source Directory (drive:\path or \\server\share\path).
destination :: Destination Dir (drive:\path or \\server\share\path).
/MIR :: Mirror a complete directory tree.
For more usage information run ROBOCOPY /?
**** /MIR can DELETE files as well as copy them !
The command I am using to create my robocopy job is as follows:
$RoboCopy2 = Start-Job {
$Results = "C:\Results"
$Source = "C:\MyTests"
$Destination = "C:\NewTests"
$ReportDate = (Get-Date).tostring("dd-MM-yyyy-hh-mm-ss")
$RoboCopyLog = New-Item -itemType File -Path C:\Results -Name $("RoboCopyLog_" + $ReportDate + ".txt")
Robocopy $Source $Destination /E /L:$RoboCopyLog
}
Wait-Job $RoboCopy2
Receive-Job $RoboCopy2
As Ansgar Wiechers mentioned, your problem is when calling your /L:RoboCopyLog.
If you see the documentation for Robocopy here you will find that:
/l
Specifies that files are to be listed only (and not copied, deleted, or time stamped).
Whereas what you are requiring is either of these depending if you want to append or not:
/log:<LogFile>
Writes the status output to the log file (overwrites the existing log file).
/log+:<LogFile> Writes the status output to the log file (appends the
output to the existing log file).

Robocopy for long paths with /mir switch changed destination to root and nuked machine

My purpose was to copy files with long paths (+255 char) and spaces in the folder path from a computer to a server with an analogus folder already in place (needed to maintain folder structure). Below is an example of the script:
robocopy "c:\long path\with spaces" "\\servername\long path\with spaces" filename.html /MIR /R:5 /LOG+:\\server\logfolder /v /NP
The result was the below log and a complete deletion of the source machine. Any thoughts?
Source : c:\long path\with spaces
Dest : \
Files : filename.html
Options : /V /S /E /PURGE /MIR /NP /R:5 /W:30
I've been researching and have found no other situation. I'd absolutely love to avoid this issue in the future.
unfortunately, robocopy is going to see the "\\" as an escape sequence, and you end up with a path that's just \. you need to use /"\servername\long path\with spaces\/" for quoted UNC paths.

Powershell Error Trap for Get-ChildItem -Filter and Robocopy (ErrorAction)

I'm drafting a powershell script to manually backup some DC's. The backups need to be moved from one folder to another and I am using Get-ChildItem -Filter "Backup*" to select the backup files, then robocopy to move them.
It works but I would like to add in a trap and custom error message "Error Copying Local Backup from XX to SystemStateBackup" if the source or destination path is incorrect. I would also like to pipe any errors to a log file.
The original with no trap is
Get-ChildItem f:\WindowsImageBackup\ITUPW-PRODDC7 -Filter "Backup*" -Name | ForEach-Object {
robocopy "f:\WindowsImageBackup\ITUPW-PRODDC7\$_" "f:\WindowsImageBackup\ITUPW-PRODDC7\SystemStateBackup\$_" /Z /S /MOVE /njh /njs /ndl /nc /ns /np /nfl}
The trap will work if I only use part of the code
Trap {"Error Copying Local Backup from XX to SystemStateBackup";Continue} Get-ChildItem f:\WindowsImageBackup\ITUPW-PRODDC7 -Filter "Backup*" -Name -ea "Stop"
But it won't work this way
Trap {"Error Copying Local Backup from XX to SystemStateBackup";Continue} Get-ChildItem f:\WindowsImageBackup\ITUPW-PRODDC7 -Filter "Backup*" -Name | ForEach-Object {
robocopy "f:\WindowsImageBackup\ITUPW-PRODDC7\$_" "f:\WindowsImageBackup\ITUPW-PRODDC7\SystemStateBackup\$_" /Z /S /MOVE /njh /njs /ndl /nc /ns /np /nfl} -ea "Stop"
It results in the following powershell error
Get-ChildItem : Cannot find path 'F:\WindowsImageBackup\ITUPW-PRODDC' because it does not exist.
At line:1 char:88
+ Trap {"Error Copying Local Backup from XX to SystemStateBackup";Continue} Get-ChildItem <<<< f:\WindowsImageBackup\I
TUPW-PRODDC -Filter "Backup*" -Name | ForEach-Object { robocopy "f:\WindowsImageBackup\ITUPW-PRODDC7\$" "f:\WindowsIma
geBackup\ITUPW-PRODDC7\SystemStateBackup\$" /Z /S /MOVE /njh /njs /ndl /nc /ns /np /nfl} -ea "Stop"
+ CategoryInfo : ObjectNotFound: (F:\WindowsImageBackup\ITUPW-PRODDC:String) [Get-ChildItem], ItemNotFoun
dException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
Any advice would be greatly appreciated.
Thank you for you time.
Amelia - A sys admin who drew the short straw
You are applying the -ea "Stop" to the foreach rather than to the Get-ChildItem. Move it before the pipe |
For the robocopy, you might want to use $? or $lastexitcode (within the foreach-object) to see if it ran fine.