Search code examples
powershellredgate

How to create dynamic powershell query?


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.