使用 PowerShell 将文件复制到共享驱动器

Use PowerShell to Copy Files to a Shared Drive

我有一个 powershell 脚本,该脚本将数据库备份文件复制到网络驱动器。 该脚本仅从 2 个复制 1 个文件。 我想将所有文件从本地磁盘 D 复制到网络驱动器,并将文件存档。 我该如何修复这个脚本?

$TimeStamp = get-date -f dd_MM_yyyy

$Destination = "M:\networkdrive\folder\BACKUP_10.200.153.55\DATA_" + $TimeStamp

New-Item -ItemType directory -Path $Destination -Force

Copy-Item -Path D:\MSSQL\BACKUP\*.* -Destination $Destination -Force

Remove-Item D:\MSSQL\BACKUP\*.*

您将不得不循环查找所有文件并复制每个文件。我建议做类似的事情:

$TimeStamp = get-date -f dd_MM_yyyy
$Destination = "M:\networkdrive\folder\BACKUP_10.200.153.55\DATA_" + $TimeStamp
New-Item -ItemType directory -Path $Destination -Force

gci "D:\MSSQL\BACKUP" -recurse | %{ #loops through al files in that folder and subfolders
    #gets path to file
    $file = $_.Fullname

    #copies file 
    Copy-Item -Path $file -Destination $Destination -Force 

    #removes file from original location
    Remove-Item $file 
}

这会将压缩的备份文件夹发送到网络存储。

#requires -Version 5

$Destination = "M:\networkdrive\folder\BACKUP_10.200.153.55\DATA_$(Get-Date -f dd_MM_yyyy).zip"

Compress-Archive -Path D:\MSSQL\BACKUP\* -DestinationPath $Destination -CompressionLevel Optimal -Force -ErrorAction Stop

Get-ChildItem D:\MSSQL\BACKUP -Recurse -Force | Remove-Item -Force