Find Azure ResourceGroup from Azure SQL Server Name - powershell

I'm trying to write a powershell script that deploys a BACPAC to an Azure database. I found this example on the Microsoft site
I don't have to have to ask which resource group the SQL Server is in so using Powershell, is it possible to find the resource group a SQL server resides in if you only know the server name?

Try this:
$resourceGroupName = (Get-AzureRmResourceGroup | Get-AzureRmSqlServer | where {$_.ServerName -eq 'Your Server Name Here'}).ResourceGroupName

One quick way of doing this is to use the Find-AzureRmResource cmdlet. You can specify as much information as you'd like (resource group, like name, resource type) to narrow down the search results.

Not really any better or worse than Dean's but using the generic cmdlets (would work for any resourceType):
(Get-AzureRmResource | Where-Object {$_.ResourceType -eq "Microsoft.Sql/servers/databases" -and $_.ResourceName -eq "$svr/$db"}).ResourceGroupName

Related

Is there a way to retrieve compute model of Azure SQL database in powershell script

I'm trying to get a list of all "Provisioned" (not "Serverless") Azure databases from particular subscription using Powershell. I'm fairly new to ps, so probably I don't see something very obvious. In Set-AzSQLDatabase method there is a parameter -ComputeModel, however, I can not find corresponding parameter or property to filter on for use in Get-AzSQLDatabase. Can it be done in powershell script? Would really appreciate any pointers in the right direction.
There is no ComputeModel property in the result of Get-AzSQLDatabase, the option is to use Get-AzResource.
There is a Kind property in the result of the command, for Provisioned database, it is v12.0,user,vcore, for Serverless database, it is v12.0,user,vcore,serverless, so we can use it to filter the databases in your subscription.
Sample:
Get-AzResource -ResourceType Microsoft.Sql/servers/databases | Where-Object {$_.Kind -eq 'v12.0,user,vcore'} | ft
If you want to get details about the databases, you could use ConvertTo-Json.
Get-AzResource -ResourceType Microsoft.Sql/servers/databases | Where-Object {$_.Kind -eq 'v12.0,user,vcore'} | ConvertTo-Json

Check existence of single role in server 2016 using powershell

I'm trying to check if Windows Deployment Services is installed in server 2016 using powershell and then use this condition to do further steps. I've tried using Get-WindowsFeature but it gives the status list of all roles and features. I want a command that checks whether a single role or feature is installed or not.
My intention is to:
if(WDS is not installed){
Install-WindowsFeature -Name WDS }
else
Do nothing
Facing problem in finding out status of WDS role
Found the answer thanks to #TheIncorrigible1 and #DavidMartin
Using Get-WindowsFeature -Name WDS | % Installed worked.
Also, Get-WindowsFeature -Name WDS | Format-List helps in finding more helpful details.
You can use
param(
[Parameter(Mandatory=$true)][string]$FeatureName
)
(get-windowsfeature |where name -eq $FeatureName).Installstate
Just pass FeatureName to the the variable

Compiling DSC Config that contains Get-NetAdapter in Azure Automation

As seen in the many Azure Quick Start examples, it is common to use Get-NetAdapter to get the Network Interface Name for things like DNS configuration. This is an example:
configuration MyConfig
{
$Interface=Get-NetAdapter|Where Name -Like "Ethernet*"|Select-Object -First 1
$InterfaceAlias=$($Interface.Name)
Node localhost
{
xDnsServerAddress DnsServerAddress
{
Address = $DNSServer
InterfaceAlias = $InterfaceAlias
AddressFamily = 'IPv4'
}
}
}
If the command Get-NetAdapter is in my configuration and the config is compiled by Azure Automation, I get the following error:
Cannot connect to CIM server. The specified service does not exist as an installed service.
Is there a workaround?
The answer is - its not possible. The configs are compiled on the Azure Automation server, not the target node. Even if I were to find a way to get the network adapter name in the config, it would get the name of the adapter on the DSC pull server, not the target node.
The code in the question will work if you use 1 config per node and you are pre-compiling on the target node then uploading it to Azure Automation.
Try this:
xDnsServerAddress DnsServerAddress
{
Address = $DNSServer
InterfaceAlias = (Get-NetAdapter | ? name -Like "Ethernet*" | select -First 1).Name
AddressFamily = 'IPv4'
}
Get-NetAdapter internally is using WMI to get the information which doesn't work in Azure Automation. However you can use get-netipinterface cmdlet to get information about the adapter.

Get-AzureRmResource returns different data depending on how it was called

This just blew my mind. I run these commands to access a particular resource and print out its location:
PS H:\> $hmm = Get-AzureRmResource -ResourceGroupName "RG_NAME" -ResourceName "R_NAME" -ResourceType "Microsoft.ServiceBus/namespaces"
PS H:\> $hmm.Location
East US 2
But if I run these commands I get different data for the same field:
PS H:\> $hmm2 = Get-AzureRmResource | Where-Object {$_.ResourceName -match "R_NAME"}
PS H:\> $hmm2.Location
eastus2
Before you ask, I only have one resource whose name is "R_NAME".
Why is the Azure API returning different values depending on how I try to access the data? Is there some kind of conversion happening in the background on Azure that's normalizing the data or something?
No one except for the devs will be able to answer this question (why this happens exactly). But probably this happens because when you do a get against the subscription you are talking to an Azure Resource provider and when talking to a single resource you are talking to a servicebus provider. And their responses differ. This can happen. Microsoft is a huge company. things like this happen all the time.

Can't find tagged Azure resource group with powershell

I have tagged my azure resource groups based on environments.
Now I'am trying to do some automation on the test environments and I want to retrieve all resource groups tagged with Env=test.
I found this line here (https://azure.microsoft.com/nb-no/documentation/articles/resource-group-using-tags/)
$testRg= Find-AzureRmResourceGroup -Tag #{ Name="Env"; Value="Test" } | %{ $_.Name }
The problem is that this doesn't return anything.
Then i tried to tag a single resource with the same tag and that worked with this line:
$testR= Find-AzureRmResource -TagName Env -TagValue Test| %{ $_.ResourceName }
Anyone experienced this or know what I'am doing wrong?
After august 2016. Azure powershell had breaking changes apparently.
https://azure.microsoft.com/en-us/documentation/articles/resource-group-using-tags/
The correct way to retrieve resource group based on tags after Azure powershell verion 3.0 is:
(Find-AzureRmResourceGroup -Tag #{ Env="Test" }).Name