1
votes

I've been trying to create a powershell script to create an Azuer firewall rule on the fly.

The code I'm using is:

$servername = "xxx" 
$username = "[email protected]" 
$password = "xxx" 
$subscription = "Pay-As-You-Go" 
$secpasswd = ConvertTo-SecureString 
$password -AsPlainText -Force $cred = New-Object System.Management.Automation.PSCredential ($userName, $secpasswd) 

Login-AzureRmAccount -Credential $cred -SubscriptionName $subscription

Select-AzureSubscription -SubscriptionName $subscription -Default

$ipAddress = (Invoke-WebRequest 'http://myexternalip.com/raw').Content -replace "`n"

New-AzureSqlDatabaseServerFirewallRule -ServerName $servername -RuleName 'Test' -StartIpAddress $ipAddress -EndIpAddress $ipAddress

I also get the following errors:

Select-AzureSubscription : The subscription name Pay-As-You-Go doesn't exist.

and

New-AzureSqlDatabaseServerFirewallRule : No default subscription has been designated. Use Select-AzureSubscription -Default to set the default subscription.

I'm also tried changing the Login-AzureRmAccount to Add-AzureAccount but it didn't make any difference. The user I created is an owner of the SQL server.

Is there anything else I might be missing?

1

1 Answers

1
votes

It looks like you're mixing AzureRM and Azure modes... You need to make sure the subscription you want is accessible from that mode. If you login with Login-AzureRMAccount or Add-AzureRMAccount - use Get-AzureRMSubscription to see what you have access to.

If you login with Add-AzureAccount, use Get-AzureSubscription...

Check that and see what you find.