Skip to content

Instantly share code, notes, and snippets.

@Agazoth
Last active April 5, 2019 17:16
Show Gist options
  • Save Agazoth/f057d5ef1f6eb9209e299096cd4aa8b0 to your computer and use it in GitHub Desktop.
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
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()
}
}
}
@alujones
Copy link

alujones commented Apr 5, 2019

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