Mehkir
Mehkir

Reputation: 11

Powershell Script works in Debugger but not at normal run(ISE)

This script(version 1) works only if i debug it in ISE. But if execute it, it wont Remove files in the oldFilesPath to copy the new one from backupPath to there. I tried other ways with Remove-Item like piping. del wont work also. Now i did some experiments and wrote a second version(version 2). I put my operations into functions and get the same unexpected behaviour. But if i pause for example 60 seconds between two functions the script runs well in my case. So i think that powershell doesn´t manage the execution well or something like this.. i don´t know. Maybe anyone of you have an explanation for that?

Version 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"
        }

    }
}

Version 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;
}

Upvotes: 0

Views: 1475

Answers (3)

user6580974
user6580974

Reputation:

I had a similar issue where the Powershell Script works in Debugger or by typing each command on the console, but not at normal run(ISE) or invoking the script.

I realized that I was using an asynch method that did not have time to execute on the normal run (that was too fast). To test, I introduced some timeouts here and there and the code ran as expected.

Then it was just a matter of identifying the async method and waiting while it completed before continuing.

Upvotes: 1

TheMadTechnician
TheMadTechnician

Reputation: 36287

How does this possibly work, even in debug? Your Test-Path commands should never return true because

"$oldFilesPath\$_" 

is the same as

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

You need to change it to

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

for this to work. Same thing with "$backupPath\$_" near the end of the script.

Upvotes: 0

fampop
fampop

Reputation: 78

Do you mean actually stepping through with the debugger in ISE or just running the script in ISE. If the latter you might have a permissions issue on the directory you are trying to access. Running the script in ISE runs it with your user credentials.

Upvotes: 0

Related Questions