Last active
April 5, 2019 17:16
-
-
Save Agazoth/f057d5ef1f6eb9209e299096cd4aa8b0 to your computer and use it in GitHub Desktop.
The code nessesary to add modules to your Azure Powershell Functions without using any other tools then Powershell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try { | |
$NuGet = Get-PackageProvider -ListAvailable -Name Nuget -ErrorAction Stop | |
} Catch { | |
# Logging to the console in Azure gives a warm and cosy feeling and provides documentation for your scripts | |
Write-Output "Installing NuGet" | |
Install-PackageProvider NuGet -Scope CurrentUser -Force | Out-Null | |
} | |
$PSLocalModulePath = "$($env:UserProfile)\Documents\WindowsPowershell\Modules" | |
# Test the existance of the path, and create it, if it doesn't exist | |
if (!$(Test-Path $PSLocalModulePath)){ | |
Write-Output "Creating $PSLocalModulePath" | |
New-Item -ItemType Directory -Path $PSLocalModulePath -Force | Out-Null | |
} | |
# Get an array with the module paths | |
[string[]]$ModulePaths = $env:PSModulePath -split ';' | |
# Find the broken path(s) | |
[string[]]$BrokenPaths = $ModulePaths | where {$_ -notmatch '^\w:'} | |
# Remove the broken paths and doublets and create a new array | |
[string[]]$GoodPaths = $ModulePaths| where {$BrokenPaths -notcontains $_} | Select-Object -Unique | |
# Only update the variable, if the local module path is missing | |
if ($GoodPaths -notcontains $PSLocalModulePath){ | |
Write-Output "Adding $PSLocalModulePath to PSModulePath" | |
# Add the local module path first in the PSModulePath | |
$NewModulePath = $PSLocalModulePath, $($GoodPaths -join ';') -join ';' | |
$env:PSModulePath = $NewModulePath | |
} | |
# Replace $MyModule with any module you have available | |
$MyModule = "Replace with your module" | |
if (!$(Test-Path $(Join-Path $PSLocalModulePath $MyModule))){ | |
Write-Output "Installing $MyModule" | |
try { | |
Install-Module $MyModule -Scope CurrentUser -Force | |
} | |
catch { | |
try { | |
# for some reason AzureRM modules are picky. If you got one installed, the others can only be copied to the folder. | |
Save-Module $MyModule -Path $PSLocalModulePath | |
} catch { | |
Write-Output $error[0].tostring() | |
} | |
} | |
} |
Narrowed it down to this:
If you set the application setting WEBSITE_LOAD_CERTIFICATES to anything - "*", a thumbprint, even if it's to a certificate that is then successfully imported - the above error occurs.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I used this in a test project while learning about Azure, and it's worked properly for a few weeks (I've replaced lines 30 and beyond with a function so it can be used with multiple modules if I need them). Yesterday, it stopped working with the following error:
Any ideas what I might be doing wrong? Usual disclaimers to the effect that I don't think I changed anything of significance.