此腳本(版本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;
}
如果我通過調試程序,它像預期的那樣工作,但只是運行腳本意外運行。我以Admin身份登錄。另外我將執行策略設置爲不受限制並以管理員身份運行ISE。它仍然沒有辦法。 – Mehkir
當試圖刪除腳本時會拋出什麼錯誤。我也會把寫主機放在你的每個循環中,我認爲你的日期測試邏輯不起作用, –
fampop
沒有任何錯誤。如果我在一次運行中運行帶有Move-Item,Remove-Item和Copy-Item之類的一條指令的腳本,它就可以工作。例如:如果我按照該順序發表評論,並且在每一條評論之後,我都會運行該腳本,它就像預期的那樣工作1)註釋 - >#刪除項目,#移動項目,複製項目 - 運行。 2)註釋 - >#刪除項目,移動項目,#複製項目。 - 跑。 3)註釋 - >刪除項目,#移動項目,#複製項目。 - 跑。一切安好。正常運行,沒有任何反應。 – Mehkir