このスクリプト(バージョン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;
}
私はデバッガをステップ実行すると、期待どおりに動作しますが、スクリプトを実行するだけで予期しない動作が発生します。私は管理者としてログインしています。また、実行ポリシーを無制限に設定し、ISEを管理者として実行しました。それはまだトリックをしません。 – Mehkir
スクリプトを削除しようとすると、どのようなエラーが発生しますか?私もwrite-hostをあなたのifループのそれぞれに入れます。私はあなたの日付テストロジックが動作していないと思います。 –
fampop
エラーはありません。実行時にMove-Item、Remove-Item、Copy-Itemなどの1つの命令でスクリプトを実行すると動作します。たとえば:私がその順序でコメントし、すべてのコメントの後にスクリプトを実行すると、期待どおりに動作します。 1)コメント - >#Remove-Item、#Move-Item、Copy-Item - Run。 2)コメント - >#Remove-Item、Move-Item、#Copy-Item。 - 実行します。 3)コメント - >削除 - 項目、#移動項目、#コピー項目。 - 実行します。すべて順調。正常に実行され、何も起こりません。 – Mehkir