Simple Post-Backup Script Not Working


Author
Message
tony_mswf
tony_mswf
New Member
New Member (12 reputation)New Member (12 reputation)New Member (12 reputation)New Member (12 reputation)New Member (12 reputation)New Member (12 reputation)New Member (12 reputation)New Member (12 reputation)New Member (12 reputation)New Member (12 reputation)
Group: Forum Members
Posts: 8, Visits: 13
I added a script to run when the main Macrium Backup script is executed and completed.
The purpose of that script is to change the name of the folder in which the backup files are placed.
That script runs as a standalone item but doesn't run properly when called from the main Macrium Backup script, all that happens is that Notepad opens and presents the script in Notepad.
Any thoughts?

Here is the standalone script that is called:
$folderName = (Get-Date).tostring("Backup-yyyy-dd-MM-HH-mm-ss")
Rename-Item "J:\WIndows-PC.Data-Image\Backup” $folderName

Here it is the relevany content in the Macrium Backup script.

function Main()
{
Write-Host 'PowerShell script for Macrium Reflect Backup Definition File';
Write-Host "BDF: $strXmlFilePath";
Elevate;

$iExitCode = Backup;

Write-Host ' * Executing "at end" program... ' -NoNewLine;
$strRunAtEndApp = 'D:\Windows-PC.Data-Image Script.ps1';
$strRunAtEndArgs = '';
if ([string]::IsNullOrEmpty($strRunAtEndArgs))
{
  Start-Process -FilePath $strRunAtEndApp;
}
else
{
  Start-Process -FilePath $strRunAtEndApp -ArgumentList $strRunAtEndArgs;
}
Write-Host 'Done.';
Write-Host "Script finished with exit code $iExitCode.";
Exit $iExitCode;
}

Edited 12 September 2022 5:07 PM by tony_mswf
jphughan
jphughan
Macrium Evangelist
Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)
Group: Forum Members
Posts: 14K, Visits: 84K
For what are likely safety reasons, the default action when calling or double-clicking a PS1 file is simply to open it for viewing, as distinct from older script formats like BAT and VBS files that actually execute in those situations.  That change has likely saved quite a number of unintended executions from users who might have double-clicked on a PS1 file when they meant to single-click it or similar.  In any case, since a PS1 file isn't an actual application and isn't executed as its default action, you need to call PowerShell itself as the external application, which means your $strRunAtEndApp line should look like this:
$strRunAtEndApp = 'C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe';


And then your $strRunAtEndArgs line should look like this:
$strRunAtEndArgs = '-executionpolicy bypass -noprofile -nologo -file "D:\Windows-PC.Data-Image Script.ps1"';


If you wanted to be able to do this from within the script generation wizard, you would have needed to specify C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe as the application file name and then this on the Parameters line:
-executionpolicy bypass -noprofile -nologo -file "D:\Windows-PC.Data-Image Script.ps1"


Edited 13 September 2022 2:10 PM by jphughan
jphughan
jphughan
Macrium Evangelist
Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)
Group: Forum Members
Posts: 14K, Visits: 84K
Also be aware that renaming the destination folder will cause it to no longer appear automatically under Reflect's Existing Backups tab unless you manually add that new folder path to the "Folders to Search" list by clicking the Edit button in that view.  (The folder paths there are NOT searched recursively, i.e. you can't just add a parent folder and expect all backups in all subfolders to appear.)  In addition, you'll never be able to use that job to create Differential or Incremental backups because your XML definition file will continue pointing to the old folder path, which thanks to that rename won't contain any backups to which a Differential or Incremental backup could be appended.  As long as you're ok only creating Full backups AND not having any of your backups show up under Existing Backups automatically, then you should be fine.  BUT...

...if you're only creating Fulls, then have you considered the alternative of simply using Reflect's file naming parameters to incorporate the backup date into the name of the generated backup file itself?  See below.  Notice what I entered in the Backup File Name field and then the text below that field showing an example of the file name that would result.  I did go into Advanced Options and disable the Backup File Name Prefix option for this example, which should be fine if you're only making Full backups, although this setup instead of the folder rename design would make it easy to create Diff/Inc backups if you so chose.  And if you truly want the exact date/time syntax from your script, which placing the day before the month (despite the fact that this would cause sorting by name NOT to sort properly by date??), then you can even achieve that by replacing the single ISODATE parameter with the desired sequence of other more granular date parameters that are available for use in the Backup File Name field.


