2017-05-11 37 views
0

このスクリプト(バージョン1)は、ISEでデバッグする場合にのみ動作します。しかし、それを実行すると、oldFilesPath内のファイルを削除して新しいファイルをbackupPathからそこにコピーすることはありません。私は配管のようなRemove-Itemで他の方法を試しました。デルはまた働きません。今私はいくつかの実験を行い、2番目のバージョン(バージョン2)を書きました。私は自分の操作を関数に入れ、同じ予期しない動作をします。しかし、私が2つの機能の間で例えば60秒間休止すると、スクリプトは私の場合にはうまくいく。だから、私はpowershellは、このような実行をうまく管理していないと思う..私は知らない。多分あなたの誰かがそれについての説明をしていますか?Powershellスクリプトはデバッガで動作しますが、通常実行時(ISE)では動作しません

バージョン1

function createIfDontExist ($directory) 
{ 
    if (-not (Test-Path -Path "$directory" -PathType Container)) { 
     New-Item -Path "$directory" -ItemType directory 
    } 
} 

#current user 
$user="$env:username" 

#Driveletter of the backup drive 
$driveLetter="D:" 

#date from today 
$currentDate = Get-Date 

################################################################################################################ 
#if no HDD detected, error message appears           
$backupDrive = Test-Path "$driveLetter\" 
if($backupDrive -eq $false) { 
    "Backup-HDD not connected!" | Out-File "C:\Users\$user\AppData\Local\hddTest.txt" 
    #"$currentDate : Backup failed!" | Out-File "C:\Users\$user\AppData\Local\outlookBackup.txt" -Append 
exit 
} 
else { 
    "Backup-HDD connected." | Out-File "C:\Users\$user\AppData\Local\hddTest.txt" 
    #"$currentDate : Backup success." | Out-File "C:\Users\$user\AppData\Local\outlookBackup.txt" -Append 

    #path where the .pst file is located            
    $sourcePath = "C:\Users\$user\Documents\Outlook\" 

    #location where the .pst-file should be backed up 
    $backupPath = "$driveLetter\Outlook-Backup\$user" 

    #location for old .pst-file 
    $oldFilesPath = "$driveLetter\Outlook-Backup\Old-Files\$user" 

    #date 7 days ago 
    $deleteDate = $currentDate.AddDays(-7) 

    #Creates the Outlook-Backup and Old-Files-Path if they don´t exist 
    createIfDontExist($backupPath) 
    createIfDontExist($oldFilesPath) 

    #gets non directory files from Outlook-Backup 
    $filesInBackupPath = Get-ChildItem $backupPath 

    #Checks if files in Backup-Path are there longer than seven days since the last backup. 
    #Files older than seven days will be moved to the Old-Files-Path and replace their older copies 
    $filesInBackupPath | foreach { 
     if($_.CreationTime -lt $deleteDate) 
     { 
      if(Test-Path -Path "$oldFilesPath\$_" -PathType Leaf){ 
       Remove-Item -Path $oldFilesPath\$_ -Force 
      } 
      Move-Item $_.FullName $oldFilesPath -Force 
     } 

    } 

    #gets non directory files from Source 
    $filesInSourcePath = Get-ChildItem $sourcePath 

    #Checks if Backup-Path contains same files like Source-Path and copies them possibly from Source-Path to Backup-Path 
    $filesInSourcePath | foreach { 
     if(-not (Test-Path -Path "$backupPath\$_" -PathType Leaf)) 
     { 
      Copy-Item $_.FullName "$backupPath" 
     } 

    } 
} 

バージョン2

function createIfDontExist ($directory) 
{ 
    if (-not (Test-Path -Path "$directory" -PathType Container)) { 
     New-Item -Path "$directory" -ItemType directory; 
    } 
} 

#Checks if there are files in the Backup-Path which are 7 days old and deletes identically named files in the Old-Files-Path. 
function removeOldFiles($folders) 
{ 
    $backupidx = 0; 
    $oldfilesidx = 1; 
    $backupfolder = $folders[$backupidx]; 
    $oldfilesfolder = $folders[$oldfilesidx]; 
    ForEach ($backupFile in (Get-ChildItem -Path $backupfolder)) { 
     if($backupFile.CreationTime -lt $deleteDate) 
     { 
      if(Test-Path -Path "$oldfilesfolder\$($backupFile.Name)" -PathType Leaf){ 
       $file = Get-ChildItem -Path "$oldfilesfolder\$($backupFile.Name)"; 
       $file.Delete(); 
      } 
     } 
    } 
    return; 
} 

#Checks if there are files in the Backup-Path which are 7 days old and moves them into the Old-Files-Path. 
function moveBackupFilesToOldPath($folders) 
{ 
    $backupidx = 0; 
    $oldfilesidx = 1; 
    $backupfolder = $folders[$backupidx]; 
    $oldfilesfolder = $folders[$oldfilesidx]; 
    ForEach ($backupFile in (Get-ChildItem -Path $backupfolder)) { 
     if($backupFile.CreationTime -lt $deleteDate) 
     { 
      $backupFile.MoveTo("$oldfilesfolder\$($backupFile.Name)"); 
     } 
    } 
    return; 
} 

