Remove-AzureRmSqlDatabase keeps asking me to run LoginAzureRmAccount - azure-powershell

I have an Azure SQL database which I want to delete. The command should be:
Remove-AzureRmSqlDatabase -ResourceGroupName $dbResourceGroup -ServerName $dbServerName -DatabaseName $dbToDelete -Whatif -Force
The error I keep getting back is
Remove-AzureRmSqlDatabase : Run Login-AzureRmAccount to login.
I tried running Login-AzureRmAccount as myself, then as a service principal I use for unattended scripts, and nothing worked.
I am able to log into the Azure RM portal and delete databases. I am also able to run Invoke-SqlCmd against this database to query and manipulate data.
How can I make this work?

According to this error message, it seems that you have not login Azure whit the right subscription.
We can use this command to get the sql database's information, and check the subscription.
(Get-AzureRmSqlDatabase -DatabaseName jasontest1 -ServerName jasontest -ResourceGroupName jasontest).resourceid
Then we can find the subscription in the powershell output.
PS C:\Users> (Get-AzureRmSqlDatabase -DatabaseName jasontest1 -ServerName jasontest -ResourceGroupName jasontest).resourceid
/subscriptions/5384xxxx-xxxx-xxxx-xxxx-xxxxe29axxxx/resourceGroups/jasontest/providers/Microsoft.Sql/servers/jasontest/databases/jasontest1
To find which subscription, we can use this command Get-AzureRmSubscription to list it:
PS C:\Users> Get-AzureRmSubscription
Name : Visual Studio Ultimate with MSDN
Id : 5384xxxx-xxxx-xxxx-xxxx-xxxxe29axxxx
TenantId : 1fcfxxx-xxxx-xxxx-xxxx9-xxxx8bf8xxxx
State : Enabled
Also we can use this command to select the subscription:
Get-AzureRmSubscription -SubscriptionId 5384xxxx-xxxx-xxxx-xxx-xxxxe29axxxx

My Powershell Azure modules had dependency errors for something. To fix it I (at the behest of Microsoft tech support) ran:
PS C:\> Install-Module AzureRM -Force
This re-installed it and fixed the dependency problems.

Related

The term 'Get-AzureSubscription' is not recognized as a name of a cmdlet, function, script file, or executable program

I installed Az Module with the following command, but I still get the following error.
Install-Module -Name Az -AllowClobber -Scope AllUsers
Get-AzureSubscription: The term 'Get-AzureSubscription' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
Any ideas?
The command to get the subscription is Get-AzSubscription.
Most Azure commands include Az, not Azure.
You might need to login first with Connect-AzAccount, and logging in through the browser popup.
Sign in with Azure Powershell - Microsoft
Get-AzSubscription no longer works. Use Get-AzureRmSubscription to retrieve your Azure Subscription details.

Azure SQL Server firewall configuration using PowerShell not working

When executing New-AzSqlServerFirewallRule -ResourceGroupName $ResourceGroupName -ServerName $SqlServerName -AllowAllAzureIPs
I get this error: New-AzSqlServerFirewallRule : Could not load type 'Microsoft.Azure.CertificateCloudCredentials' from assembly 'Microsoft.Azure.Common, Version=2.0.0.0 ...
The user I'm using has Owner rights on the Subscription. I can successfully execute e.g. New-AzSqlServer. However, Get-AzSqlServerFirewallRule is also not working. Installed the latest Az.Sql 2.12.0.
When configuring via the Portal I get below error, but the configuration is accepted never the less:
Failed to overwrite firewall rules for server: weu-te-sdb-patrntest-01 and refresh the blade.
ErrorCode: undefined
ErrorMessage: Cannot read property 'toString' of undefined
Upgrading to the following PowerShell modules did the trick:
Az 5.1.0
Az.Accounts 2.2.2
Az.Sql 2.12.0
Couldn't determine why I had that error in the Portal. However the deployment of a new Azure SQL instance and using above module versions worked.

Cannot install Azure/AzureRM PowerShell modules on Worker roles

