Functions/Update-ARMresourceList.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 |
#Requires -Version 5.0 function Update-ARMresourceList { <# .SYNOPSIS This will update the allResources.json file that is used as input when creating a New-ARMresource .DESCRIPTION This will update the allResources.json file that is used as input when creating a New-ARMresource. This cmdlet supports ShouldProcess (whatif). .PARAMETER Credential Credentials for an Azure subscription .PARAMETER Force Force an update of the file if it exists .EXAMPLE Update-ARMresourceList This will try and fetch the resoruce provider list from Azure using the current AzureRMcontext. If no AzureRMcontext exists, it will invoke Login-AzureRMAccount without credentials. .EXAMPLE Update-ARMresourceList -Credential (Get-Credential -Message "Azure Credential") This will try and fetch the resoruce provider list from Azure using the current AzureRMcontext. If no AzureRMcontext exists, it will invoke Login-AzureRMAccount with the specified credentials. .INPUTS PSCustomObject .OUTPUTS string .NOTES Author: Tore Groneng Website: www.firstpoint.no Twitter: @ToreGroneng #> [cmdletbinding( SupportsShouldProcess=$true )] Param( [pscredential]$Credential , [switch]$Force ) $LoggedIn = $false $f = $MyInvocation.InvocationName Write-Verbose -Message "$f - START" try { Write-Verbose -Message "$f - Getting AzureRMContext" Get-AzureRmContext $LoggedIn = $true } catch { $ex = $_.Exception Write-Verbose -Message "$f - Get-AzureRmContext threw an exception, propably not loggedin" } if ($LoggedIn -eq $false) { if ($Credential) { Write-Verbose -Message "$f - Invoking Login-AzureRmAccount with credentials" Login-AzureRmAccount -Credential $Credential -ErrorAction Stop } else { Write-Verbose -Message "$f - Invoking Login-AzureRmAccount without credentials" Login-AzureRmAccount -ErrorAction Stop } } $fileName = Split-Path -Path $PSScriptRoot -Parent | Join-Path -ChildPath Data | Join-Path -ChildPath "allResources.json" $outFile = @{} $shouldProcessOperation = "Creating file" if ($Force.IsPresent) { $outFile.Add("Force", $true) $shouldProcessOperation = "Overwriting file" } if ($pscmdlet.ShouldProcess("$fileName", $shouldProcessOperation)) { Write-Verbose -Message "$f - Getting a providerlist, saving to [$fileName]" Get-AzureRmResourceProvider -ListAvailable | ConvertTo-Json -Depth 10 | Out-File -FilePath "$fileName" -Encoding utf8 @outFile } Write-Verbose -Message "$f - END" } |