#Checks if Backup-Path contains same files like Source-Path and copies them possibly from Source-Path to Backup-Path. 
function syncFolder($folders) 
{ 
    $sourceidx = 0; 
    $destinationidx = 1; 
    $sourcefolder = $folders[$sourceidx]; 
    $destinationfolder = $folders[$destinationidx]; 
    ForEach ($sourceFile in (Get-ChildItem -Path $sourcefolder)) { 
     if(-not (Test-Path -Path "$destinationfolder\$($sourceFile.Name)" -PathType Leaf)) 
     { 
      $sourceFile.CopyTo("$destinationFolder\$($sourceFile.Name)"); 
     } 
    } 
    return; 
} 

#current user 
$user="$env:username" 

#Driveletter of the backup drive 
$driveLetter="D:" 

#date from today 
$currentDate = Get-Date 

################################################################################################################ 
#if no HDD detected, error message appears           
$backupDrive = Test-Path "$driveLetter\" 
if($backupDrive -eq $false) { 
    "Backup-HDD not connected!" | Out-File "C:\Users\$user\AppData\Local\festplattenTest.txt" 
    #"$currentDate : Backup failed!" | Out-File "C:\Users\$user\AppData\Local\outlookBackup.txt" -Append 
    exit 
} 
else { 
    "Backup-HDD connected." | Out-File "C:\Users\$user\AppData\Local\hddTest.txt" 
    #"$currentDate : Backup success." | Out-File "C:\Users\$user\AppData\Local\outlookBackup.txt" -Append 

    #path where the .pst file is located            
    $sourcePath = "C:\Users\$user\Documents\Outlook-Dateien"; 

    #location where the .pst-file should be backed up 
    $backupPath = "$driveLetter\Outlook-Backup\$user"; 

    #location for old .pst-file 
    $oldFilesPath = "$driveLetter\Outlook-Backup\Old-Files\$user"; 

    #date 7 days ago 
    $deleteDate = $currentDate.AddDays(-7); 

    #Creates the Outlook-Backup and Old-Files-Path if they don´t exist 
    createIfDontExist($backupPath); 
    createIfDontExist($oldFilesPath); 

    removeOldFiles($backupPath, $oldFilesPath); 
    Start-Sleep -Seconds 60; 
    moveBackupFilesToOldPath($backupPath, $oldFilesPath); 
    Start-Sleep -Seconds 60; 
    syncFolder($sourcePath, $backupPath); 
    return; 
} 

答えて

0

は、あなたが実際にISEでデバッガと通じステッピングか、単にISEでスクリプトを実行している意味ですか。後者の場合、アクセスしようとしているディレクトリに対してアクセス権の問題がある可能性があります。 ISEでスクリプトを実行すると、ユーザーの資格情報でスクリプトが実行されます。

+1

私はデバッガをステップ実行すると、期待どおりに動作しますが、スクリプトを実行するだけで予期しない動作が発生します。私は管理者としてログインしています。また、実行ポリシーを無制限に設定し、ISEを管理者として実行しました。それはまだトリックをしません。 – Mehkir

+0

スクリプトを削除しようとすると、どのようなエラーが発生しますか?私もwrite-host をあなたのifループのそれぞれに入れます。私はあなたの日付テストロジックが動作していないと思います。 – fampop

+0

エラーはありません。実行時にMove-Item、Remove-Item、Copy-Itemなどの1つの命令でスクリプトを実行すると動作します。たとえば:私がその順序でコメントし、すべてのコメントの後にスクリプトを実行すると、期待どおりに動作します。 1)コメント - >#Remove-Item、#Move-Item、Copy-Item - Run。 2)コメント - >#Remove-Item、Move-Item、#Copy-Item。 - 実行します。 3)コメント - >削除 - 項目、#移動項目、#コピー項目。 - 実行します。すべて順調。正常に実行され、何も起こりません。 – Mehkir

0

これはデバッグ時でもどのように機能しますか?あなたのTest-Pathコマンドがtrueを返すことはありません

"$oldFilesPath\$_" 

は、あなたが仕事に、このために

"$oldFilesPath\$($_.Name)" 

にそれを変更する必要が

D:\Outlook-Backup\Old-Files\$user\D:\Outlook-Backup\$user\<filename> 

と同じであるため。スクリプトの終わり近くに"$backupPath\$_"と同じこと。

+0

私はそれを試してみましたが、違いはありません。また、Move-ItemとCopy-Item命令をコメントすると、Remove-Item命令が有効になることもわかります。複数の指示が手順を邪魔するようです...その非常に興味深いものです。 – Mehkir

関連する問題