2016-08-17 8 views
0

シンプルなpowershellスクリプトを作成して、共有コンピュータのある場所から多くの場所にファイルやフォルダをコピーしましたが、この作業はうまくいっていますスクリプト次回の実行は、両親を、問題をコピーして、書き込みexistenコンテンツPowershellがフォルダとファイルをコピーして親フォルダをサブフォルダとして再作成

オーバーする週作成されexampelのためのサブフォルダと同じ親agian内のフォルダ:

Folder1には、(次回のスクリプトの実行をコピーまたはフォルダが存在する場合)このように見える

フォルダ1 \フォルダ1

enter image description here enter image description here

あなたは写真で正しくこのコピーフォルダとコンテンツが、フォルダが存在しないか、これは同じ名前で同じフォルダ内に新しいサブフォルダを作成する場合はここで

があると見ることができるようにレビューされるスクリプトのコピー。

Clear-Host 
#source path 
$sourceDir = '\\source\Users\asdew\Desktop\dep' 

#Array with Sites Path 
$targetDir = @(

       #Sites in pc1 
       '\\Apc20\Users\afded\Desktop\site01', 

       #Sites in pc2 

       '\\Apc10\Users\adsed\Desktop\site02' 

       ) 

$i=0 

Foreach ($dest in $targetDir){ 

Get-ChildItem $sourceDir -Recurse | % { 

    $dest = $targetDir[$i] + $_.FullName.SubString($sourceDir.Length) 

    If (!($dest.Contains('.')) -and !(Test-Path $dest)) 

    { 
     mkdir $dest 
    } 



     #Copy Files and over write existen files. 
    Write-Host "copy start. Please wait..We are copying your files.." -ForegroundColor DarkYellow 

    Copy-Item $_.FullName -Destination $dest -Force -PassThru -ErrorAction SilentlyContinue 

    If(-not $?) 

    {Write-Warning "Your copy Failed!!" -InformationAction} 

    else 
    {Write-Host "Your copy was Success!" -ForegroundColor Green} 


    } 

    $i = $i+1 
} 

私はこの問題を解決するのに役立つことを願っています。 よろしくお願いします。

答えて

0

私はコピーアイテムがすべての仕事をするべきだと思います。次のコードを試してください

Clear-Host 
#source path 
$sourceDir = '\\source\Users\asdew\Desktop\dep' 
#Array with Sites Path 
$targetDir = @(

      #Sites in pc1 
      '\\Apc20\Users\afded\Desktop\site01', 

      #Sites in pc2 

      '\\Apc10\Users\adsed\Desktop\site02' 

      ) 


Foreach ($dest in $targetDir){ 
    Get-ChildItem $sourceDir | Copy-Item -Recurse -Destination $dest -ErrorAction Continue -Force 
} 
関連する問題