2017-10-03 4 views
0

によって定義された-Destinationパラメータで失敗し、私は私が2歳以上であり、新しいルートフォルダにその親ディレクトリをコピーするすべてのファイルをアーカイブすることができますPowerShellスクリプトを開発しようとしています。私はまた、アーカイブが完了した後、元のファイルと空のディレクトリを削除したいと思います。 コピー項目はスクリプトブロック

私は、現時点では、テスト・スクリプトから呼び出され、私は最初の部分(ファイルおよび親ディレクトリの移動)を行うことを許可する必要があり、以下の機能を持っており、それはエラーで失敗している:

コピー項目:引数がスクリプトブロックとして指定され、入力がないため、パラメータ 'Destination'を評価できません。スクリプトブロックは入力なしで評価することはできません。 C:¥Users¥cfire¥Documents¥WindowsPowerShell¥Modules¥ShareMigration¥ShareMigration.psm1:99 char:43 +コピーアイテム - フォース - デスティネーション{ +〜 + CategoryInfo:MetadataError :(:) [項目]、ParameterBindingException + FullyQualifiedErrorId:ここScriptBlockArgumentNoInput、Microsoft.PowerShell.Commands.CopyItemCommand

の関数である:

機能ArchiveFiles {[CmdletBinding()]

Param (
    [Parameter(Mandatory=$True)][string]$SourceDirectory, 
    [Parameter(Mandatory=$True)][string]$DestinationDirectory, 
    [Parameter(Mandatory=$True)][ValidateSet('AddMinutes','AddHours','AddDays','AddMonths','AddYears')][string]$TimeUnit, 
    [Parameter(Mandatory=$True)][int]$TimeLength 
) 
Begin { 
    Write-Host "Archiving files..." -ForegroundColor Yellow -BackgroundColor DarkGreen 
} 
Process { 
    $Now = Get-Date 
    $LastWrite = $Now.$TimeUnit(-$TimeLength) 

    $Items = Get-ChildItem -Path $SourceDirectory -Recurse | where { $_.LastWriteTime -lt "$LastWrite" } 

    ForEach($Item in $Items) { 
     Copy-Item -Force -Destination { 
      If ($_.PSIsContainer) { 
       If (!(Test-Path -Path $_.Parent.FullName)) { 
        New-Item -Force -ItemType Directory -Path 
        (
         Join-Path $DestinationDirectory $_.Parent.FullName.Substring($SourceDirectory.length) 
        ) 
       } 
       Else { 
        Join-Path $DestinationDirectory $_.Parent.FullName.Substring($SourceDirectory.length) 
       } 
      } 
      Else { 
       Join-Path $DestinationDirectory $_.FullName.Substring($SourceDirectory.length) 
      } 
     } 
    } 
} 
End { 
    Write-Host "Archiving has finished." -ForegroundColor Yellow -BackgroundColor DarkGreen 
} 

Join-Pathの結果を-Destinationパラメータに渡すと、トリックは実行されますが、再生されていないように思えました。パスごとに新しいアイテムを作成する必要がありますか? Powershellの新種なので、これはちょっと残念です。私は建設的な批判と解決策に感謝します。

ありがとうございます!

+0

を使用している方がはるかに簡単

を取ることに役立つ可能性がさまざまなオプションの多くを持っています。なぜあなたはjoin-pathコマンドのコピー項目infrontを使わないのですか? – guiwhatsthat

+0

これは良い考えです。私はそれを試してみましょう。 –

+0

なぜRobocopyを使用しないのですか? – ArcSet

答えて

1

あなたが探しているものをachiveするRobocopyをを使用することができます。

Source : What to copy

Destination : Where to put copy

Days : How old should the last access time for file be before copy

RemoveOldFiles : Delete Files and folders from source that were copied over.

Robocopyのは、私がスクリプトブロックにコピー項目をやったことがないあなたは、これがこのケースでは、

/MINLAD : Get files older then Last Access Date

/e : Copy sub directories even empty ones

/mov : Move files instead of copying them

Function ArchiveFileSystem([string]$Source, [string]$Destination, [int]$Days, [switch]$RemoveOldFiles){ 
    $LastWrite = Get-date (Get-Date).AddDays(-$Days) -Format yyyyMMdd 
    robocopy $Source $Destination /MINLAD:$LastWrite /e (&{if($RemoveOldFiles -eq $true){'/mov'}}) 
} 
ArchiveFileSystem -Source C:\TestSource -Destination C:\TestDestination -Days 1 -RemoveOldFiles 
+0

はどうもありがとうございました。これは私がしようとしていたものよりもはるかに簡単なアプローチです。このソリューションは、私にとって大きな動作しているようです目的。もう一度ありがとう! –