Wednesday, 14 October 2020

Azure PowerShell : Script to create Azure App Service

This PowerShell script is to create an Azure App Service with .NETCORE Web Application:
#Provide following variables:

$subscriptionname = ""
$Environment = ""
$webappName=""              #Provide Web App Name
$location="westus"                         #Provide Location
$resourceGroupName = ""     #Provide Resource Group
$AppServicePlan = ""   #Provide Service Plan
$BusinessOwner = ""
$Owner =""

$SubsId = Get-AzSubscription -SubscriptionName $subscriptionname

Select-AzSubscription -SubscriptionId $SubsId.Id

#Creating WebApp
New-AzWebApp -ResourceGroupName $resourceGroupName -Name $webappName `
-Location $location -AppServicePlan $AppServicePlan

Set-AzWebApp -ResourceGroupName $resourceGroupName `
-Name $webappName -AlwaysOn $true


#Setting .NET CORE
$dotnetResourceName = $webappName + "/metadata"

$PropertiesObject = @{
    "CURRENT_STACK" =  "dotnetcore"
}
New-AzResource -PropertyObject $PropertiesObject -ResourceGroupName $resourceGroupName `
-ResourceType Microsoft.Web/sites/config -ResourceName $dotnetResourceName -Force



#Creating App Insight
New-AzApplicationInsights -ResourceGroupName $resourceGroupName `
-Name $webappName -Location $location

# Collecting create app insight info
$appInsightsKey = Get-AzApplicationInsights -Name $webappName `
-ResourceGroupName $resourceGroupName


$errorpage = "https://"+ $webappName + 
".scm.azurewebsites.net/detectors?type=tools&name=eventviewer"

$appconnectionstring = "InstrumentationKey=" + $appInsightsKey.InstrumentationKey `
+";IngestionEndpoint=https://"+ $location + "-0.in.applicationinsights.azure.com/"


$app = Get-AzWebApp -ResourceGroupName $resourceGroupName -Name $webappName -ErrorAction Stop
$newAppSettings = @{} # case-insensitive hash map
# preserve non Application Insights application settings.
$app.SiteConfig.AppSettings | %{$newAppSettings[$_.Name] = $_.Value} 
$newAppSettings["ANCM_ADDITIONAL_ERROR_PAGE_LINK"] = $errorpage;
 # set the Application Insights instrumentation key
$newAppSettings["APPINSIGHTS_INSTRUMENTATIONKEY"] = $appInsightsKey.InstrumentationKey;
 # set the Application Insights connection string
$newAppSettings["APPLICATIONINSIGHTS_CONNECTION_STRING"] = $appconnectionstring;
 # enable the ApplicationInsightsAgent
$newAppSettings["ApplicationInsightsAgent_EXTENSION_VERSION"] = "~2";
$newAppSettings["XDT_MicrosoftApplicationInsights_Mode"] = "default";

$app = Set-AzWebApp -AppSettings $newAppSettings `
-ResourceGroupName $app.ResourceGroup -Name $app.Name -ErrorAction Stop

$Tags = @{App="Apps";BusinessOwner=$BusinessOwner;Owner=$Owner;Environment =$Environment};

Set-AzResource -ResourceGroupName $resourceGroupName `
-Name $webappName -ResourceType Microsoft.Web/sites -Tag $Tags -Force
    
  

Tuesday, 21 July 2020

MS Azure : Use PowerShell ISE to Run Azure Automation Runbooks

We can Manage and Run Azure Automation Runbooks using PowerShell ISE. This way it helps to test the Powershell script in local Powershell console and then can be published to Azure Automation Runbooks.
To use that we have to install the module Azure Automation Runbook ToolKit on to local machine


Install the PowerShell Module using the below cmdlet:
You can install the ISE add-on module from PowerShell Gallery with:
Install-Module AzureAutomationAuthoringToolkit -Scope CurrentUser
Then, if PowerShell ISE to always automatically load the add-on, run:
Install-AzureAutomationIseAddOn
Otherwise, whenever you want to load the add-on, just run the following in the PowerShell ISE:
Import-Module AzureAutomationAuthoringToolkit


Here is How it looks like in Powershell after Importing and Signing in: