2017-05-11 60 views
0

此腳本(版本1)只在我在ISE中調試時才起作用。但是,如果執行它,它不會刪除oldFilesPath中的文件,以將新路徑從backupPath複製到那裏。我嘗試使用Remove-Item類似管道的其他方法。德爾也不會工作。現在我做了一些實驗並寫了第二個版本(版本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

如果我通過調試程序,它像預期的那樣工作,但只是運行腳本意外運行。我以Admin身份登錄。另外我將執行策略設置爲不受限制並以管理員身份運行ISE。它仍然沒有辦法。 – Mehkir

+0

當試圖刪除腳本時會拋出什麼錯誤。我也會把寫主機放在你的每個循環中,我認爲你的日期測試邏輯不起作用, – fampop

+0

沒有任何錯誤。如果我在一次運行中運行帶有Move-Item,Remove-Item和Copy-Item之類的一條指令的腳本,它就可以工作。例如:如果我按照該順序發表評論,並且在每一條評論之後,我都會運行該腳本,它就像預期的那樣工作1)註釋 - >#刪除項目,#移動項目,複製項目 - 運行。 2)註釋 - >#刪除項目,移動項目,#複製項目。 - 跑。 3)註釋 - >刪除項目,#移動項目,#複製項目。 - 跑。一切安好。正常運行,沒有任何反應。 – Mehkir

0

即使在調試時,這可能如何工作?你Test-Path命令不應該返回true,因爲

"$oldFilesPath\$_" 

相同

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

您需要將其更改爲

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

這個工作。與腳本結尾處的"$backupPath\$_"相同。

+0

我試了一下,但它沒有任何區別。我還發現,如果我評論Move-Item和Copy-Item指令,Remove-Item指令將生效。似乎多個指令會擾亂程序......它非常好奇。 – Mehkir

相關問題