Edited 13 September 2022 4:16 AM by jphughan
tony_mswf
tony_mswf
New Member
New Member (12 reputation)New Member (12 reputation)New Member (12 reputation)New Member (12 reputation)New Member (12 reputation)New Member (12 reputation)New Member (12 reputation)New Member (12 reputation)New Member (12 reputation)New Member (12 reputation)
Group: Forum Members
Posts: 8, Visits: 13
jphughan - 13 September 2022 3:29 AM
For what are likely safety reasons, the default action when calling or double-clicking a PS1 file is simply to open it for viewing, as distinct from older script formats like BAT and VBS files that actually execute in those situations.  That change has likely saved quite a number of unintended executions from users who might have double-clicked on a PS1 file when they meant to single-click it or similar.  In any case, since a PS1 file isn't an actual application and isn't executed as its default action, you need to call PowerShell itself as the external application, which means your $strRunAtEndApp line should look like this:
$strRunAtEndApp = 'C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe';


And then your $strRunAtEndArgs line should look like this:
'-executionpolicy bypass -noprofile -nologo -file "D:\Windows-PC.Data-Image Script.ps1"';


If you wanted to be able to do this from within the script generation wizard, you would have needed to specify C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe as the application file name and then this on the Parameters line:
-executionpolicy bypass -noprofile -nologo -file "D:\Windows-PC.Data-Image Script.ps1"


Many thanks for that feedback, updated as per your advice, worked perfectly.
Thanks again.
tony_mswf
tony_mswf
New Member
New Member (12 reputation)New Member (12 reputation)New Member (12 reputation)New Member (12 reputation)New Member (12 reputation)New Member (12 reputation)New Member (12 reputation)New Member (12 reputation)New Member (12 reputation)New Member (12 reputation)
Group: Forum Members
Posts: 8, Visits: 13
jphughan - 13 September 2022 4:04 AM
Also be aware that renaming the destination folder will cause it to no longer appear automatically under Reflect's Existing Backups tab unless you manually add that new folder path to the "Folders to Search" list by clicking the Edit button in that view.  (The folder paths there are NOT searched recursively, i.e. you can't just add a parent folder and expect all backups in all subfolders to appear.)  In addition, you'll never be able to use that job to create Differential or Incremental backups because your XML definition file will continue pointing to the old folder path, which thanks to that rename won't contain any backups to which a Differential or Incremental backup could be appended.  As long as you're ok only creating Full backups AND not having any of your backups show up under Existing Backups automatically, then you should be fine.  BUT...

...if you're only creating Fulls, then have you considered the alternative of simply using Reflect's file naming parameters to incorporate the backup date into the name of the generated backup file itself?  See below.  Notice what I entered in the Backup File Name field and then the text below that field showing an example of the file name that would result.  I did go into Advanced Options and disable the Backup File Name Prefix option for this example, which should be fine if you're only making Full backups, although this setup instead of the folder rename design would make it easy to create Diff/Inc backups if you so chose.  And if you truly want the exact date/time syntax from your script, which placing the day before the month (despite the fact that this would cause sorting by name NOT to sort properly by date??), then you can even achieve that by replacing the single ISODATE parameter with the desired sequence of other more granular date parameters that are available for use in the Backup File Name field.


Good points. 
Just some additional information as to why I chose to update the folder as opposed to the file(s).
The images are typically 10s of GB (always Full backup) so I always split into multiple files, the reason being that the backup is in the first instance transferred to a NAS and then subsequently copied off-site.
From my pers[ective it is easier to manage time stamped folders with the underlying files rather than a folder with a common name and time stamped files. In addition, if the backup was made and put into folder "Backup" and that version was not transferred to the NAS and copied off site, then on the next scheduled backup the file "Backup" would have new image files added. The lack of folder identity then makes it more difficult to track versions in off-site copies.
Hope that makes sense.
jphughan
jphughan
Macrium Evangelist
Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)
Group: Forum Members
Posts: 14K, Visits: 84K
Great!  Glad to hear it works, and the additional background definitely makes it clearer why this appeals to you.  Enjoy your setup!

GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Reading This Topic

Login

Explore
Messages
Mentions
Search