How to create dynamic powershell query?

374 views Asked by At

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
3

There are 3 answers

1
vonPryz On BEST ANSWER

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.

0
manojlds On

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

0
Tom On

Throwing in another solution...

"DATABASEBACKUP_$(Get-Date -Format MM-dd-yyyy)"