Skip to content

Commit

Permalink
First resource group creation function commit, not finished
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoColomb0 committed Dec 5, 2023
1 parent 5e1ecbe commit f8c9b99
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 66 deletions.
1 change: 1 addition & 0 deletions Base.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ $VirtualMachine = Set-AzVMSourceImage -VM $VirtualMachine –PublisherName 'Micr

New-AzVM -ResourceGroupName $ResourceGroupName -Location $LocationName -VM $VirtualMachine

Set-AzScheduledAutoshutdown -ResourceGroupName $resourceGroupName -VmName $vmName -ShutdownDaily -ShutdownTime $shutdownTime -TimeZone $timeZone

$PublicIPConnect=Get-AzPublicIpAddress -ResourceGroupName $resourcegroupname -Name $PublicIPAddressName

Expand Down
140 changes: 74 additions & 66 deletions Deployer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,172 +9,180 @@
Prerequisite : PowerShell, Az module and an Azure subscription :)
#>

function AdministratorCheck
{
## Infrastructure parameters
# Generic parameters
$LocationName = 'westeurope'

# Resource group
$ResourceGroupName = 'AzurePSDeployer-rg'

# Virtual machine
$VMName = 'AzurePSDeployer-vm'
$ComputerName = 'AzurePSDeployer-vm'
$VMSize = 'Standard_B2ms'
$ShutdownTime = '14:15'
$Timezone = 'Central European Time'
$ImagePublisher = 'MicrosoftWindowsDesktop'
$ImageOffer = 'Windows-11'
$ImageSKU = 'win11-23h2-pro'

# Network
$VNetName = 'AzurePSDeployer-vnet'
$NICName = 'AzurePSDeployer-nic'
$SubnetName = 'AzurePSDeployer-snet'
$NSGName = 'AzurePSDeployer-nsg'
$PublicIPAddressName = 'AzurePSDeployer-pip'
$SubnetAddressPrefix = '192.168.77.0/24'
$VNetAddressPrefix = '192.168.0.0/16'
$DNSNameLabel = 'apsdvm'

function AdministratorCheck {
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if ($isAdmin)
{
if ($isAdmin) {
Write-Host "[INFO] Script is being run as administrator."
}
else
{
Write-Host "[ERROR] Please run this script with administrator privileges." -ForegroundColor Red
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
}
}

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..."
Install-Module -Name Az -AllowClobber -Scope CurrentUser -Force
Write-Host "[INFO] Az module installed successfully."
}
else
{
else {
Write-Host "[INFO] Az module not installed. Exiting script."
exit
}
}
}

function AccountCheck
{
function AccountCheck {
$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
}
}
}

function SubscriptionCheck
{
function SubscriptionCheck {
$azSubCheck = Get-AzContext
$azSubName = $azSubCheck.Subscription.Name
$azSubList = Get-AzSubscription

if ($azSubCheck)
{
Write-Host "[INFO] The $($azSubName) is currently selected on the Az module."
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
}
}
}
}

function CreateResourceGroup {
# Fix error
$CheckRG = Get-AzResourceGroup -Name $ResourceGroupName -ProgressAction SilentlyContinue
if ($CheckRG) {
Write-Host "[WARNING] Resource group $($ResourceGroupName) was already created, skipping the creation part." -ForegroundColor Yellow
}
else {
New-AzResourceGroup -Name $ResourceGroupName -Location $LocationName
}
}

AdministratorCheck
AzModuleCheck
AccountCheck
SubscriptionCheck
SubscriptionCheck
CreateResourceGroup

0 comments on commit f8c9b99

Please sign in to comment.