diff --git a/README.md b/README.md index 910a9f1..9b1c92c 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,10 @@ iwr https://github.com/MarcoColomb0/AzurePSDeployer/raw/main/AzurePSDeployer.ps1 ### Method #3 (you are able to fully trust the code 😊) Cloning the repository or downloading it as an [archive](https://github.com/MarcoColomb0/AzurePSDeployer/archive/refs/heads/main.zip) on your local machine + +> [!IMPORTANT] +> The log file is saved in the current user's temp folder (%temp%). The filename is composed of $VMName (the virtual machine name defined in the variables) and the execution date + ## Credits This project is maintained by [MarcoColomb0](https://github.com/MarcoColomb0). diff --git a/deployments/w11-23h2.ps1 b/deployments/w11-23h2.ps1 index 6c23dd9..57e381c 100644 --- a/deployments/w11-23h2.ps1 +++ b/deployments/w11-23h2.ps1 @@ -35,44 +35,28 @@ $SubnetAddressPrefix = '192.168.77.0/24' $VNetAddressPrefix = '192.168.0.0/16' $DNSNameLabel = 'apsdwc' -# Verifies administrative privileges on the local machine -# function AdministratorCheck -# { -# $isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) -# if ($isAdmin) -# { -# Write-Host "[INFO] Script is being run as administrator." -# } -# else -# { -# Write-Host "[ERROR] Please run this script with administrator privileges but do not blindly trust!" -ForegroundColor Red -# Write-Host "The source code is available at https://github.com/MarcoColomb0/AzurePSDeployer" -ForegroundColor Red -# exit -# } -# } +# Logs +$LogsDate = Get-Date -format dd-MM-yyyy +$LogsPath = "$ENV:TEMP\$VMName-$LogsDate.log" +Start-Transcript $LogsPath -Append | Out-Null # Checks for the presence of the Az module on the local machine; if not found, prompts for installation -function AzModuleCheck -{ +function AzModuleCheck { $azModuleInstalled = Get-Module -Name Az* -ListAvailable - if ($azModuleInstalled) - { + if ($azModuleInstalled) { Write-Host "[INFO] Az module found." } - else - { + else { Write-Host "[ERROR] Azure PowerShell Az module is not installed." -ForegroundColor Red $installAzModule = Read-Host "[PROMPT] Do you want to install the Az module now? (Y/N)" - if ($installAzModule -eq 'Y' -or $installAzModule -eq 'Yes') - { + if ($installAzModule -eq 'Y' -or $installAzModule -eq 'Yes') { Write-Host "[INFO] Az module is installing..." Write-Host "[INFO] Az module installed successfully." } - else - { + else { Write-Host "[INFO] Az module not installed. Exiting script." exit } @@ -80,65 +64,52 @@ function AzModuleCheck } # Checks for already linked Azure accounts to the Az module and if not present runs "Connect-AzAccount" -function AccountCheck -{ +function AccountCheck { $global:azLoginCheck = Get-AzContext - if ($azLoginCheck) - { + if ($azLoginCheck) { Write-Host "[INFO] Az module is logged in to $($azLoginCheck.Account)" $PromptForAccount = Read-Host "[PROMPT] Do you want to keep using this account? (Y/N)" - if ($PromptForAccount -eq 'Y' -or $PromptForAccount -eq 'Yes') - { + if ($PromptForAccount -eq 'Y' -or $PromptForAccount -eq 'Yes') { Write-Host "[INFO] Current account: $($azLoginCheck.Account)" } - else - { + else { Write-Host "[WARNING] You will connect another Azure account to PowerShell." -ForegroundColor Yellow $PromptForAnotherAccount = Read-Host "[PROMPT] Do you want to continue? (Y/N)" - if ($PromptForAnotherAccount -eq 'Y' -or $PromptForAnotherAccount -eq 'Yes') - { - try - { + if ($PromptForAnotherAccount -eq 'Y' -or $PromptForAnotherAccount -eq 'Yes') { + try { Write-Host "[INFO] Connecting to another account..." Connect-AzAccount | Out-Null } - catch - { + catch { Write-Host "[ERROR] An error occurred while connecting to another Azure account: $_" -ForegroundColor Red exit } } - else - { + else { Write-Host "[WARNING] Aborting, going back to the Azure Account Login Check." AccountCheck } } } - else - { + else { Write-Host "[WARNING] Az module is not logged in" Write-Host "[WARNING] You will connect another Azure account to PowerShell." -ForegroundColor Yellow $PromptForAnotherAccount = Read-Host "[PROMPT] Do you want to continue? (Y/N)" - if ($PromptForAnotherAccount -eq 'Y' -or $PromptForAnotherAccount -eq 'Yes') - { - try - { + if ($PromptForAnotherAccount -eq 'Y' -or $PromptForAnotherAccount -eq 'Yes') { + try { Write-Host "[INFO] Connecting to another account..." Connect-AzAccount } - catch - { + catch { Write-Host "[ERROR] An error occurred while connecting to another Azure account: $_" -ForegroundColor Red exit } } - else - { + else { Write-Host "[WARNING] Aborting, going back to the Azure Account Login Check." AccountCheck } @@ -146,57 +117,47 @@ function AccountCheck } # Checks for subscriptions linked to the account. If no subscriptions are available, the process terminates. If multiple subscriptions are linked to the account in use, prompts the user to choose one. -function SubscriptionCheck -{ +function SubscriptionCheck { $global:azSubCheck = Get-AzContext $azSubName = $azSubCheck.Subscription.Name $azSubList = Get-AzSubscription - if ($azSubCheck) - { + if ($azSubCheck) { Write-Host "[INFO] The $($azSubName) subscription is currently selected on the Az module." $PromptForSubscription = Read-Host "[PROMPT] Do you want to keep using this subscription? (Y/N)" - if ($PromptForSubscription -eq 'Y' -or $PromptForSubscription -eq 'Yes') - { + if ($PromptForSubscription -eq 'Y' -or $PromptForSubscription -eq 'Yes') { Write-Host "[INFO] Current subscription: $($azSubName)" } - else - { + else { Write-Host "[WARNING] You will switch your current subscription to another." -ForegroundColor Yellow Write-Host "[INFO] The subscription selection window is likely in the background. Please ensure to check your taskbar in order to proceed." $SelectedSubscription = $azSubList | Out-GridView -PassThru -Title "Subscriptions List" - if ($SelectedSubscription) - { + if ($SelectedSubscription) { Write-Host "[INFO] Loading the selected subscription: $($SelectedSubscription.Name)" Set-AzContext -Subscription $SelectedSubscription | Out-Null } - else - { + else { Write-Host "[ERROR] No subscription selected. Exiting..." exit } } } - else - { + else { Write-Host "[WARNING] There is no subscription selected in the Az module. This is unusual, as Az automatically assigns a random subscription available in your tenant." $PromptForSubscriptionNotFound = Read-Host "[PROMPT] Would you like to check the available subscriptions anyway? (Y/N)" - if ($PromptForSubscriptionNotFound -eq 'Y' -or $PromptForSubscriptionNotFound -eq 'Yes') - { + if ($PromptForSubscriptionNotFound -eq 'Y' -or $PromptForSubscriptionNotFound -eq 'Yes') { Write-Host "[WARNING] You will switch your current subscription to another." -ForegroundColor Yellow Write-Host "[INFO] The subscription selection window is likely in the background. Please ensure to check your taskbar in order to proceed." $SelectedSubscription = $azSubList | Out-GridView -PassThru -Title "Subscriptions List" - if ($SelectedSubscription) - { + if ($SelectedSubscription) { Write-Host "[INFO] Loading the selected subscription: $($SelectedSubscription.Name)" Set-AzContext -Subscription $SelectedSubscription | Out-Null } - else - { + else { Write-Host "[ERROR] No subscription selected. Exiting..." exit } @@ -205,20 +166,15 @@ function SubscriptionCheck } # Checks for a Resource Group with matching name and if not present it creates it -function CreateResourceGroup -{ - if ($CheckRG) - { +function CreateResourceGroup { + if ($CheckRG) { Write-Host "[WARNING] Resource group $($ResourceGroupName) was already created, skipping the creation part." -ForegroundColor Yellow } - else - { - try - { + else { + try { $CheckRG = Get-AzResourceGroup -Name $ResourceGroupName -ErrorAction Stop } - catch - { + catch { Write-Host "[INFO] The resource group was not found and it's being created." New-AzResourceGroup -Name $ResourceGroupName -Location $LocationName | Out-Null } @@ -226,16 +182,13 @@ function CreateResourceGroup $Timeout = 90 # Maximum time to wait in seconds $StartTime = Get-Date - while ((Get-Date) -lt $startTime.AddSeconds($timeout)) - { + while ((Get-Date) -lt $startTime.AddSeconds($timeout)) { $CheckRG = Get-AzResourceGroup -Name $ResourceGroupName - if ($CheckRG.ProvisioningState -eq "Succeeded") - { + if ($CheckRG.ProvisioningState -eq "Succeeded") { Write-Host "[SUCCESS] RG $($ResourceGroupName) is now available." -ForegroundColor Green return } - elseif ($CheckVM.ProvisioningState -eq "Failed") - { + elseif ($CheckVM.ProvisioningState -eq "Failed") { Write-Host "[ERROR] RG $($ResourceGroupName)) creation failed." -ForegroundColor Red return } @@ -248,15 +201,12 @@ function CreateResourceGroup } # Checks for VNet or Subnet with matching names and if not present it creates them -function CreateVNetAndSubnet -{ +function CreateVNetAndSubnet { $CheckVNet = Get-AzVirtualNetwork -Name $VNetName - if ($CheckVNet) - { + if ($CheckVNet) { Write-Host "[WARNING] Virtual network $($VNetName) was already created, skipping the creation part." -ForegroundColor Yellow } - else - { + else { Write-Host "[INFO] The virtual network and the subnet were not found and are being created." $CreatedSubnet = New-AzVirtualNetworkSubnetConfig -Name $SubnetName -AddressPrefix $SubnetAddressPrefix New-AzVirtualNetwork -Name $VNetName -ResourceGroupName $ResourceGroupName -Location $LocationName -AddressPrefix $VNetAddressPrefix -Subnet $CreatedSubnet | Out-Null @@ -264,16 +214,13 @@ function CreateVNetAndSubnet $Timeout = 90 # Maximum time to wait in seconds $StartTime = Get-Date - while ((Get-Date) -lt $startTime.AddSeconds($timeout)) - { + while ((Get-Date) -lt $startTime.AddSeconds($timeout)) { $CheckVNet = Get-AzVirtualNetwork -ResourceGroupName $ResourceGroupName -Name $VNetName - if ($CheckVNet.ProvisioningState -eq "Succeeded") - { + if ($CheckVNet.ProvisioningState -eq "Succeeded") { Write-Host "[SUCCESS] VNet $($VNetName) and $($SubnetName) are now available." -ForegroundColor Green return } - elseif ($CheckVNet.ProvisioningState -eq "Failed") - { + elseif ($CheckVNet.ProvisioningState -eq "Failed") { Write-Host "[ERROR] VNet $($VNetName)) creation failed." -ForegroundColor Red return } @@ -286,31 +233,25 @@ function CreateVNetAndSubnet } # Checks for Public IP with matching name and if not present it creates it -function CreatePIP -{ +function CreatePIP { $CheckPIP = Get-AzPublicIpAddress -Name $PublicIPAddressName - if ($CheckPIP) - { + if ($CheckPIP) { Write-Host "[WARNING] Public IP address $($PublicIPAddressName) was already created, skipping the creation part." -ForegroundColor Yellow } - else - { + else { Write-Host "[INFO] The public IP address was not found and is being created." New-AzPublicIpAddress -Name $PublicIPAddressName -DomainNameLabel $DNSNameLabel -ResourceGroupName $ResourceGroupName -Location $LocationName -AllocationMethod Static | Out-Null Write-Host "[INFO] Checking if the PIP is ready..." $Timeout = 90 # Maximum time to wait in seconds $StartTime = Get-Date - while ((Get-Date) -lt $startTime.AddSeconds($timeout)) - { + while ((Get-Date) -lt $startTime.AddSeconds($timeout)) { $CheckPIP = Get-AzPublicIpAddress -ResourceGroupName $ResourceGroupName -Name $PublicIPAddressName - if ($CheckPIP.ProvisioningState -eq "Succeeded") - { + if ($CheckPIP.ProvisioningState -eq "Succeeded") { Write-Host "[SUCCESS] PIP $($PublicIPAddressName) is now available." -ForegroundColor Green return } - elseif ($CheckPIP.ProvisioningState -eq "Failed") - { + elseif ($CheckPIP.ProvisioningState -eq "Failed") { Write-Host "[ERROR] PIP $($PublicIPAddressName)) creation failed." -ForegroundColor Red return } @@ -323,15 +264,12 @@ function CreatePIP } # Checks for NSG with matching name and if not present it creates it and adds a rule -function CreateNSG -{ +function CreateNSG { $CheckNSG = Get-AzNetworkSecurityGroup -Name $NSGName - if ($CheckNSG) - { + if ($CheckNSG) { Write-Host "[WARNING] Network security group $($NSGName) was already created, skipping the creation part." -ForegroundColor Yellow } - else - { + else { Write-Host "[INFO] The network security group was not found and is being created." $NSGRuleRDP = New-AzNetworkSecurityRuleConfig -Name $NSGRuleName -Description "Deployed with AzurePSDeployer" -Protocol Tcp -Direction Inbound -Priority 1000 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 3389 -Access Allow New-AzNetworkSecurityGroup -ResourceGroupName $ResourceGroupName -Location $LocationName -name $NSGname -SecurityRules $NSGRuleRDP | Out-Null @@ -340,16 +278,13 @@ function CreateNSG $Timeout = 90 # Maximum time to wait in seconds $StartTime = Get-Date - while ((Get-Date) -lt $startTime.AddSeconds($timeout)) - { + while ((Get-Date) -lt $startTime.AddSeconds($timeout)) { $CheckNSG = Get-AzNetworkSecurityGroup -ResourceGroupName $ResourceGroupName -Name $NSGName - if ($CheckNSG.ProvisioningState -eq "Succeeded") - { + if ($CheckNSG.ProvisioningState -eq "Succeeded") { Write-Host "[SUCCESS] NSG $($NSGName) is now available." -ForegroundColor Green return } - elseif ($CheckNSG.ProvisioningState -eq "Failed") - { + elseif ($CheckNSG.ProvisioningState -eq "Failed") { Write-Host "[ERROR] NSG $($NSGName) creation failed." -ForegroundColor Red return } @@ -362,20 +297,17 @@ function CreateNSG } # Checks for NIC with matching name and if not present it creates it -function CreateNIC -{ +function CreateNIC { $VNetID = Get-AzVirtualNetwork -Name $VNetName $PIPID = Get-AzPublicIpAddress -Name $PublicIPAddressName $NSGID = Get-AzNetworkSecurityGroup -Name $NSGName $CheckNIC = Get-AzNetworkInterface -Name $NICName - if ($CheckNIC) - { + if ($CheckNIC) { Write-Host "[WARNING] Network interface card $($NICName) was already created, skipping the creation part." -ForegroundColor Yellow } - else - { + else { Write-Host "[INFO] The network interface card was not found and is being created." New-AzNetworkInterface -Name $NICName -ResourceGroupName $ResourceGroupName -Location $LocationName -SubnetId $VNetID.Subnets[0].Id -PublicIpAddressId $PIPID[0].Id -NetworkSecurityGroupId $NSGID[0].Id | Out-Null @@ -383,16 +315,13 @@ function CreateNIC $Timeout = 90 # Maximum time to wait in seconds $StartTime = Get-Date - while ((Get-Date) -lt $startTime.AddSeconds($timeout)) - { + while ((Get-Date) -lt $startTime.AddSeconds($timeout)) { $CheckNIC = Get-AzNetworkInterface -ResourceGroupName $ResourceGroupName -Name $NICName - if ($CheckNIC.ProvisioningState -eq "Succeeded") - { + if ($CheckNIC.ProvisioningState -eq "Succeeded") { Write-Host "[SUCCESS] NIC $($NICName) is now available." -ForegroundColor Green return } - elseif ($CheckNIC.ProvisioningState -eq "Failed") - { + elseif ($CheckNIC.ProvisioningState -eq "Failed") { Write-Host "[ERROR] NIC $($NICName) creation failed." -ForegroundColor Red return } @@ -405,17 +334,14 @@ function CreateNIC } # Checks for VM with matching name and creates it with properties that were defined earlier -function CreateVM -{ +function CreateVM { $NICID = Get-AzNetworkInterface -Name $NICName $CheckVM = Get-AzVM -Name $VMName - if ($CheckVM) - { + if ($CheckVM) { Write-Host "[WARNING] Virtual machine $($VMName) was already created, skipping the creation part." -ForegroundColor Yellow } - else - { + else { # Disable unnecessary cost optimizations suggestions Update-AzConfig -DisplayRegionIdentified $false | Out-Null Write-Host "[INFO] The virtual machine was not found and it's going to be created." @@ -434,16 +360,13 @@ function CreateVM $Timeout = 90 # Maximum time to wait in seconds $StartTime = Get-Date - while ((Get-Date) -lt $startTime.AddSeconds($timeout)) - { + while ((Get-Date) -lt $startTime.AddSeconds($timeout)) { $CheckVM = Get-AzVM -ResourceGroupName $ResourceGroupName -Name $VMName - if ($CheckVM.ProvisioningState -eq "Succeeded") - { + if ($CheckVM.ProvisioningState -eq "Succeeded") { Write-Host "[SUCCESS] Virtual machine $($VMName) is now available." -ForegroundColor Green return } - elseif ($CheckVM.ProvisioningState -eq "Failed") - { + elseif ($CheckVM.ProvisioningState -eq "Failed") { Write-Host "[ERROR] Virtual machine $($VMName) creation failed." -ForegroundColor Red return } @@ -456,8 +379,7 @@ function CreateVM } # Prints a summary of the provisioned resources and builds the DNS name (that is not obtainable in the same shell due to Az module limitations) -function InfrastructureSummary -{ +function InfrastructureSummary { Write-Host "[SUCCESS] The tool has finished setting up your Azure infrastructure" -ForegroundColor Green Write-Host "Final infrastructure summary" -ForegroundColor Green Write-Host "Account: $($global:azLoginCheck.Account)" @@ -473,14 +395,12 @@ function InfrastructureSummary } # Prompts the user for connection and if the user accepts it builds an RDP file and execute it through "mstsc.exe" -function ConnectWizard -{ +function ConnectWizard { $RDPAddress = "$($DNSNameLabel).$($LocationName).cloudapp.azure.com" $RDPUsername = $global:Credentials.Username $PromptForConnection = Read-Host "[PROMPT] Do you want to connect via RDP to $($VMName)? (Y/N)" - if ($PromptForConnection -eq 'Y' -or $PromptForConnection -eq 'Yes') - { + if ($PromptForConnection -eq 'Y' -or $PromptForConnection -eq 'Yes') { Write-Host "[INFO] Connection is preparing..." # RDP file content @@ -507,7 +427,6 @@ username:s:$RDPUsername # Functions execution -# AdministratorCheck AzModuleCheck AccountCheck SubscriptionCheck @@ -518,4 +437,4 @@ CreateNSG CreateNIC CreateVM InfrastructureSummary -ConnectWizard +ConnectWizard \ No newline at end of file diff --git a/deployments/ws-2022.ps1 b/deployments/ws-2022.ps1 index 1349bb8..ce7f8d8 100644 --- a/deployments/ws-2022.ps1 +++ b/deployments/ws-2022.ps1 @@ -35,44 +35,28 @@ $SubnetAddressPrefix = '192.168.77.0/24' $VNetAddressPrefix = '192.168.0.0/16' $DNSNameLabel = 'apsdws' -# Verifies administrative privileges on the local machine -# function AdministratorCheck -# { -# $isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) -# if ($isAdmin) -# { -# Write-Host "[INFO] Script is being run as administrator." -# } -# else -# { -# Write-Host "[ERROR] Please run this script with administrator privileges but do not blindly trust!" -ForegroundColor Red -# Write-Host "The source code is available at https://github.com/MarcoColomb0/AzurePSDeployer" -ForegroundColor Red -# exit -# } -# } +# Logs +$LogsDate = Get-Date -format dd-MM-yyyy +$LogsPath = "$ENV:TEMP\$VMName-$LogsDate.log" +Start-Transcript $LogsPath -Append | Out-Null # Checks for the presence of the Az module on the local machine; if not found, prompts for installation -function AzModuleCheck -{ +function AzModuleCheck { $azModuleInstalled = Get-Module -Name Az* -ListAvailable - if ($azModuleInstalled) - { + if ($azModuleInstalled) { Write-Host "[INFO] Az module found." } - else - { + else { Write-Host "[ERROR] Azure PowerShell Az module is not installed." -ForegroundColor Red $installAzModule = Read-Host "[PROMPT] Do you want to install the Az module now? (Y/N)" - if ($installAzModule -eq 'Y' -or $installAzModule -eq 'Yes') - { + if ($installAzModule -eq 'Y' -or $installAzModule -eq 'Yes') { Write-Host "[INFO] Az module is installing..." Write-Host "[INFO] Az module installed successfully." } - else - { + else { Write-Host "[INFO] Az module not installed. Exiting script." exit } @@ -80,65 +64,52 @@ function AzModuleCheck } # Checks for already linked Azure accounts to the Az module and if not present runs "Connect-AzAccount" -function AccountCheck -{ +function AccountCheck { $global:azLoginCheck = Get-AzContext - if ($azLoginCheck) - { + if ($azLoginCheck) { Write-Host "[INFO] Az module is logged in to $($azLoginCheck.Account)" $PromptForAccount = Read-Host "[PROMPT] Do you want to keep using this account? (Y/N)" - if ($PromptForAccount -eq 'Y' -or $PromptForAccount -eq 'Yes') - { + if ($PromptForAccount -eq 'Y' -or $PromptForAccount -eq 'Yes') { Write-Host "[INFO] Current account: $($azLoginCheck.Account)" } - else - { + else { Write-Host "[WARNING] You will connect another Azure account to PowerShell." -ForegroundColor Yellow $PromptForAnotherAccount = Read-Host "[PROMPT] Do you want to continue? (Y/N)" - if ($PromptForAnotherAccount -eq 'Y' -or $PromptForAnotherAccount -eq 'Yes') - { - try - { + if ($PromptForAnotherAccount -eq 'Y' -or $PromptForAnotherAccount -eq 'Yes') { + try { Write-Host "[INFO] Connecting to another account..." Connect-AzAccount | Out-Null } - catch - { + catch { Write-Host "[ERROR] An error occurred while connecting to another Azure account: $_" -ForegroundColor Red exit } } - else - { + else { Write-Host "[WARNING] Aborting, going back to the Azure Account Login Check." AccountCheck } } } - else - { + else { Write-Host "[WARNING] Az module is not logged in" Write-Host "[WARNING] You will connect another Azure account to PowerShell." -ForegroundColor Yellow $PromptForAnotherAccount = Read-Host "[PROMPT] Do you want to continue? (Y/N)" - if ($PromptForAnotherAccount -eq 'Y' -or $PromptForAnotherAccount -eq 'Yes') - { - try - { + if ($PromptForAnotherAccount -eq 'Y' -or $PromptForAnotherAccount -eq 'Yes') { + try { Write-Host "[INFO] Connecting to another account..." Connect-AzAccount } - catch - { + catch { Write-Host "[ERROR] An error occurred while connecting to another Azure account: $_" -ForegroundColor Red exit } } - else - { + else { Write-Host "[WARNING] Aborting, going back to the Azure Account Login Check." AccountCheck } @@ -146,57 +117,47 @@ function AccountCheck } # Checks for subscriptions linked to the account. If no subscriptions are available, the process terminates. If multiple subscriptions are linked to the account in use, prompts the user to choose one. -function SubscriptionCheck -{ +function SubscriptionCheck { $global:azSubCheck = Get-AzContext $azSubName = $azSubCheck.Subscription.Name $azSubList = Get-AzSubscription - if ($azSubCheck) - { + if ($azSubCheck) { Write-Host "[INFO] The $($azSubName) subscription is currently selected on the Az module." $PromptForSubscription = Read-Host "[PROMPT] Do you want to keep using this subscription? (Y/N)" - if ($PromptForSubscription -eq 'Y' -or $PromptForSubscription -eq 'Yes') - { + if ($PromptForSubscription -eq 'Y' -or $PromptForSubscription -eq 'Yes') { Write-Host "[INFO] Current subscription: $($azSubName)" } - else - { + else { Write-Host "[WARNING] You will switch your current subscription to another." -ForegroundColor Yellow Write-Host "[INFO] The subscription selection window is likely in the background. Please ensure to check your taskbar in order to proceed." $SelectedSubscription = $azSubList | Out-GridView -PassThru -Title "Subscriptions List" - if ($SelectedSubscription) - { + if ($SelectedSubscription) { Write-Host "[INFO] Loading the selected subscription: $($SelectedSubscription.Name)" Set-AzContext -Subscription $SelectedSubscription | Out-Null } - else - { + else { Write-Host "[ERROR] No subscription selected. Exiting..." exit } } } - else - { + else { Write-Host "[WARNING] There is no subscription selected in the Az module. This is unusual, as Az automatically assigns a random subscription available in your tenant." $PromptForSubscriptionNotFound = Read-Host "[PROMPT] Would you like to check the available subscriptions anyway? (Y/N)" - if ($PromptForSubscriptionNotFound -eq 'Y' -or $PromptForSubscriptionNotFound -eq 'Yes') - { + if ($PromptForSubscriptionNotFound -eq 'Y' -or $PromptForSubscriptionNotFound -eq 'Yes') { Write-Host "[WARNING] You will switch your current subscription to another." -ForegroundColor Yellow Write-Host "[INFO] The subscription selection window is likely in the background. Please ensure to check your taskbar in order to proceed." $SelectedSubscription = $azSubList | Out-GridView -PassThru -Title "Subscriptions List" - if ($SelectedSubscription) - { + if ($SelectedSubscription) { Write-Host "[INFO] Loading the selected subscription: $($SelectedSubscription.Name)" Set-AzContext -Subscription $SelectedSubscription | Out-Null } - else - { + else { Write-Host "[ERROR] No subscription selected. Exiting..." exit } @@ -205,20 +166,15 @@ function SubscriptionCheck } # Checks for a Resource Group with matching name and if not present it creates it -function CreateResourceGroup -{ - if ($CheckRG) - { +function CreateResourceGroup { + if ($CheckRG) { Write-Host "[WARNING] Resource group $($ResourceGroupName) was already created, skipping the creation part." -ForegroundColor Yellow } - else - { - try - { + else { + try { $CheckRG = Get-AzResourceGroup -Name $ResourceGroupName -ErrorAction Stop } - catch - { + catch { Write-Host "[INFO] The resource group was not found and it's being created." New-AzResourceGroup -Name $ResourceGroupName -Location $LocationName | Out-Null } @@ -226,16 +182,13 @@ function CreateResourceGroup $Timeout = 90 # Maximum time to wait in seconds $StartTime = Get-Date - while ((Get-Date) -lt $startTime.AddSeconds($timeout)) - { + while ((Get-Date) -lt $startTime.AddSeconds($timeout)) { $CheckRG = Get-AzResourceGroup -Name $ResourceGroupName - if ($CheckRG.ProvisioningState -eq "Succeeded") - { + if ($CheckRG.ProvisioningState -eq "Succeeded") { Write-Host "[SUCCESS] RG $($ResourceGroupName) is now available." -ForegroundColor Green return } - elseif ($CheckVM.ProvisioningState -eq "Failed") - { + elseif ($CheckVM.ProvisioningState -eq "Failed") { Write-Host "[ERROR] RG $($ResourceGroupName)) creation failed." -ForegroundColor Red return } @@ -248,15 +201,12 @@ function CreateResourceGroup } # Checks for VNet or Subnet with matching names and if not present it creates them -function CreateVNetAndSubnet -{ +function CreateVNetAndSubnet { $CheckVNet = Get-AzVirtualNetwork -Name $VNetName - if ($CheckVNet) - { + if ($CheckVNet) { Write-Host "[WARNING] Virtual network $($VNetName) was already created, skipping the creation part." -ForegroundColor Yellow } - else - { + else { Write-Host "[INFO] The virtual network and the subnet were not found and are being created." $CreatedSubnet = New-AzVirtualNetworkSubnetConfig -Name $SubnetName -AddressPrefix $SubnetAddressPrefix New-AzVirtualNetwork -Name $VNetName -ResourceGroupName $ResourceGroupName -Location $LocationName -AddressPrefix $VNetAddressPrefix -Subnet $CreatedSubnet | Out-Null @@ -264,16 +214,13 @@ function CreateVNetAndSubnet $Timeout = 90 # Maximum time to wait in seconds $StartTime = Get-Date - while ((Get-Date) -lt $startTime.AddSeconds($timeout)) - { + while ((Get-Date) -lt $startTime.AddSeconds($timeout)) { $CheckVNet = Get-AzVirtualNetwork -ResourceGroupName $ResourceGroupName -Name $VNetName - if ($CheckVNet.ProvisioningState -eq "Succeeded") - { + if ($CheckVNet.ProvisioningState -eq "Succeeded") { Write-Host "[SUCCESS] VNet $($VNetName) and $($SubnetName) are now available." -ForegroundColor Green return } - elseif ($CheckVNet.ProvisioningState -eq "Failed") - { + elseif ($CheckVNet.ProvisioningState -eq "Failed") { Write-Host "[ERROR] VNet $($VNetName)) creation failed." -ForegroundColor Red return } @@ -286,31 +233,25 @@ function CreateVNetAndSubnet } # Checks for Public IP with matching name and if not present it creates it -function CreatePIP -{ +function CreatePIP { $CheckPIP = Get-AzPublicIpAddress -Name $PublicIPAddressName - if ($CheckPIP) - { + if ($CheckPIP) { Write-Host "[WARNING] Public IP address $($PublicIPAddressName) was already created, skipping the creation part." -ForegroundColor Yellow } - else - { + else { Write-Host "[INFO] The public IP address was not found and is being created." New-AzPublicIpAddress -Name $PublicIPAddressName -DomainNameLabel $DNSNameLabel -ResourceGroupName $ResourceGroupName -Location $LocationName -AllocationMethod Static | Out-Null Write-Host "[INFO] Checking if the PIP is ready..." $Timeout = 90 # Maximum time to wait in seconds $StartTime = Get-Date - while ((Get-Date) -lt $startTime.AddSeconds($timeout)) - { + while ((Get-Date) -lt $startTime.AddSeconds($timeout)) { $CheckPIP = Get-AzPublicIpAddress -ResourceGroupName $ResourceGroupName -Name $PublicIPAddressName - if ($CheckPIP.ProvisioningState -eq "Succeeded") - { + if ($CheckPIP.ProvisioningState -eq "Succeeded") { Write-Host "[SUCCESS] PIP $($PublicIPAddressName) is now available." -ForegroundColor Green return } - elseif ($CheckPIP.ProvisioningState -eq "Failed") - { + elseif ($CheckPIP.ProvisioningState -eq "Failed") { Write-Host "[ERROR] PIP $($PublicIPAddressName)) creation failed." -ForegroundColor Red return } @@ -323,15 +264,12 @@ function CreatePIP } # Checks for NSG with matching name and if not present it creates it and adds a rule -function CreateNSG -{ +function CreateNSG { $CheckNSG = Get-AzNetworkSecurityGroup -Name $NSGName - if ($CheckNSG) - { + if ($CheckNSG) { Write-Host "[WARNING] Network security group $($NSGName) was already created, skipping the creation part." -ForegroundColor Yellow } - else - { + else { Write-Host "[INFO] The network security group was not found and is being created." $NSGRuleRDP = New-AzNetworkSecurityRuleConfig -Name $NSGRuleName -Description "Deployed with AzurePSDeployer" -Protocol Tcp -Direction Inbound -Priority 1000 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 3389 -Access Allow New-AzNetworkSecurityGroup -ResourceGroupName $ResourceGroupName -Location $LocationName -name $NSGname -SecurityRules $NSGRuleRDP | Out-Null @@ -340,16 +278,13 @@ function CreateNSG $Timeout = 90 # Maximum time to wait in seconds $StartTime = Get-Date - while ((Get-Date) -lt $startTime.AddSeconds($timeout)) - { + while ((Get-Date) -lt $startTime.AddSeconds($timeout)) { $CheckNSG = Get-AzNetworkSecurityGroup -ResourceGroupName $ResourceGroupName -Name $NSGName - if ($CheckNSG.ProvisioningState -eq "Succeeded") - { + if ($CheckNSG.ProvisioningState -eq "Succeeded") { Write-Host "[SUCCESS] NSG $($NSGName) is now available." -ForegroundColor Green return } - elseif ($CheckNSG.ProvisioningState -eq "Failed") - { + elseif ($CheckNSG.ProvisioningState -eq "Failed") { Write-Host "[ERROR] NSG $($NSGName) creation failed." -ForegroundColor Red return } @@ -362,20 +297,17 @@ function CreateNSG } # Checks for NIC with matching name and if not present it creates it -function CreateNIC -{ +function CreateNIC { $VNetID = Get-AzVirtualNetwork -Name $VNetName $PIPID = Get-AzPublicIpAddress -Name $PublicIPAddressName $NSGID = Get-AzNetworkSecurityGroup -Name $NSGName $CheckNIC = Get-AzNetworkInterface -Name $NICName - if ($CheckNIC) - { + if ($CheckNIC) { Write-Host "[WARNING] Network interface card $($NICName) was already created, skipping the creation part." -ForegroundColor Yellow } - else - { + else { Write-Host "[INFO] The network interface card was not found and is being created." New-AzNetworkInterface -Name $NICName -ResourceGroupName $ResourceGroupName -Location $LocationName -SubnetId $VNetID.Subnets[0].Id -PublicIpAddressId $PIPID[0].Id -NetworkSecurityGroupId $NSGID[0].Id | Out-Null @@ -383,16 +315,13 @@ function CreateNIC $Timeout = 90 # Maximum time to wait in seconds $StartTime = Get-Date - while ((Get-Date) -lt $startTime.AddSeconds($timeout)) - { + while ((Get-Date) -lt $startTime.AddSeconds($timeout)) { $CheckNIC = Get-AzNetworkInterface -ResourceGroupName $ResourceGroupName -Name $NICName - if ($CheckNIC.ProvisioningState -eq "Succeeded") - { + if ($CheckNIC.ProvisioningState -eq "Succeeded") { Write-Host "[SUCCESS] NIC $($NICName) is now available." -ForegroundColor Green return } - elseif ($CheckNIC.ProvisioningState -eq "Failed") - { + elseif ($CheckNIC.ProvisioningState -eq "Failed") { Write-Host "[ERROR] NIC $($NICName) creation failed." -ForegroundColor Red return } @@ -405,17 +334,14 @@ function CreateNIC } # Checks for VM with matching name and creates it with properties that were defined earlier -function CreateVM -{ +function CreateVM { $NICID = Get-AzNetworkInterface -Name $NICName $CheckVM = Get-AzVM -Name $VMName - if ($CheckVM) - { + if ($CheckVM) { Write-Host "[WARNING] Virtual machine $($VMName) was already created, skipping the creation part." -ForegroundColor Yellow } - else - { + else { # Disable unnecessary cost optimizations suggestions Update-AzConfig -DisplayRegionIdentified $false | Out-Null Write-Host "[INFO] The virtual machine was not found and it's going to be created." @@ -434,16 +360,13 @@ function CreateVM $Timeout = 90 # Maximum time to wait in seconds $StartTime = Get-Date - while ((Get-Date) -lt $startTime.AddSeconds($timeout)) - { + while ((Get-Date) -lt $startTime.AddSeconds($timeout)) { $CheckVM = Get-AzVM -ResourceGroupName $ResourceGroupName -Name $VMName - if ($CheckVM.ProvisioningState -eq "Succeeded") - { + if ($CheckVM.ProvisioningState -eq "Succeeded") { Write-Host "[SUCCESS] Virtual machine $($VMName) is now available." -ForegroundColor Green return } - elseif ($CheckVM.ProvisioningState -eq "Failed") - { + elseif ($CheckVM.ProvisioningState -eq "Failed") { Write-Host "[ERROR] Virtual machine $($VMName) creation failed." -ForegroundColor Red return } @@ -456,8 +379,7 @@ function CreateVM } # Prints a summary of the provisioned resources and builds the DNS name (that is not obtainable in the same shell due to Az module limitations) -function InfrastructureSummary -{ +function InfrastructureSummary { Write-Host "[SUCCESS] The tool has finished setting up your Azure infrastructure" -ForegroundColor Green Write-Host "Final infrastructure summary" -ForegroundColor Green Write-Host "Account: $($global:azLoginCheck.Account)" @@ -473,14 +395,12 @@ function InfrastructureSummary } # Prompts the user for connection and if the user accepts it builds an RDP file and execute it through "mstsc.exe" -function ConnectWizard -{ +function ConnectWizard { $RDPAddress = "$($DNSNameLabel).$($LocationName).cloudapp.azure.com" $RDPUsername = $global:Credentials.Username $PromptForConnection = Read-Host "[PROMPT] Do you want to connect via RDP to $($VMName)? (Y/N)" - if ($PromptForConnection -eq 'Y' -or $PromptForConnection -eq 'Yes') - { + if ($PromptForConnection -eq 'Y' -or $PromptForConnection -eq 'Yes') { Write-Host "[INFO] Connection is preparing..." # RDP file content @@ -507,7 +427,6 @@ username:s:$RDPUsername # Functions execution -# AdministratorCheck AzModuleCheck AccountCheck SubscriptionCheck @@ -518,4 +437,4 @@ CreateNSG CreateNIC CreateVM InfrastructureSummary -ConnectWizard +ConnectWizard \ No newline at end of file