Export/Public/New-StandAloneServer.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
function New-StandAloneServer {
    [CmdletBinding()]
    <#
    .SYNOPSIS
        Creates a new Application Server, installs Business Central, sys-preps it and saves it as an image
    .DESCRIPTION
        This CmdLet will create a new VM, initialize it (install this and other modules to it), download the desired Business Central DVD and install it locally.
        Afterwards it will generalize the created VM and set it as an Image to be used for future ScaleSets
    #>

    param(
        [Parameter(Mandatory = $true)]
        [string]
        $ResourceGroupName,        
        [Parameter(Mandatory = $true)]
        [string]
        $ResourceLocation,
        [Parameter(Mandatory = $false)]
        [string]
        $ImageResourceGroupName = $ResourceGroupName,
        [Parameter(Mandatory = $true)]
        [string]
        $ImageName,
        [Parameter(Mandatory = $false)]
        [string]
        $VirtualNetworkResourceGroup,
        [Parameter(Mandatory = $false)]
        [string]
        $StorageAccountType,
        [Parameter(Mandatory = $true)]
        [string]
        $VirtualMachineName,        
        [Parameter(Mandatory = $false)]
        [string]
        $StorageAccountResourceGroupName = $ResourceGroupName,
        [Parameter(Mandatory = $false)]
        [string]
        $StorageAccountName,
        [Parameter(Mandatory = $true)]
        [string]
        $VirtualNetworkName,
        [Parameter(Mandatory = $true)]
        [string]
        $SubnetName,
        $PrivateIpAddress,
        [Parameter(Mandatory = $false)]
        [string]
        $KeyVaultResourceGroupName = $ResourceGroupName,
        [Parameter(Mandatory = $true)]
        [string]
        $KeyVaultName,
        [Parameter(Mandatory = $false)]
        [string]
        [Alias("Version")]
        $BCVersion,
        [Parameter(Mandatory = $false)]
        [string]
        [Alias("CumulativeUpdate")]
        $BCCumulativeUpdate,
        [Parameter(Mandatory = $false)]
        [string]
        [Alias("Language")]
        $BCLanguage,
        [Parameter(Mandatory = $true)]
        [string]
        $VmAdminUserName,
        [Parameter(Mandatory = $true)]
        [string]
        $VmAdminPassword,
        [Parameter(Mandatory = $false)]
        [string]
        $VmSize,
        [Parameter(Mandatory = $false)]
        [string]
        $VmOperatingSystem,
        [Parameter(Mandatory = $true)]
        [ValidateSet('App', 'Web', 'Both')]
        [string]
        $InstallationType,
        [Parameter(Mandatory = $false)]
        [object]
        $ResourceTags,
        [switch]
        $AsJob
    ) 
    $scriptBlock = {
        $VM = Get-AzVM -ResourceGroupName $ResourceGroupName -Name $VirtualMachineName -ErrorAction SilentlyContinue
        if ($VM){
            Write-Verbose "VM $VirtualMachineName already exists. Stopping here."
            return
        }
        $oldErrorActionPreference = $ErrorActionPreference
        $ErrorActionPreference = "Stop"

        $oldVerbosePreference = $VerbosePreference
        $VerbosePreference = 'SilentlyContinue'
        Import-Module Az.Compute, Az.Resources
        $VerbosePreference = 'Continue'
        
        # Needed if started as Job
        $args[0].GetEnumerator() | ForEach-Object {
            New-Variable -Name $_.Key -Value $_.Value
        }
        
        Write-Verbose "Checking if Image $ImageName already exists"
        $image = Get-AzImage -ResourceGroupName $ImageResourceGroupName -ImageName $ImageName -ErrorAction SilentlyContinue
        if (-not($image)) {                        
            Write-Verbose "Image $ImageName does not exist. Exiting here."
            return
        }        
        Write-Verbose "Starting Image creation for $ImageName"

        try {
            # Copy necessary parameters to new HashTable; this will be used inside the Parameters for the upcoming New-AzResourceGroupDeployment
            $paramsObject = @{ }        
            foreach ($var in $args[0].GetEnumerator()) {
                if ($var.Key -notin @('ResourceGroupName', 'ResourceLocation', 'ImageName', 'TemplateFile', 'TemplateUri', 'AsJob', 'DoNotGeneralize')) {
                    $paramsObject.Add($var.Key, $var.Value)
                }
            }
            $VmAdminPasswordSecured = ConvertTo-SecureString $VmAdminPassword -AsPlainText -Force
            $Credential = New-Object System.Management.Automation.PSCredential ($VmAdminUserName, $VmAdminPasswordSecured);

            $VNet = Get-AzVirtualNetwork -Name $VirtualNetworkName -ResourceGroupName $VirtualNetworkResourceGroup
            $Subnet = Get-AzVirtualNetworkSubnetConfig -Name $SubnetName -VirtualNetwork $VNet            
            $NIC = New-AzNetworkInterface -Name "$($VirtualMachineName)_Nic_01" -ResourceGroupName $ResourceGroupName -Location $ResourceLocation -Subnet $Subnet -IpConfigurationName "IPConfig1"

            $VirtualMachine = New-AzVMConfig -VMName $VirtualMachineName -VMSize $VmSize -IdentityType SystemAssigned
            $VirtualMachine = Set-AzVMOperatingSystem -VM $VirtualMachine -Windows -ComputerName $VirtualMachineName -Credential $Credential -ProvisionVMAgent
            $VirtualMachine = Add-AzVMNetworkInterface -VM $VirtualMachine -Id $NIC.Id
            $VirtualMachine = Set-AzVMSourceImage -VM $VirtualMachine -Id $image.Id
            $VirtualMachine = Set-AzVMOSDisk -VM $VirtualMachine -Name "$($VirtualMachineName)_OsDisk_01" -CreateOption FromImage -DiskSizeInGB 128 -StorageAccountType StandardSSD_LRS

            $vm = New-AzVM -ResourceGroupName $ResourceGroupName -Location $ResourceLocation -VM $VirtualMachine -Verbose:$Verbose
            $vm = Get-AzVM -ResourceGroupName $ResourceGroupName -Name $VirtualMachineName

            Wait-ForNewlyCreatedIdentity -ResourceGroupName $ResourceGroupName -ObjectId $vm.Identity.PrincipalId -Verbose:$Verbose
            Write-Verbose "Assigning role 'Reader' on Resource Group-level..."
            New-AzRoleAssignment -ObjectId $vm.Identity.PrincipalId -RoleDefinitionName "Reader" -ResourceGroupName $ResourceGroupName | Out-Null        
            Write-Verbose "Assigning role 'Contributor' on Storage Account-level..."
            New-AzRoleAssignment -ObjectId $vm.Identity.PrincipalId -RoleDefinitionName "Contributor" -ResourceGroupName $StorageAccountResourceGroupName -ResourceName (Get-AzStorageAccount -ResourceGroupName $StorageAccountResourceGroupName -Name $StorageAccountName | Select-Object -First 1).StorageAccountName -ResourceType "Microsoft.Storage/storageAccounts" | Out-Null

            Set-KeyVaultPermissionsForScaleSet -ResourceGroupName $ResourceGroupName -KeyVaultResourceGroup $KeyVaultResourceGroupName -KeyVaultName $KeyVaultName -VMName $VirtualMachineName -Verbose
        }
        catch {
            Write-Error $_
        }
        $VerbosePreference = $oldVerbosePreference
        $ErrorActionPreference = $oldErrorActionPreference
    }
    $params = Get-FunctionParameters $MyInvocation
    
    if ($AsJob) {            
        Start-Job -ScriptBlock $scriptBlock -InitializationScript { Import-Module D365BCOnAzureDeployment -Force } -ArgumentList $params
    }
    else {
        Invoke-Command -ScriptBlock $scriptBlock -ArgumentList $params
    }
}