此脚本(版本1)仅在我在ISE中调试时才有效。但是如果执行它,它将不会删除oldFilesPath中的文件以将新的文件从backupPath复制到那里。我尝试了其他方式使用Remove-Item之类的管道。德尔也不会工作。现在我做了一些实验并写了第二个版本(版本2)。我将我的操作放入函数中并获得相同的意外行为。但是,如果我在两个函数之间暂停60秒,那么脚本在我的情况下运行良好。所以我认为powershell不能很好地管理执行或类似的事情......我不知道。也许你们中有人对此有解释?
版本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 :(得分:0)
你的意思是实际上是在ISE中使用调试器,或者只是在ISE中运行脚本。如果是后者,您可能会在您尝试访问的目录上遇到权限问题。在ISE中运行脚本会使用您的用户凭据运行它。
答案 1 :(得分:0)
即使在调试中,这可能如何工作?您的Test-Path
命令永远不会返回true,因为
"$oldFilesPath\$_"
与
相同D:\Outlook-Backup\Old-Files\$user\D:\Outlook-Backup\$user\<filename>
您需要将其更改为
"$oldFilesPath\$($_.Name)"
为此工作。与脚本末尾附近的"$backupPath\$_"
相同。
答案 2 :(得分:0)
我遇到了类似的问题,其中Powershell脚本在调试器中工作,或者在控制台上键入每个命令,但不是在正常运行(ISE)或调用脚本。
我意识到我正在使用一个没有时间在正常运行时执行的异步方法(这太快了)。为了测试,我在这里和那里引入了一些timeouts,代码按预期运行。
然后,这只是识别异步方法并在继续之前等待完成的问题。