How to create dynamic powershell query?
-
16-04-2021 - |
Question
I have a Powershell query to get the backup of my database. But i need to have the backup the data with different names like DATABASEBACKUP_CURRENTDATE. How can i achieve this?
./RedGate.SQLAzureBackupCommandLine.exe /as: AzureServerNAME/ad: AzureDatabaseName /au:AzureUserName /ap:AzurePassword /cc /s /ls:. /ld:LOCALDATABASENAME_CURRENTDATE /dl /v /ba
Solution
First, build a string that contains the current date:
$backupName = $("Backup-{0}" -f [DateTime]::Now.ToString("yyyy-MM-dd"))
Then call RedGate and use the date string as a parameter:
./RedGate.SQLAzureBackupCommandLine.exe /as: AzureServerNAME /ad: AzureDatabaseName /au:AzureUserName /ap:AzurePassword /cc /s /ls:. /ld:$backupName /dl /v /ba
Ed: Copypaste/markup messed with brackets and colons, fixed 'em.
OTHER TIPS
To get a string constructed with the current date, you can simply do:
"DATABASEBACKUP_{0:MM-dd-yyyy}" -f [DateTime]::Now
It will give something like DATABASEBACKUP_01-12-2012
. You can use that as arguments as needed
Throwing in another solution...
"DATABASEBACKUP_$(Get-Date -Format MM-dd-yyyy)"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow