简体   繁体   English

如何创建动态Powershell查询?

[英]How to create dynamic powershell query?

I have a Powershell query to get the backup of my database. 我有一个Powershell查询来获取数据库的备份。 But i need to have the backup the data with different names like DATABASEBACKUP_CURRENTDATE. 但是我需要备份具有不同名称的数据,例如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

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并将日期字符串用作参数:

./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. 埃德(Ed):Copypaste / markup用括号和冒号弄乱了,固定了'em。

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 . 它将给出类似DATABASEBACKUP_01-12-2012 You can use that as arguments as needed 您可以根据需要将其用作参数

Throwing in another solution... 抛出另一种解决方案...

"DATABASEBACKUP_$(Get-Date -Format MM-dd-yyyy)" “ DATABASEBACKUP _ $(获取日期-格式MM-dd-yyyy)”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM