Powershell. Get free Azure Service Plan, without apps/slots - azure

Perhaps anyone was trying to get unused resources in Azure by Powershell. I'm interested in a specific resource Azure Service Plan for app services.
I found out how to get resources without apps but stuck with slots. Because resource it-self has 2 type of services running app/slots. After all I get list of App Service without apps but some are with slots. Question is how to get 0/0 resources.
There is a numberOfSites parameter for this purpose but what about slots?
Anyone, please.
$RT = "Microsoft.Web/serverfarms"
$farms = Get-AzureRmResource -ODataQuery "`$filter=resourcetype eq 'Microsoft.Web/serverfarms'"
foreach ($farm in $farms){
$full_farm = Get-AzureRmResource -ResourceGroupName $farm.ResourceGroupName -ResourceType $RT -ResourceName $farm.Name
if ($full_farm.Properties.numberOfSites -eq '0'){
Get-AzureRmResource -ResourceGroupName $farm.ResourceGroupName -ResourceType $RT -ResourceName $farm.Name | Select-Object Name,ResourceGroupName,Location,Sku

You could try my script below, it works on my side.
Note: The azure function consumption plan is not included in my sample.
$RT = "Microsoft.Web/serverfarms"
$farms = Get-AzureRmResource -ODataQuery "`$filter=resourcetype eq 'Microsoft.Web/serverfarms'"
foreach ($farm in $farms){
$usage = Get-AzureRmResource -ResourceGroupName $farm.ResourceGroupName -ResourceType Microsoft.Web/serverfarms/usages -ResourceName $farm.Name -ApiVersion 2018-02-01
if($farm.Kind -ne 'functionapp'){
if($farm.Sku.Name -eq 'F1' -or $farm.Sku.Name -eq 'D1'){
if (($usage | Where-Object {$_.unit -eq 'Sites'}).currentValue -eq '0'){
Get-AzureRmResource -ResourceGroupName $farm.ResourceGroupName -ResourceType $RT -ResourceName $farm.Name | Select-Object Name,ResourceGroupName,Location,Sku
}
}else{
if(($usage | Where-Object {$_.unit -eq 'Sites'}).currentValue -eq '1'){
Get-AzureRmResource -ResourceGroupName $farm.ResourceGroupName -ResourceType $RT -ResourceName $farm.Name | Select-Object Name,ResourceGroupName,Location,Sku
}
}
}
}
Check in the portal:

Related

Setup and deploy Azure function using PowerShell

I try to setup and deploy Azure Function by using PowerShell script based on this topic: Setup Azure Function from PowerShell
My script looks like this:
#=============Defining All Variables=========
$location = 'Central US'
$resourceGroupName = 'MyResourceGroup'
$subscriptionId = 'MysubscriptionId'
$functionAppName = 'MyfunctionAppName'
$appServicePlanName = 'ASP-test-8b50'
$tier = 'Dynamic'
$archivePath = 'd:\TestAzureFunc.zip'
Connect-AzAccount
#========Creating Azure Resource Group========
$resourceGroup = Get-AzResourceGroup | Where-Object { $_.ResourceGroupName -eq $resourceGroupName }
if ($resourceGroup -eq $null)
{
New-AzResourceGroup -Name $resourceGroupName -Location $location -force
}
#selecting default azure subscription by name
Select-AzSubscription -SubscriptionID $subscriptionId
Set-AzContext $subscriptionId
#========Creating App Service Plan============
New-AzAppServicePlan -ResourceGroupName $resourceGroupName -Name $appServicePlanName -Location $location -Tier $tier
$functionAppSettings = #{
ServerFarmId="/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Web/serverfarms/$appServicePlanName";
alwaysOn=$True;
}
#========Creating Azure Function========
$functionAppResource = Get-AzResource | Where-Object { $_.ResourceName -eq $functionAppName -And $_.ResourceType -eq "Microsoft.Web/Sites" }
if ($functionAppResource -eq $null)
{
New-AzResource -ResourceType 'Microsoft.Web/Sites' -ResourceName $functionAppName -kind 'functionapp' -Location $location -ResourceGroupName $resourceGroupName -Properties $functionAppSettings -force
}
#========Defining Azure Function Settings========
$AppSettings =#{}
$AppSettings =#{'FUNCTIONS_EXTENSION_VERSION' = '~2';
'FUNCTIONS_WORKER_RUNTIME' = 'dotnet';}
Set-AzWebApp -Name $functionAppName -ResourceGroupName $resourceGroupName -AppSettings $AppSettings
#========Deploy Azure Function from zip========
Publish-AzWebapp -ResourceGroupName $resourceGroupName -Name $functionAppName -ArchivePath $archivePath
The script works without errors. Resource group and Function App created as needed. But the list of functions of the Function App is empty.
Function details here:
My intuition tells me that I've forgotten something. But I don't know what.
Could you advise me on how to deploy my Azure function properly?
One of the workaround you can follow ,
Looking at your script we need to ensure that we are providing function app configuration as below cmdlts the link you followed:-
$AzFunctionAppSettings = #{
APPINSIGHTS_INSTRUMENTATIONKEY = $AppInsightsKey;
AzureWebJobsDashboard = $AzFunctionAppStorageAccountConnectionString;
AzureWebJobsStorage = $AzFunctionAppStorageAccountConnectionString;
FUNCTIONS_EXTENSION_VERSION = "~4";
FUNCTIONS_WORKER_RUNTIME = "dotnet";
}
And also make sure that the storage account connection string you provided in the function is same as here providing.
And then you can navigate to Kudu API to check the wwwroot folder is exist or not.
For more information please refer the below links:-
SO THREAD|Powershell command Publish-AzWebApp not publishing apllication
BLOG|How to Deploy Azure Function Apps With Powershell.

