有没有办法调用Backup-SqlDatabase cmdlet,但是它是否使用SQL Server凭据连接到SQL Server 2012?
我目前使用的命令是:
Backup-SqlDatabase -ServerInstance $serverName -Database $sqldbname -BackupFile "$($backupFolder)$($dbname)_db_$($addinionToName).bak"
但这取决于正在调用它的用户,默认情况下,正在使用Windows身份验证。
答案 0 :(得分:11)
backup-sqldatabase cmdlet支持Credential参数。如果查看cmdlet的帮助,甚至还有一个示例(来自help backup-sqldatabase -full):
-------------------------- EXAMPLE 4 --------------------------
C:\PS>Backup-SqlDatabase -ServerInstance Computer\Instance -Database MyDB -Credential (Get-Credential sa)
Description
-----------
This command creates a complete database backup of the database 'MyDB', using the sa SQL Server credential. This co
mmand will prompt you for a password to complete SQL Server authentication.
答案 1 :(得分:3)
在运行Backup-SqlDatabase
$backupFolder = '...'
$additionToName = '...'
$user = 'Username'
$pass = 'Password'
$inst = 'Server\Instance'
$db = 'master'
$file = "$backupFolder${db}_db_$additionToName.bak"
$root = "SQLSERVER:\SQL\$inst"
$drv = 'sqldrive'
$cred = New-Object Management.Automation.PSCredential -ArgumentList $user, $pass
New-PSDrive $drv -PSProvider SqlServer -Root $root -Credential $cred -Scope 1
Set-Location $drv
Backup-SqlDatabase -ServerInstance $inst -Database $db -BackupFile $file
或者您可以通过SQL statement运行Invoke-SqlCmd
来备份数据库:
$backupFolder = '...'
$additionToName = '...'
$user = 'Username'
$pass = ConvertTo-SecureString 'Password' -AsPlainText -Force
$inst = 'Server\Instance'
$db = 'master'
$file = "$backupFolder${db}_db_$additionToName.bak"
$sql = @"
USE $db;
GO
BACKUP DATABASE $db TO DISK = '$file';
GO
"@
Invoke-Sqlcmd -Query $sql -ServerInstance $inst –Username $user –Password $pass