powershell 从Network Drives Powershell复制

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell 从Network Drives Powershell复制相关的知识,希望对你有一定的参考价值。

$sourceDirectory = "C:\Original"
$destinationDirectory = "C:\Destination\"
$daysBack = 10

function GetFileCount {
    Param ($directory, $time)
    Write-Host "Files Found: " -NoNewLine
    Get-ChildItem $directory -Recurse *.pdf* |  Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-($time)) } | Measure-Object |  %{$_.Count}
}

function ExecuteMigration {
    Param ($source, $destination, $time)
    Get-ChildItem $source -Recurse *.pdf* |  Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-($time)) } | foreach {

        $split = $_.Fullname  -split '\\'
        $DestFile =  $split[1..($split.Length - 1)] -join '\' 
        $DestFile =  ($destination + $DestFile).Replace("\Original", "")
        #$DestFile =  $destination + $DestFile
        $null = New-Item -Path  $DestFile -Type File -Force
        Copy-Item -Path  $_.FullName -Destination $DestFile -Force
    }
}

function DeleteOldFiles {
    Param ($directory, $time)
    Write-Host "Files Found: " -NoNewLine
    Get-ChildItem $directory -Recurse *.pdf* |  Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-($time)) } | Remove-Item -Force
}

Get-Date -Format g

GetFileCount $sourceDirectory $daysBack

Get-Date -Format g

ExecuteMigration $sourceDirectory $destinationDirectory $daysBack

Get-Date -Format g

DeleteOldFiles $sourceDirectory $daysBack

echo "Done"
# EXECUTE THE COMMENT BELOW TO SEE FILES OLDER THAN 2 YEARS

$sourceDirectory = "C:\TestMoveCopy\Files\"

$destinationDirectory = "C:\Tainas\Files\"

$daysBack = 30

function GetFileCount {
    Param ($directory, $time)
    Write-Host "Files Found: " -NoNewLine
    Get-ChildItem $directory -Recurse *.* |  Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-($time)) } | Measure-Object |  %{$_.Count}
}

function ExecuteMigration {

    Param ($source, $destination, $time)
    
    Get-ChildItem $source -Recurse *.pdf |  Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-($time)) } | foreach {

        $split = $_.FullName  -split '\\'

        $DestFile =  $split[1..($split.Length - 1)] -join '\' 

        ## Build the new  destination file path

        #$DestFile =  ($destination + $DestFile).Replace("C:\Files", "")
        $DestFile =  ($destination + $DestFile)
        

        ## Copy-Item won't  create the folder structure so we have to 

        ## create a blank file  and then overwrite it

        $null = New-Item -Path  $DestFile -Type File -Force

        Copy-Item -Path  $_.FullName -Destination $DestFile -Force
    }
}

function 


GetFileCount $sourceDirectory $daysBack

ExecuteMigration $sourceDirectory $destinationDirectory $daysBack

echo "Done"

以上是关于powershell 从Network Drives Powershell复制的主要内容,如果未能解决你的问题,请参考以下文章

如何troubleshoot SharePoint Online map network drive失败?

SMART Hard Drive INFO Powershell [重复]

Map network drive遇到报错“The network folder specified is currently mapped using a different user

DHCP/DNS offload and Config drive support for adv shared network

关于windows service不能访问网络共享盘(NetWork Drive)的解决方案

powershell network.ps1