Lock azure resource with PowerShell

I've been trying to run a script to create a lock on azure resource to prevent resources being deleted inadvertently.
I get an error message and I can't figure out why it's showing me this error message.
Script:
#Sign in to Azure account
Login-AzAccount
#Select the subscription you want to work on
Select-AzSubscription -Subscription "test.subscription"
#Get All Resources in a resource group
$Resources = Get-AzResource -ResourceGroupName dummy_rg | Format-Table
# Create lock "delete" on each Resource if it doesn't exist
foreach($Resource in $Resources) {
$ResourceName = $Resource.Name
$lck = Get-AzResourceLock -ResourceGroupName $Resource.ResourceGroupName -ResourceName $ResourceName -ResourceType $Resource.ResourceType
if ($null -eq $lck)
{
Write-Host "$ResourceName has no lock"
New-AzResourceLock -resourceGroupName $rg -ResourceName $ResourceName -ResourceType $Resource.ResourceType -LockName "$ResourceName-lck" -LockLevel CanNotDelete -Force
Write-Host "$ResourceName has been locked"
}
else
{
Write-host "$ResourceName already locked"
}
}
Error message:
Gaurav request result:
#Start logging
Start-Transcript -Path "C:\Windows\Logs\Lock - $(((get-date).ToUniversalTime()).ToString("yyyy-MM-dd_hh-mm-ss")).log" -Force
#Connect to Azure account
Login-AzAccount
#Select Azure subscription
Set-AzContext -Subscription "subscription_id_numbers"
#Deny rule on Azure Data Factory and Azure Machine Learning
$Resources = Get-AzResource | Where-Object {$_.Name -NotLike '*adf*' -and $_.Name -NotLike '*aml*'}
# Create lock "delete" on each Resource if it doesn't exist
foreach($Resource in $Resources) {
$ResourceName = $Resource.Name
$lck = Get-AzResourceLock -ResourceGroupName $Resource.ResourceGroupName -ResourceName $ResourceName -ResourceType $Resource.ResourceType
if ($lck -eq $null)
{
Write-Host "$ResourceName has no lock"
Set-AzResourceLock -ResourceGroupName $Resource.ResourceGroupName -ResourceName $ResourceName -ResourceType $Resource.ResourceType -LockName "$ResourceName-lck" -LockLevel CanNotDelete -Force
Write-Host "$ResourceName has been locked"
}
else
{
Write-host "$ResourceName already locked"
}
}
#Stop Logging
Stop-Transcript
This will loop on every ressources except azure data factory in the tenant and create a "delete" type lock to make sure resources aren't deleted inadvertently.
Read comments in each section to understand the code.

