Can't search the gallery for modules

I don’t have a great work around but I have a fix that resolves this without any startup scripts or anything.

I’ve got this in the 2.9.1 build. I’ll probably release the pretty quickly as there is another issue I’d like to get resolved around licensing. Otherwise, it will be available in tomorrow’s nightly.

3 Likes

We have released PSU 2.9.1 with this fix.

1 Like

Appreciate the hussle!

That’s awesome. Works a treat now without setting the path. Thanks @adam!

One thing I’ve noticed now, though, is that Connect-AzAccount gives this warning:

 [warning] Both Az and AzureRM modules were detected on this machine. Az and AzureRM modules cannot be imported in the same session or used in the same script or runbook. If you are running PowerShell in an environment you control you can use the 'Uninstall-AzureRm' cmdlet to remove all AzureRm modules from your machine. If you are running in Azure Automation, take care that none of your runbooks import both Az and AzureRM modules. More information can be found here: https://aka.ms/azps-migration-guide 

Is it safe (or heck, is it even possible) to remove the AzureRM module from the app service instance, or does PSU use that somehow?

The AzureRM modules are going end of life and the Az modules are their replacement, there’s some backward compatibility with aliases etc but lots of breaking changes with function and parameter names so scripts need to be reworked to use them.

I dont think PSU would use them at all, but maybe adam can confirm. They seem to be installed on a powershell based app service by default under the following location:
C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ResourceManager\AzureResourceManager

Tbh, I’d have expected MS to shift over to Az modules on new app service resources by now already, but obviously they havent yet.

Since I’ve no idea what these are actually used for when it comes to the app service itself, I’d probably just install the Az modules as you already have, but put a line on my dashboard startup script to ensure that any RM modules are unloaded and Az is imported.

1 Like

OK in the end all I could do was add this to my startup script:

Set-Item -Path Env:\PSModulePath -Value (($env:PSModulePath -split ';' | Where-Object { $_ -notlike '*AzureResourceManager*' }) -join ';')

Posting this in case it helps someone else. It doesn’t seem to be possible to remove the old AzureRM modules from an Azure App Service.

So im trying to setup some code in the initialize.ps1 script that checks and installs various modules that we use.

Code looks something like this…

<# MODULES #>
$RequiredModules = @(
    "Microsoft.Graph",
    "AzureAD"
)

try {
    Install-PackageProvider -Name NuGet -RequiredVersion 2.8.5.201 -Force
}
catch {
    "Failed to install nuget $_" | Out-File -FilePath $log -Append
    Write-Error $_ -ErrorAction Stop
}

#Set-PSRepository -Name PSGallery -InstallationPolicy Trusted

foreach ($Module in $RequiredModules) {
    $FoundModule = Get-Module -ListAvailable -Name $Module
    $FoundModule | Select-Object Name, Version | Out-File -FilePath $log -Append

    if ($FoundModule) {
        $message = "Module $($FoundModule.Name):$($FoundModule.Version) exists"
        $message | Out-File -FilePath $log -Append
    } 
    else {
        $Message = "Module $Module does not exist"
        $message | Out-File -FilePath $log -Append

        try {
            Install-Module -Name $Module -Force -Confirm:$false
            "installed module $Module" | Out-File -FilePath $log -Append
        }
        catch {
            $message = "Failed to install module $module $_"
            $message | Out-File -FilePath $log -Append
        }
    }
}

However in my logs im getting the following:

Module Microsoft.Graph does not exist
Failed to install module Microsoft.Graph 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 'C:\Program Files\PackageManagement\ProviderAssemblies' or 'C:\WINDOWS\system32\config\systemprofile\AppData\Local\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?"

I am stuck. Is anyone doing something like this? Is there something I am missing with the initialize.ps1 that I need to add?

Thanks,
Michael

PowerShell Universal Startup Script · Mabster.NET

See if that helps at all, @mikedhanson!

1 Like

Papa bless. Ill give this a shot. Thanks for sharing!

@mabster Ran into some issues with your code…

When you are checking to see if the module is already installed

$installed = Get-PSResource -Name $m -Path $path

if there is nothing in that $path or $path is an empty directory you will get an exception.

I made some minor tweaks which works for me on a fresh install.

$LogPath = "C:\ProgramData\UniversalAutomation\logs\"
$Log = "$LogPath\initialize.log"

if (! (Test-Path -Path $LogPath)) {
    try {
        New-Item -ItemType Directory $LogPath
    }
    catch {
        Write-Host "Failed to create log folder under $LogPath. Reason: $_" -ErrorAction Stop
    }
}

$Repository = "C:\ProgramData\UniversalAutomation\Repository"

# make a 'Modules' folder alongside the repository
$path = Join-Path $Repository '..\Modules\'

if (! (Test-Path $path)) {
    New-Item -Path $path -ItemType Directory -Force
}
$path = Resolve-Path $path 
"Path is $path" | Out-File -FilePath $log -Append

# Add it to the path
if ($env:PSModulePath -split ';' -notcontains $path) {
    $env:PSModulePath += ";$path"
}

$RequiredModules = @(
    'Microsoft.Graph.Users',
    'Microsoft.Graph.Authentication',
    'Microsoft.Graph.Intune',
    'Microsoft.Graph.Groups',
    "AzureAD",
    "JWT"
)

foreach ($Module in $RequiredModules){
    "Attempting to install $Module" | Out-File -FilePath $log -Append
    try {
        $installed = Get-PSResource -Name $Module -Path $path
    }
    catch {
        "Failed to find module. $Module."
    }

    try {
        if ($installed) {
            "$Module is installed. $installed"
            $CurrentMod = Find-PSResource -Name $Module
            if ($CurrentMod.Version -le $installed.Version) {
                "Skipping $Module" | Out-File -FilePath $log -Append
                return
            }

            "Uninstalling $Module so we upgrade to version $($CurrentMod.Version)" | Out-File -FilePath $log -Append
            Uninstall-PSResource -Name $Module
        } 
        else {
            "Installing $Module" | Out-File -FilePath $log -Append
            Save-PSResource -Name $Module -Path $path -TrustRepository -IncludeXML
        }   
    }
    catch {
        "Could not install/update Module $Module. Reason $_" | Out-File -FilePath $log -Append
    }
}

$env:PSModulePath = ($env:PSModulePath -split ';' | Where-Object { $_ -notlike '*AzureResourceManager*' }) -join ';'


1 Like