PowerShell脚本不从目录中提取文件

时间:2016-06-13 14:56:00

标签: powershell copy directory

我正在编写一个脚本来从远程计算机复制用户数据(\ RemotePC \ C $ \ Users *)。对于我的方案,认为相关的文件在90天内。我遇到的问题是,无论我似乎改变什么,文件夹似乎永远不会复制。从示例中,我的C:\ TestFolder \包含4个文件夹,其中包含文本文件和文本文件。

当我运行脚本时,只复制文本文件本身。我希望复制此目录中的所有内容。提前谢谢。

 $OldPC = l02argallim
#$OldPC = Read-Host "Input PC name to copy files from"
#$OldPath = "\\OldPC\C$\Users\*"
$OldPath = "C:\TestFolder\"
$NewPath = "C:\OldUserBackup\"
$MaxDays = "-1"
$CurrDate = Get-Date
#$Exclude = @("rgallimore", "eaune", "dfreeman", "pduffee", "public", "default", "administrator")
$Exclude = @("b")
cls
$PathExist = Test-Path -Path $NewPath
    if($PathExist -eq $True)
    {
        Write-Host "$NewPath has already been created." -ForegroundColor Red
        Write-Host "The script will now terminate." -ForegroundColor Red
        Pause        
    }
    elseif($PathExist -eq $False)
    {
        New-Item $NewPath -type directory
        foreach($file in (Get-ChildItem $OldPath))
        {   
            if($file.LastWriteTime -gt ($CurrDate).AddDays($MaxDays))            
            {
                Copy-Item -Path $file.FullName -Destination $NewPath -Recurse -Force
                Write-Host "$file copied successfully." -ForegroundColor Cyan
            }
            else
            {
            Write-Host "Not copying $file" -ForegroundColor Yellow
            }
        }
        $a = [math]::abs($MaxDays)
        Write-Host "$OldPath successfully copied contents from the past $a day(s) to $NewPath" -ForegroundColor Green
        Pause           
    }
    else
    {
        Write-Host "An unknown error has occured. Error 01." -ForegroundColor Cyan
        Pause
    }

0 个答案:

没有答案