Runbook Powershell Invoke-Sqlcmd SQL 服务器 MI 连接
Runbook Powershell Invoke-Sqlcmd SQL Server MI connection
我有一些 Powershell 代码可以将数据库放到 SQL 服务器托管实例上,我想通过 Azure Automation 安排它。代码 运行 在 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 上的 Runbooks 运行 它时,它给了我这个错误。
Invoke-Sqlcmd : A network-related or instance-specific error occurred while establishing a connection to SQL Server.
The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is
configured to allow remote connections. (provider: TCP Provider, error: 0 - A connection attempt failed because the
connected party did not properly respond after a period of time, or established connection failed because connected
host has failed to respond.)
At line:32 char:1
+ Invoke-Sqlcmd -ServerInstance $managedInstance -Database $database -U ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Invoke-Sqlcmd], SqlException
+ FullyQualifiedErrorId : SqlExceptionError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand
Invoke-Sqlcmd :
At line:32 char:1
+ Invoke-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" 来解决此问题,但这似乎更倾向于虚拟机上的 Sql 服务器数据库。我还在我的代码顶部包含了 Import-Module Sqlserver 命令,即使这个模块已经在 运行books 模块 blade 中。使用 SSMS 连接到实例我可以确认勾选了允许远程连接框。
您可以使用最新的 Azure Rm SQL 命令行开关并删除数据库。
# 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'
}
}
希望对您有所帮助。
我有一些 Powershell 代码可以将数据库放到 SQL 服务器托管实例上,我想通过 Azure Automation 安排它。代码 运行 在 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 上的 Runbooks 运行 它时,它给了我这个错误。
Invoke-Sqlcmd : A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 0 - A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.) At line:32 char:1 + Invoke-Sqlcmd -ServerInstance $managedInstance -Database $database -U ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [Invoke-Sqlcmd], SqlException + FullyQualifiedErrorId : SqlExceptionError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand Invoke-Sqlcmd : At line:32 char:1 + Invoke-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" 来解决此问题,但这似乎更倾向于虚拟机上的 Sql 服务器数据库。我还在我的代码顶部包含了 Import-Module Sqlserver 命令,即使这个模块已经在 运行books 模块 blade 中。使用 SSMS 连接到实例我可以确认勾选了允许远程连接框。
您可以使用最新的 Azure Rm SQL 命令行开关并删除数据库。
# 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'
}
}
希望对您有所帮助。