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 4, 2019

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:

2019-04-04T15:51:47  Welcome, you are now connected to log-streaming service.
2019-04-04T15:51:59.116 [Info] Installing NuGet
2019-04-04T15:52:00.269 [Error] Install-PackageProvider : Destination Path not set.
at <ScriptBlock>, D:\home\site\wwwroot\InstallModuleEx.ps1: line 8
at run.ps1: line 12
+ Install-PackageProvider
+ _______________________
    + CategoryInfo          : InvalidOperation: (https://onegetc...package.swidtag:String) [Install-PackageProvider], Exception
    + FullyQualifiedErrorId : DestinationPathNotSet,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackageProvider
2019-04-04T15:52:00.281 [Info] Installing Az
2019-04-04T15:52:11.629 [Info] Install-Module failed - saving the module
2019-04-04T15:52:23.500 [Info] Exception calling "ShouldContinue" with "2" argument(s): "A command that prompts the user failed because the host program or the command type does not support user interaction. The host was attempting to request confirmation with the following message: PowerShellGet requires NuGet provider version '2.8.5.201' or newer to interact with NuGet-based repositories. The NuGet provider must be available in 'D:\Program Files (x86)\PackageManagement\ProviderAssemblies' or 'D:\local\LocalAppData\PackageManagement\ProviderAssemblies'. You can also install the NuGet provider by running 'Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force'. Do you want PowerShellGet to install and import the NuGet provider now?"

Any ideas what I might be doing wrong? Usual disclaimers to the effect that I don't think I changed anything of significance.

@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