From 23dc8c1cddefa1a0d42c319014bee6ed34d4626c Mon Sep 17 00:00:00 2001 From: Thomas Findelkind Date: Fri, 26 May 2023 13:19:33 +0200 Subject: [PATCH] Include commandline parameter For automation usage of the deployment script I added a simple ordered usage of commandline parameters. --- deploymentscript.ps1 | 90 ++++++++++++++++++++++++++------------------ 1 file changed, 53 insertions(+), 37 deletions(-) diff --git a/deploymentscript.ps1 b/deploymentscript.ps1 index dd182c5..d9d9e06 100644 --- a/deploymentscript.ps1 +++ b/deploymentscript.ps1 @@ -1,4 +1,11 @@ -function Get-UrlStatusCode([string] $Url) +# use commandline parameters in the form of +# pwsh ./deploymentscript.ps1 subscriptionid mywebapp eastus +# or via prompt +$selectedSubscription=$($args[0]) +$deploymentName=$($args[1]) +$location=$($args[2]) + +function Get-UrlStatusCode([string] $Url) { try { @@ -13,49 +20,57 @@ $ErrorActionPreference = "Stop" [Console]::ResetColor() # az login --use-device-code -$output = az account show -o json | ConvertFrom-Json -$subscriptionList = az account list -o json | ConvertFrom-Json -$subscriptionList | Format-Table name, id, tenantId -AutoSize -$selectedSubscription = $output.name -Write-Host "Currently logged in to subscription """$output.name.Trim()""" in tenant " $output.tenantId -$selectedSubscription = Read-Host "Enter subscription Id ("$output.id")" -$selectedSubscription = $selectedSubscription.Trim() + if([string]::IsNullOrWhiteSpace($selectedSubscription)) { - $selectedSubscription = $output.id -} else { - # az account set --subscription $selectedSubscription - Write-Host "Changed to subscription ("$selectedSubscription")" + $output = az account show -o json | ConvertFrom-Json + $subscriptionList = az account list -o json | ConvertFrom-Json + $subscriptionList | Format-Table name, id, tenantId -AutoSize + $selectedSubscription = $output.name + Write-Host "Currently logged in to subscription """$output.name.Trim()""" in tenant " $output.tenantId + $selectedSubscription = Read-Host "Enter subscription Id ("$output.id")" + $selectedSubscription = $selectedSubscription.Trim() + if([string]::IsNullOrWhiteSpace($selectedSubscription)) { + $selectedSubscription = $output.id + } else { + # az account set --subscription $selectedSubscription + Write-Host "Changed to subscription ("$selectedSubscription")" + } } -while($true) { - $deploymentName = Read-Host -Prompt "Enter webapp name" - $deploymentName = $deploymentName.Trim() - if($deploymentName.ToLower() -match "xbox") { - Write-Host "Webapp name cannot have keywords xbox,windows,login,microsoft" - continue - } elseif ($deploymentName.ToLower() -match "windows") { - Write-Host "Webapp name cannot have keywords xbox,windows,login,microsoft" - continue - } elseif ($deploymentName.ToLower() -match "login") { - Write-Host "Webapp name cannot have keywords xbox,windows,login,microsoft" - continue - } elseif ($deploymentName.ToLower() -match "microsoft") { - Write-Host "Webapp name cannot have keywords xbox,windows,login,microsoft" - continue - } - # Create the request - $HTTP_Status = Get-UrlStatusCode('http://' + $deploymentName + '.azurewebsites.net') - if($HTTP_Status -eq 0) { - break - } else { - Write-Host "Webapp name taken" + +if([string]::IsNullOrWhiteSpace($deploymentName)) { + while($true) { + $deploymentName = Read-Host -Prompt "Enter webapp name" + $deploymentName = $deploymentName.Trim() + if($deploymentName.ToLower() -match "xbox") { + Write-Host "Webapp name cannot have keywords xbox,windows,login,microsoft" + continue + } elseif ($deploymentName.ToLower() -match "windows") { + Write-Host "Webapp name cannot have keywords xbox,windows,login,microsoft" + continue + } elseif ($deploymentName.ToLower() -match "login") { + Write-Host "Webapp name cannot have keywords xbox,windows,login,microsoft" + continue + } elseif ($deploymentName.ToLower() -match "microsoft") { + Write-Host "Webapp name cannot have keywords xbox,windows,login,microsoft" + continue + } + # Create the request + $HTTP_Status = Get-UrlStatusCode('http://' + $deploymentName + '.azurewebsites.net') + if($HTTP_Status -eq 0) { + break + } else { + Write-Host "Webapp name taken" + } } } -$location = Read-Host -Prompt "Enter location (eastus)" -$location = $location.Trim() if([string]::IsNullOrWhiteSpace($location)) { - $location = "eastus" + $location = Read-Host -Prompt "Enter location (eastus)" + $location = $location.Trim() + if([string]::IsNullOrWhiteSpace($location)) { + $location = "eastus" + } } $resourceGroup = $deploymentName + $location + "-rg" @@ -99,3 +114,4 @@ while($true) { exit } } +