2017-08-28 114 views
1

我有一个脚本powershell,脚本可以将数据库备份文件复制到网络驱动器。 该脚本只从2复制2个文件。 我想将本地磁盘D上的所有文件复制到网络驱动器,并归档文件。 我该如何修复这个脚本?使用PowerShell将文件复制到共享驱动器

$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\*.* 
+0

是否要从该目录复制项目?你的最终目标是什么? – TheIncorrigible1

回答

0

你将不得不做一个循环来找出所有的文件和复制每一个。我建议做类似:

$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 
} 
1

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

#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 
相关问题