我正在尝试使用Azure自动化操作手册对Azure SQL数据库运行一些SQL语句。 在我可以在https://docs.microsoft.com上find的所有示例中,他们都使用自动化凭据和连接string,如下面的代码所示:
$SqlCredential = Get-AutomationPSCredential -Name $SqlCredentialAsset # Get the username and password from the SQL Credential $SqlUsername = $SqlCredential.UserName $SqlPass = $SqlCredential.GetNetworkCredential().Password # Define the connection to the SQL Database $Conn = New-Object System.Data.SqlClient.SqlConnection("Server=tcp:$SqlServer,$SqlServerPort;Database=$Database;User ID=$SqlUsername;Password=$SqlPass;Trusted_Connection=False;Encrypt=True;")
很简单,但是请求会在我的Azure SQL防火墙中停止,因为我不知道请求将来自哪个IP地址!
我如何允许我的PowerShell Runbook对Azure SQL数据库进行身份validation和运行SQL命令,而不启用“允许访问Azure服务”(checkbox在下面的屏幕截图中看到)( 这将允许所有资源在Azure上,而不仅限于我订阅 )
MS站点上提供的示例假定您已启用“允许访问Azure服务”选项。 如果您不想这样做,那么您将需要configuration您的脚本以确定运行时的IP地址,并将其作为脚本的一部分添加到SQL防火墙规则中,否则您需要查看使用混合自动化工作者 ,你可以放在你自己的vnet并分配一个静态IP。
如果你想在脚本中添加你的IP,你可以这样做:
$response = Invoke-WebRequest ifconfig.co/ip $ip = $response.Content.Trim() New-AzureSqlDatabaseServerFirewallRule -StartIPAddress $ip -EndIPAddress $ip -RuleName <Name of Rule> -ServerName <your database server name here>