2017-08-28 115 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 
相關問題