How to upload liquid files in an integration account via powershell script or any other automated way?

My liquid files are in git_hub repo and I want to upload liquid files in an integration account via powershell or cli
I'm using "New-AzResource"
New-AzResource -Location $ResourceLocation -PropertyObject $PropertiesObject -ResourceGroupName $ResouceGroupname -ResourceType Microsoft.Logic/integrationAccounts/maps -ResourceName "$IntegrationAccountName/$ResourceName" -ApiVersion 2016-06-01 -Force
Login-AzureRmAccount
$IntegrationAccountName = "Integration Account name"
$ResouceGroupname = "ResourcegroupName"
$ResourceLocation = "westus" # location
$ResourceName = "liquid name"
$Content = Get-Content -Path "C:\Tom\simple.liquid" | Out-String
Write-Host $Content
$PropertiesObject = #{
mapType = "liquid"
content = "$Content"
contentType = "text/plain"
}
New-AzureRmResource -Location $ResourceLocation -PropertyObject $PropertiesObject -ResourceGroupName $ResouceGroupname -ResourceType Microsoft.Logic/integrationAccounts/maps -ResourceName " $IntegrationAccountName/$ResourceName" -ApiVersion 2016-06-01 -Force
source: https://stackoverflow.com/a/49063466/1384539

Azure Resource Manager - Get publish username and password of an AzureWebApp

I can get the publish settings use the following method:
$website = Get-AzureWebSite -Name $webSiteName
$websitePreProd = ($website | ?{ $_.Name -like "*Preprod)" })
$siteProperties = $websitePreProd.SiteProperties.Properties
$url = ($siteProperties | ?{ $_.Name -eq "RepositoryURI" }).Value.ToString() + "/MsDeploy.axd"
$userName = ($siteProperties | ?{ $_.Name -eq "PublishingUsername" }).Value
$pw = ($siteProperties | ?{ $_.Name -eq "PublishingPassword" }).Value
But ideally I want to use ARM via:
$existingSlot = Get-AzureWebApp -Name $websiteName -ResourceGroupName $resourcegroup -Slot "PreProd" -ErrorAction SilentlyContinue
How do I get URL, username and password using ARM?
#juvchan's answer will work. Here in an alternative for completeness:
$res = Invoke-AzureRmResourceAction -ResourceGroupName <RG name> -ResourceType Microsoft.Web/sites/config -ResourceName <sitename>/publishingcredentials -Action list -ApiVersion 2015-08-01 -Force
$userName = $res.Properties.PublishingUserName
$pwd = $res.Properties.PublishingPassword
You can try the simple script below to solve your problem.
I assume you are trying to get the MS-Deploy publish profile.
Note: You should install the latest Azure PowerShell to be able to use the new Azure RM commands.
$webAppSlot = Get-AzureRMWebAppSlot -ResourceGroupName <Res-Grp-Name> -Name <webAppName> -Slot <slotName>
$pp = Get-AzureRMWebAppSlotPublishingProfile -WebApp $webAppSlot -OutputFile <OutputFileFullPathName>
[xml]$ppXml = Get-Content <OutputFileFullPathName>
# MSDeploy Publish Profile
$publishUrl = $ppXml.publishData.FirstChild.publishUrl
$userName = $ppXml.publishData.FirstChild.userName
$userPWD = $ppXml.publishData.FirstChild.userPWD

