【问题标题】:Use PowerShell to Copy Files to a Shared Drive使用 PowerShell 将文件复制到共享驱动器
【发布时间】:2017-08-28 23:46:12
【问题描述】:

我有一个脚本 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\*.*

【问题讨论】:

  • 你想从那个目录复制项目吗?你的最终目标是什么?

标签: powershell powershell-3.0


【解决方案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

【讨论】:

    【解决方案2】:

    您将不得不循环查找所有文件并复制每个文件。我建议这样做:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-28
      • 2013-04-01
      • 2011-11-08
      • 2020-02-17
      • 2015-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多