ARMDeploy.psm1
<#
LICENSE: The MIT License (MIT) Copyright (c) 2020 Preston K. Parsard Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. DISCLAIMER: THIS SAMPLE CODE AND ANY RELATED INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. We grant You a nonexclusive, royalty-free right to use and modify the Sample Code and to reproduce and distribute the Sample Code, provided that You agree: (i) to not use Our name, logo, or trademarks to market Your software product in which the Sample Code is embedded; (ii) to include a valid copyright notice on Your software product in which the Sample Code is embedded; and (iii) to indemnify, hold harmless, and defend Us and Our suppliers from and against any claims or lawsuits, including attorneys’ fees, that arise or result from the use or distribution of the Sample Code. #> function New-ARMDeployHeader { <# .SYNOPSIS Create a new script header for console and log output. .DESCRIPTION This function will create a new header label with a title for the script. .EXAMPLE Create a new header hashtable with a title of 'TEST' and two line separators; single and double. New-ARMDeployHeader -label TEST -charCount 80 Name Value ---- ----- Title TEST : 01/21/2020 18:16:20 SeparatorSingle -------------------------------------------------------------------------------- SeparatorDouble ================================================================================ .PARAMETER label The title that will be used for the script .PARAMATER charCount The number of characters representing the width of the header, using the "=" character as a header separator. .INPUTS None .OUTPUTS [hashtable]$header .NOTES AUTHOR: Preston K. Parsard KEYWORDS: Header .LINK https://www.powershellgallery.com/packages/ARMDeploy #> [CmdletBinding()] [OutputType([hashtable])] param ( [Parameter(Mandatory = $true)] [string]$label, [Parameter(Mandatory = $true)] [int]$charCount ) # end param $header = @{ # Draw double line SeparatorDouble = ("=" * $charCount) Title = ("$label :" + " $(Get-Date)") # Draw single line SeparatorSingle = ("-" * $charCount) } # end hashtable return $header } # end function function Get-ARMDeployPSModule { <# .SYNOPSIS Install PowerShell modules from a specified repository. .DESCRIPTION This function installs a set of PowerShell or PowerShell DSC resource modules from a designated PowerShell (NuGet) repository. The repository can be a local or online repository, such as \\<server>\<share>\modules or https://www.powershellgallery.com. You can even use Azure Artifacts (For more details, see link 2 in the LINK tag below). This function will check if the locally installed version of a repository is older than the current online version. In this case, the module will be updated to the most recent online version. Since this function performs module auto-updating, if a specific version must be used for backward compatibility, this function should not be used. Instead use the Install-Module cmdlet with the -RequiredVersion parameter. .EXAMPLE This example shows installing two modules; 'WriteToLogs' and 'xActiveDirectory' installed from the online public PSGallery repository. $modules = @('WriteToLogs','xActiveDirectory') Get-ARMDeployPSModule -ModulesToInstall $modules -PSRepository 'PSGallery' -Verbose VERBOSE: Using the provider 'Bootstrap' for searching packages. VERBOSE: Finding the package 'Bootstrap::FindPackage' 'Nuget','','','''. VERBOSE: Installing the package 'https://onegetcdn.azureedge.net/providers/nuget-2.8.5.208.package.swidtag'. VERBOSE: Installed the package 'nuget' to 'C:\Program Files\PackageManagement\ProviderAssemblies\nuget\2.8.5.208\Microsoft.PackageManagement.NuGetProvider.dll'. VERBOSE: Acquiring providers for assembly: C:\Program Files\PackageManagement\ProviderAssemblies\nuget\2.8.5.208\Microsoft.PackageManagement.NuGetProvider.dll VERBOSE: Skipping previously processed provider 'NuGet'. Name Version Source Summary ---- ------- ------ ------- nuget 2.8.5.208 https://onege... NuGet provider for the OneGet meta-package manager VERBOSE: Importing the package provider Nuget VERBOSE: The provider 'NuGet' has already been imported. Trying to import it again. VERBOSE: Importing package provider 'NuGet'. VERBOSE: Imported provider 'C:\Program Files\PackageManagement\ProviderAssemblies\nuget\2.8.5.208\Microsoft.PackageManagement.NuGetProvider.dll' . VERBOSE: Checking for updates for module 'WriteToLogs'. VERBOSE: Repository details, Name = 'PSGallery', Location = 'https://www.powershellgallery.com/api/v2'; IsTrusted = 'True'; IsRegistered = 'True'. VERBOSE: Using the provider 'PowerShellGet' for searching packages. VERBOSE: Using the specified source names : 'PSGallery'. VERBOSE: Getting the provider object for the PackageManagement Provider 'NuGet'. VERBOSE: The specified Location is 'https://www.powershellgallery.com/api/v2' and PackageManagementProvider is 'NuGet'. VERBOSE: Searching repository 'https://www.powershellgallery.com/api/v2/FindPackagesById()?id='WriteToLogs'' for ''. VERBOSE: Total package yield:'1' for the specified package 'WriteToLogs'. VERBOSE: Performing the operation "Update-Module" on target "Version '1.1.1' of module 'WriteToLogs', updating to version '1.1.1'". VERBOSE: The installation scope is specified to be 'AllUsers'. VERBOSE: The specified module will be installed in 'C:\Program Files\WindowsPowerShell\Modules'. VERBOSE: The specified Location is 'NuGet' and PackageManagementProvider is 'NuGet'. VERBOSE: Downloading module 'WriteToLogs' with version '1.1.1' from the repository 'https://www.powershellgallery.com/api/v2'. VERBOSE: Searching repository 'https://www.powershellgallery.com/api/v2/FindPackagesById()?id='WriteToLogs'' for ''. VERBOSE: InstallPackage' <ommited for privacy> VERBOSE: DownloadPackage' - name= <omitted for privacy> VERBOSE: Completed downloading 'https://www.powershellgallery.com/api/v2/package/WriteToLogs/1.1.1'. VERBOSE: Completed downloading 'WriteToLogs'. VERBOSE: Hash for package 'WriteToLogs' does not match hash provided from the server. VERBOSE: InstallPackageLocal' VERBOSE: Validating the 'WriteToLogs' module contents under <omitted for privacy> path. VERBOSE: Test-ModuleManifest successfully validated the module manifest file <omitted for privacy> VERBOSE: Validating the authenticode signature and publisher of the catalog file or module manifest file of the module 'WriteToLogs'. VERBOSE: Catalog file 'WriteToLogs.cat' is not found in the contents of the module 'WriteToLogs' being installed. VERBOSE: For publisher validation, current module 'WriteToLogs' with version '1.1.1' with publisher name '' from root certificate authority ''. Is this module signed by Microsoft: 'False'. VERBOSE: For publisher validation, using the previously-installed module 'WriteToLogs' with version '1.1.1' under 'C:\Program Files\WindowsPowerShell\Modules\WriteToLogs\1.1.1' with publisher name '' from root certificate authority ''. Is this module signed by Microsoft: 'False'. VERBOSE: Checking for possible command collisions for the module 'WriteToLogs' commands. VERBOSE: Module 'WriteToLogs' was installed successfully to path 'C:\Program Files\WindowsPowerShell\Modules\WriteToLogs\1.1.1'. .PARAMETER ModulesToInstall The modules to install. .PARAMETER PSRepository The PowerShell repository from where modules will be installed or updated. .INPUTS None .OUTPUTS None .NOTES AUTHOR: Preston K. Parsard KEYWORDS: Modules .LINK 1: https://www.powershellgallery.com/packages/ARMDeploy 2: https://docs.microsoft.com/en-us/azure/devops/artifacts/tutorials/private-powershell-library?view=azure-devops #> [CmdletBinding(PositionalBinding = $false)] Param ( # Required modules [Parameter(Mandatory = $true, HelpMessage = "Please enter the PowerShellGallery.com modules required for this script", ValueFromPipeline = $true, Position = 0)] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string[]]$ModulesToInstall, [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$PSRepository = "PSGallery" ) #end param Set-PSRepository -Name $PSRepository -InstallationPolicy Trusted Install-PackageProvider -Name Nuget -ForceBootstrap -Force foreach ($Module in $ModulesToInstall) { if (Get-InstalledModule -Name $Module -ErrorAction SilentlyContinue) { # If module exists, update it [string]$currentVersion = (Find-Module -Name $Module -Repository $PSRepository).Version [string]$installedVersion = (Get-InstalledModule -Name $Module).Version If ($currentVersion -ne $installedVersion) { # If this is a DSC Resource module, i.e. x, then completely uninstall the module and remove it before installing it If ($Module.Substring(0,1) -eq "x") { Uninstall-Module -Name $Module -Force -ErrorAction SilentlyContinue -Verbose Remove-Module -Name $Module -Force -ErrorAction SilentlyContinue -Verbose Install-Module -Name $Module -Repository $PSRepository -Force -ErrorAction SilentlyContinue -Verbose } # end if # Otherwise just update it else { # Update modules if required Update-Module -Name $Module -Force -ErrorAction SilentlyContinue -Verbose } # end else } # end if } # end if # If the modules aren't already loaded, install and import it. else { Install-Module -Name $Module -Repository $PSRepository -Force -Verbose Import-Module -Name $Module -Verbose } #end If } #end foreach } #end function function Remove-ARMDeployPSModule { <# .SYNOPSIS Removes a specified module from the current local machine. .DESCRIPTION This function first checks to see if a particual module is currently installed on the local system, and if so, also uninstalls it and removes it from the current PowerShell session. .EXAMPLE In this example, the AzureRM module will be removed from the local machine. Remove-ARMDeployModule -ModuleToRemove AzureRM -Verbose .PARAMETER ModuleToRemove The module to remove. .INPUTS None .OUTPUTS None .NOTES AUTHOR: Preston K. Parsard KEYWORDS: Modules .LINK 1: https://www.powershellgallery.com/packages/ARMDeploy 2: https://docs.microsoft.com/en-us/powershell/azure/new-azureps-module-az?view=azps-1.6.0 #> [CmdletBinding()] param ( [string]$ModuleToRemove ) # end param # This is to avoid conflicts with installing the new Az modules since it's not recommended to have both installed on the same system. See LINK tag, item 2: in the comment based help section. if (Get-InstalledModule -Name $ModuleToRemove -ErrorAction SilentlyContinue -Verbose) { Uninstall-Module -Name $ModuleToRemove -ErrorAction SilentlyContinue -Verbose Remove-Module -Name $ModuleToRemove -ErrorAction SilentlyContinue -Verbose } # end if } # end function function New-ARMDeployDscArchive { <# .SYNOPSIS Converts each DSC configuration script to a *.zip file .DESCRIPTION This function will enumerate through a specified DSC source folder where DSC configuration scripts are located, and convert each script to a zip file. The *.ps1 configuration files must be converted to the *.zip format so they can be subsequently uploaded to Azure storage and referenced from within ARM templates for Azure deployment based DSC confiugrations. .EXAMPLE In this example, a set of DSC artifacts will be archived, with each *.ps1 file converted to a *.zip file format. New-ARMDeployDscArchive -DscSourceFolder <DscSourceFolder> -Verbose .PARAMETER DscSourceFolder The DSC source folder where the DSC artificats reside. .INPUTS None .OUTPUTS None .NOTES AUTHOR: Preston K. Parsard KEYWORDS: DSC, Desired State Configuration, State Configuration .LINK 1: https://www.powershellgallery.com/packages/ARMDeploy #> [CmdletBinding()] param ( [ValidateScript({Test-Path -Path $_ -PathType Container})] [string]$DSCSourceFolder ) # end param $DSCSourceFilePaths = @(Get-ChildItem $DSCSourceFolder -File -Filter '*.ps1' | ForEach-Object -Process {$_.FullName}) foreach ($DSCSourceFilePath in $DSCSourceFilePaths) { $DSCArchiveFilePath = $DSCSourceFilePath.Substring(0, $DSCSourceFilePath.Length - 4) + '.zip' Publish-AzVMDscConfiguration $DSCSourceFilePath -OutputArchivePath $DSCArchiveFilePath -Force -Verbose } # end foreach } # end function function Enter-ARMDeployFQDN { <# .SYNOPSIS Obtains a registered fully qualified domain name for the ADDS domain that will be created from IaaS VMs in Azure. .DESCRIPTION This function will prompt for an existing, registered fully qualified domain name. This domain name will be used as the domain in Azure using the IaaS deployment model when the domain controllers are provisioned. If the domain does not exist, the prompt for a valid, registered domain name will be re-generated. .EXAMPLE Enter-ARMDeployFQDN .INPUTS A prompt for a fully qualified domain name will be generated by the function. .OUTPUTS [string]$DomainFQDN .NOTES AUTHOR: Preston K. Parsard KEYWORDS: Active Directory, Domain, FQDN .LINK 1: https://www.powershellgallery.com/packages/ARMDeploy #> [OutputType([string])] [CmdletBinding()] param() Do { [string]$DomainFQDN = Read-Host "Please enter your fully qualified domain name, i.e. [example.contoso.com] " $DomainFQDN = $DomainFQDN.ToLower() } #end Do Until (Resolve-DnsName -Name $DomainFQDN -ErrorAction SilentlyContinue) return $DomainFQDN } # end function function Push-ARMDeployArtifactsToAzureStorage { <# .SYNOPSIS Pushes ARM artifacts to an Azure storage account container. .DESCRIPTION This function will upload artifacts to a storage account container in Azure. The function is given a list of directories, the local file system based artifacts staging directory, the storage account resource object and the storage container name. .EXAMPLE Push-ARMDeployArtifactsToAzureStorage -dirlist <dirList> -ArtifactsStagingDirectory <ArtifactsStagingDirectory> -StorageAccount <StorageAccount> -StorageContainerName <StorageContainerName> -Verbose .PARAMETER dirList The list of directories that will be enumated for files with extensions of *.json, *.ps1, *.csv, *.zip to be uploaded to Azure storage. .PARAMETER ArtifactsStagingDirectory The root file system directory from which artifict files will be discovered. .PARAMETER StorageAccount The storage account resource object that will be used for staging the artifact files in Azure blob storage. .PARAMETER StorageContainerName The storage container name within the storage account resource where the artifact files will be staged in Azure blob storage. .INPUTS None .OUTPUTS None .NOTES AUTHOR: Preston K. Parsard KEYWORDS: Active Directory, Domain, FQDN .LINK 1: https://www.powershellgallery.com/packages/ARMDeploy #> [CmdletBinding()] param ( [ValidateNotNull()] [string[]]$dirList, [string]$ArtifactsStagingDirectory, [Microsoft.Azure.Commands.Management.Storage.Models.PSStorageAccount]$StorageAccount, [string]$StorageContainerName ) # end param # Initialize internal variables $dir = $null $pathList = @() $filePaths = @() $SourcePath = $null $subStringStart = $null $subStringStop = $null # Build an array of directories that will be used to recursively copy the artifacts content to the new Azure storage account container. ForEach ($dir in $dirList) { if ($dir -eq $ArtifactsStagingDirectory) { $filePaths = Get-ChildItem -Path $dir -File -Recurse -Include *.json, *.ps1, *.csv, *.zip | ForEach-Object -Process {$_.FullName} } # end if else { $filePaths = Get-childItem -path $dir -File -Recurse | ForEach-Object -Process {$_.FullName} } # end else if ($filePaths) { $pathList += $filePaths } # end if } # end for each # Copy files to storage account # From local storage staging area to the storage account container New-AzStorageContainer -Name $StorageContainerName -Context $StorageAccount.Context -ErrorAction SilentlyContinue *>&1 foreach ($SourcePath in $pathList) { $subStringStart = (Resolve-Path -Path $ArtifactsStagingDirectory).Path.Length + 1 $subStringStop = ($SourcePath.Length - $subStringStart) Set-AzStorageBlobContent -File $SourcePath -Blob $SourcePath.Substring($subStringStart,$subStringStop) -Container $StorageContainerName -Context $StorageAccount.Context -Force } # end foreach } # end function function New-ARMDeployRandomString { <# .SYNOPSIS Creates a new random string. .DESCRIPTION This function creates a random string of 8 characters consisting of lowercase letters and numbers only. This randomly generated string can be used as name prefix, infix or suffixes for certain Azure resources such as storage accounts or public IP addresslables. This is because these resource names must be globally unique in DNS. .EXAMPLE New-ARMDeployRandomString d0454deb .INPUTS None .OUTPUTS [string]$randomString .NOTES AUTHOR: Preston K. Parsard KEYWORDS: Random, String .LINK 1: https://www.powershellgallery.com/packages/ARMDeploy #> [OutputType([string])] [CmdletBinding()] param ( [string]$randomString ) # end param $randomString = (New-Guid).Guid.Replace("-","").Substring(0,8) return $randomString } # end function function New-ARMDeployAssociateSubnetToNsg { <# .SYNOPSIS Associates a subnet to an NSG. .DESCRIPTION This function creates will associate a subnet to an NSG given the resource group name, VNET name, NSG name, subnet name and subnet range. .EXAMPLE New-ARMDeployAssociateSubnetToNSG -resGroupName <resource group name> -vnetName <VNET name> -nsgName <NSG name> -subnetName <subnet name> -subnetRange <subnet range> .PARAMETER resGroupName The resource group in which the virtual netwowrk, subnet and NSG is located. .PARAMETER vnetName The Virtual Network containing the subnet that will be associated with the specified NSG. .PARAMETER nsgName The NSG that will be associated with the subnet. .PARAMETER subnetName The subnet that will be associated with the NSG .PARAMETER subnetRange The subnet range of the subnet that will be associated to the NSG. .INPUTS None .OUTPUTS None .NOTES AUTHOR: Preston K. Parsard KEYWORDS: NSG, Network Security Group, Subnet .LINK 1: https://www.powershellgallery.com/packages/ARMDeploy #> [CmdletBinding()] param ( [string]$resGroupName, [string]$vnetName, [string]$nsgName, [string]$subnetName, [string]$subnetRange ) # end parm $vnetResource = Get-AzVirtualNetwork -ResourceGroupName $resGroupName -Name $vnetName $nsgResource = Get-AzNetworkSecurityGroup -ResourceGroupName $resGroupName -Name $nsgName Set-AzVirtualNetworkSubnetConfig -Name $subnetName -VirtualNetwork $vnetResource -AddressPrefix $subnetRange -NetworkSecurityGroup $nsgResource -Verbose $vnetResource | Set-AzVirtualNetwork -Verbose } # end function Export-ModuleMember -Function *ARMDeploy* -Verbose |