Powershell backup-sqldatabase -ExpirationDate

ti1 embracon 0 Reputation points
2023-01-25T15:28:27.3733333+00:00

Greetings!

I'm just having a bad time trying to set the expiration date to a SQL Server backup through powershell SQLSever Module.

My need is that the next backup overwrites the current file in the directory.

I've Tried to achieve that with:

$Expira = (get-date).AddDays(1)

Backup-SqlDatabase -ServerInstance $Instancia -Database $Banco -BackupFile $NomeArquivo -Credential $Credential -BackupAction Database -CompressionOption On -ExpirationDate $Expira -Initialize

It returns 'invalid value to EXPIREDATE param'.

I've tried so many ways of date formatting and can't find anything usefull in forums. The documentation also doesn't presents any example and just says the value for 'ExpirationDate' is a <DateTime>.

The command without -ExpirationDate param works just fine.

PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,595 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Andreas Baumgarten 111.3K Reputation points MVP
    2023-01-25T16:24:13.1+00:00

    Hi @ti1 embracon ,

    with the (get-date).AddDays(1) you get the date and time informationen.

    I am not 100% sure if Backup-SqlDatabase is able to work with the time information.

    You can try this to get the date information only:

    $Expira = (get-date).AddDays(1).Date
    Backup-SqlDatabase -ServerInstance $Instancia -Database $Banco -BackupFile $NomeArquivo -Credential $Credential -BackupAction Database -CompressionOption On -ExpirationDate $Expira -Initialize
    

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards

    Andreas Baumgarten


  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.