我有一些Powershell代码,可以通过Azure自动化将数据库拖放到SQL Server托管实例上。该代码可以在Windows Powershell ISE中正常运行。
#Type the Managed instance admin login
$username = "uname"
#Type the Managed instance admin password
$password = 'pwd'
#Type the Full Managed instance name
$managedInstance = "sql-srvr_mangedinstance.database.windows.net"
#Leave this parameter as is
$database = "master"
#Drop Database before restore
Invoke-Sqlcmd -ServerInstance $managedInstance -Database $database -Username $username -Password $password -Query "DROP DATABASE [DB_NAME]"
Write "Dropped Database DB_NAME"
不幸的是,当我通过Azure上的Runbook运行它时,它给了我这个错误。
Invoke-Sqlcmd:建立与SQL Server的连接时发生与网络相关或特定于实例的错误。 服务器未找到或无法访问。验证实例名称正确并且SQL Server是 配置为允许远程连接。 (提供者:TCP提供程序,错误:0-连接尝试失败,因为 一段时间后,连接方未正确响应,或者由于建立连接而建立的连接失败 主机未能响应。) 在第32行:char:1 +调用Sqlcmd -ServerInstance $ managedInstance -Database $ database -U ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:InvalidOperation:(:) [Invoke-Sqlcmd],SqlException + FullyQualifiedErrorId:SqlExceptionError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand 调用Sqlcmd: 在第32行:char:1 +调用Sqlcmd -ServerInstance $ managedInstance -Database $ database -U ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:ParserError:(:) [Invoke-Sqlcmd],ParserException + FullyQualifiedErrorId:ExecutionFailureException,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand
通过将服务器连接更改为“ Server = tcp:managedinstancename.database.windows.net:1433”,我已经看到了解决方法,但这似乎更适合于vm上的Sql Server DB。即使该模块已经在runbooks模块刀片中,我也在代码的顶部包括了Import-Module Sqlserver命令。使用SSMS连接到实例,我可以确认已选中“允许远程连接”框。
答案 0 :(得分:0)
您可以使用最新的Azure Rm SQL Commandlet并删除数据库。
# Get the current context of the account which can perform operations on the database server
$Conn = Get-AutomationConnection -Name AzureRunAsConnection
Add-AzureRMAccount -ServicePrincipal -Tenant $Conn.TenantID -ApplicationId $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint
Set-AzureRmContext -SubscriptionId $conn.SubscriptionId -ErrorAction Stop | Write-Verbose
# assign resourcegroup, database server and database names
$rgname = "rgname"
$dbservername = "servername"
$dbname = "dbname"
if(Get-AzureRmSqlDatabase -ResourceGroupName $rgname -ServerName $dbservername -DatabaseName $dbname)
{
Try{
Remove-AzureRmSqlDatabase -ResourceGroupName $rgname -ServerName $dbservername -DatabaseName $dbname -Force
Write-Output "Dropped Database $database"
}Catch{
$errorMessage = $_.Exception.Message
Write-Output $errorMessage
Write-Output 'Failed to delete database'
}
}
希望这会有所帮助。