rss logo

PowerShell script to move a file to a network share

A PowerShell script to move a file through network share.

Code

########################### # author : shebangthedolphins.net # version : 1.0 # date : 2016.06 # role : Move a file through network shares : \\10.0.0.1\folder\folder\file.txt to \\10.0.0.2\share\a\folder\file.txt # other : # updates : # - 1.X (x/x/xxxx) : # variables initialization $SRC = 'P:\folder\' $DST = 'O:\a\folder' $HIST = 'log.txt' # Mount network share P: $WshNetwork = New-Object -comObject Wscript.Network Try { $WshNetwork.RemoveNetworkDrive('P:') } Catch { Write-Host 'P: is already mounted' } Try { $WshNetwork.MapNetworkDrive('P:','\\10.0.0.1\folder',$false) } Catch { 'Error when trying to mount P' | Out-File -Append $("F:\" + $HIST) -Encoding utf8; exit 1 } # P: end # Montage lecteur réseau O: $WshNetwork = New-Object -comObject Wscript.Network Try { $WshNetwork.RemoveNetworkDrive('O:') } Catch { Write-Host 'O: is already mounted' } Try { $WshNetwork.MapNetworkDrive('O:','\\10.0.0.2\share',$false) } Catch { 'Error when trying to mount O' | Out-File -Append $("F:\" + $HIST) -Encoding utf8; exit 1 } # O: end # Robocopy 'P:\folder\file.txt' to 'O:\folder\file.txt #robocopy $SRC $DST\folder file.txt /COPYALL /NP /XJD /R:1 /W:1 Write-Host "mi -path "$SRC"file.txt -destination $DST\$(Get-Date -f yyyy-MM-dd-HH-mm)_file.txt" mi -Force -path $($SRC + "file.txt") -destination $DST\$(Get-Date -f yyyy-MM-dd-HH-mm)_file.txt # unmount network share $WshNetwork.RemoveNetworkDrive('P:') $WshNetwork.RemoveNetworkDrive('O:')

Scheduled Task

We can schedule the move :

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Contact :

contact mail address