I have a Worker roles on Azure Cloud service (classic) and I want them to install Azure and AzureRM PowerShell modules on startup. I've added a startup task in my ServiceDefinition.csdef file:
<Startup>
<Task commandLine="InstallAzureModules.cmd" executionContext="elevated" taskType="simple" />
</Startup>
InstallAzureModules.cmd file looks like this:
PowerShell.exe -ExecutionPolicy Unrestricted .\InstallAzureModules.ps1 >> "D:\InstallAzureModulesLogs.txt" 2>&1
And InstallAzureModules.ps1 looks like this:
Install-PackageProvider NuGet -Force
Install-Module Azure -AllowClobber -Force
Install-Module AzureRM -AllowClobber -Force
In result I have an error There is not enough space on the disk, however when I connect to any Worker instance using Remote Desktop and run InstallAzureModules.cmd manually all modules are installed without any errors.
Please help to have these modules installed.
Thanks.
Finally after contacting Microsoft Support the issue was resolved!
According to Support Professional who was working with my request this error is due to a redirection of application temporary folder. I was recommended to go through this link for a possible fix, but it didn't work.
Also updates to my PS script were provided and what actually helped was setting TMP and TEMP environment variables to a folder on drive C. So here is my final script:
$env_TMP = $env:TMP
$env_TEMP = $env:TEMP
$env:TMP = "C:\_trashable\Modules"
$env:TEMP = "C:\_trashable\Modules"
Install-PackageProvider NuGet -Force
Install-Module Azure -AllowClobber -Force
$env:TMP = $env_TMP
$env:TEMP = $env_TEMP
ECHO "Finished"

Creating Registered Servers via Powershell in SSMS

A cool trick that used to work for me is creating Registered Server groups and registrations in SQL Server Management Studio via PowerShell. However, now that I am running the current version (SSMS 17.9.1) I find the functionality has been lost. I'm not sure when it broke.
After installing the SQLServer module:
Install-Module -Name SqlServer
you can open a PowerShell prompt by right-clicking on server group in the Registered Servers window, and clicking Start Powershell. Here, in the past, I've been able to create new groups and registrations with commands like:
Set-Location "sqlserver:\SQLRegistration\Database Engine Server Group"
# group
New-Item -Path "sqlserver:\SQLRegistration\Database Engine Server Group\AllServers"
# registration
New-Item -Name $(encode-sqlname 'sqlsrv') -path "sqlserver:\SQLRegistration\Database Engine Server Group\AllServers" -ItemType Registration -Value ("Server=sqlsrv ; integrated security=true");
These commands still run without error. But the new server registrations are not shown in the Registered Servers window afterwards after a refresh (or even a reboot).
And, server groups and registrations that are setup in the Registered Servers window are not shown with Get-Item/Get-ChildItem at the PowerShell prompt.
Is this functionality still working for anyone else in the current version?
I'm guessing something changed when the SQLPS powershell module were supplanted with the SQLServer module, but that's just a guess. If so I'm hoping there is just a tweak I need to use these commands under the new toolset.
This is an apparent bug in version 21.1.18068 of the SQLServer PowerShell module. Uninstalling that version and installing 21.0.17279 resolves the issue.
uninstall-module SQLServer
install-module -RequiredVersion 21.0.17224 -Name SQLServer

Azure powershell not executing, version issue

I created a VSTS Release Definition with that contains a Azure Powershell script
When I create a release from the following definition everything works until I get to the Azure Powershell script, I get the following log:
2016-08-02T19:06:33.1625377Z ##[command]Import-Module -Name C:\Program
Files (x86)\Microsoft
SDKs\Azure\PowerShell\ResourceManager\AzureResourceManager\AzureRM.Profile\AzureRM.Profile.psd1
-Global 2016-08-02T19:06:37.8986980Z ##[command]Add-AzureRMAccount -ServicePrincipal -Tenant ******** -Credential System.Management.Automation.PSCredential 2016-08-02T19:06:39.0397286Z
[command]Select-AzureRMSubscription -SubscriptionId ******* -TenantId ******** 2016-08-02T19:06:39.1837302Z ##[command]& 'C:\a\e214cea58\ContinuousBuild\drop\Deployment\PrepareIoTSample.ps1'
-environmentName test -configuration debug -webPackageLocation "C:\a\e214cea58\ContinuousBuild\drop\Web\Web.zip"
-webJobPackageLocation "C:\a\e214cea58\ContinuousBuild\drop\WebJob\WebJobHost.zip"
2016-08-02T19:06:40.3037302Z ##[error]Cannot find path
'C:\a\e214cea58\ContinuousBuild\VERSION.txt' because it does not
exist. 2016-08-02T19:06:43.3880731Z ##[error]Version 1.3.2; update to
1.4.0 and run again.
I tried to google the errors but didn't find anything usefull, and I don't see an option to enter a version number. Can someone point me in the right direction?
You can't run a lower version of AzurePS because there can be only one version of Azure PS installed at a time on a single machine.
The online build machines being maintained by Microsoft they all have the same version of Azure PS. (I'm assuming you're using the hosted agent)
I'd suggest you upgrade your script so it runs properly with the new version of Azure PS