Change Azure website web hosting plan mode using Powershell

I've been reading the following article that describes how to change the web hosting plan for your site.
http://azure.microsoft.com/en-us/documentation/articles/azure-web-sites-web-hosting-plans-in-depth-overview/
That seems to work fine but if I use the same command to change the web hosting mode (using property: "sku": "Free" to "sku": "Standard") it doesn't give any error feedback and just returns with the unchanged (previous) stored configuration.
Command executed:
$standardServer=#{"sku" = "Standard"; }
Set-AzureResource -Name 'tmtest2' -ResourceGroupName 'Default-Web-JapanWest' -ResourceType Microsoft.Web/sites -ApiVersion 2014-04-01 -PropertyObject $standardServer
Anyone had any luck changing the web hosting mode using Powershell?
Edit:
I also tried this link that exactly describes what I'm trying to achieve. However it did not work.
http://msdn.microsoft.com/en-us/library/dn654578.aspx
I figured this out. You need to create a hosting plan (with 'Standard' mode) first as a resource under a default resource group. Then you need to assign the website to the hostingplan.
Here's the full script:
Switch-AzureMode AzureResourceManager
Add-AzureAccount
$locations = #('East Asia', 'Southeast Asia', 'East US', 'East US 2', 'West US', 'North Central US', 'South Central US', 'Central US', 'North Europe', 'West Europe', 'Japan East', 'Japan West', 'Brazil South')
$websiteName = Read-Host 'What is the name of your website (without azurewebsites.net)' #tmtest2
$location = Read-Host 'What is the region location of the website'
if (-Not($locations -contains $location)) {
throw "location is incorrect, try one of these values: " + (($locations | select -expand $_) -join ", ")
}
$resourceGroupName = 'Default-Web-' + $location.Replace(' ', '');
#create a new web hosting plan - Small Standard machine
$hostingPlanName = $websiteName + 'HostingPlan';
$p=#{"name"= $hostingPlanName;"sku"= "Standard";"workerSize"= "0";"numberOfWorkers"= 1}
New-AzureResource -ApiVersion 2014-04-01 -Name $hostingPlanName -ResourceGroupName $resourceGroupName -ResourceType Microsoft.Web/serverFarms -Location $location -PropertyObject $p -Verbose -Force
$r = Get-AzureResource -Name $websiteName -ResourceGroupName $resourceGroupName -ResourceType Microsoft.Web/sites -ApiVersion 2014-04-01
echo $r
$p = $null;
$p = #{ 'serverFarm' = $hostingPlanName }
$r = Set-AzureResource -Name $websiteName -ResourceGroupName $resourceGroupName -ResourceType Microsoft.Web/sites -ApiVersion 2014-04-01 -PropertyObject $p
#echo $r.Properties
if (-Not($r.Properties['sku'] -eq 'Standard')) {
throw 'script executed but sku has not been changed'
}
Start your Azure Resource Manager.
Add-AzureAccount
Switch-AzureMode AzureResourceManager
Get your Azure PowerShell API Version (This link shows how)
$DebugPreference='Continue'
Get-AzureResourceGroup
$DebugPreference='SilentlyContinue'
Get the Name, ResourceGroupName, and ResourceType of your Website Resources
Get-AzureResource | `
Where-Object { $_.ResourceType -like '*site*' } | `
Format-Table Name, ResourceGroupName, ResourceType
Change the Azure Website Hosting Plan for your target Website
Set-AzureResource
-Name the-website-name
-ResourceGroupName 'the-resource-group-name'
-ResourceType 'Microsoft.Web/sites'
-ApiVersion '2014-04-01-preview'
-PropertyObject #{ 'ServerFarm' = 'the-target-server-farm-name' }
Notes:
You can also run Get-AzureResource in a similar way.
To change the hosting plan with Set-AzureResource, you only need the ServerFarm property.

Resources