Quantcast
Channel: Windows PowerShell Forum
Viewing all articles
Browse latest Browse all 2562

Robocopy failing inside Function!

$
0
0

Hi guys,

Currently I'm making a script to copy user data and roaming profiles from a raid array to backup drive. It's going well so far except for the Robocopy part. When the robocopy command is used as a function it errors out but if I place the same command outside of the function it works...

Here is the error:

New-Item : Item with specified name D:\temp\test2\Archive 14-10-2012 already exists.
At line:38 char:24
+         $scriptCmd = {& <<<<  $wrappedCmd -Type Directory @PSBoundParameters }
    + CategoryInfo          : ResourceExists: (D:\temp\test2\Archive 14-10-2012:String) [New-Item], IOException
    + FullyQualifiedErrorId : DirectoryExist,Microsoft.PowerShell.Commands.NewItemCommand

Here is the script:

$ArchiveFolderName = "Archive " + (
Get-Date).Day + "-" + (
Get-Date).Month + "-" + (
Get-Date).Year
$ArchiveSource = "d:\temp\test"
$ArchiveDestination = "d:\temp\test2"
$BackupDrive = Get-WmiObject -Class Win32_LogicalDisk |

Select -Property `
DeviceID,
VolumeName, 
VolumeSerialNumber,  
Freespace |

Where-Object {$_.VolumeSerialNumber -eq "7E816274"}


$ArchiveSourceSize = Get-ChildItem $ArchiveSource -recurse | 

Measure-Object -property Length -sum

Function SpaceCheck
{
If ($ArchiveSourceSize.Sum -lt $BackupDrive.Freespace) {Robocopy}
Else {DeleteOldFiles}
}

Function DeleteOldFiles
{
Get-ChildItem $ArchiveDestination |
sort-object CreationTime -Descending |
select-object -last 1 #|
#remove-item -recurse
& "SpaceCheck"
}

Function Robocopy
{
MD "$ArchiveDestination\$archivefoldername"
Robocopy "$ArchiveSource" "$ArchiveDestination\$ArchiveFolderName" "*.*" /COPYALL /E /Z /XF Thumbs.DB
Exit
}

& "SpaceCheck"

Hopefully someone can see where I went wrong.



Viewing all articles
Browse latest Browse all 2562


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>