Tuesday, September 28, 2021

POWERSHELL MODULE VERIFY UNINSTALL INSTALL UPGRADE

I was tired of managing multiple versions of modules installed in my powershell

When multiple version of a module are installed, powershell sometimes call the wrong version of the module and end up with a command not found in the old version.

Here is a function to Verify, uninstall, install and upgrade powershell modules:


##################################################

# module verify, update or get

##################################################

function moduleverifgetupdate

{

    param($modulearr01, $versionminimumarr01, $versionupgradetolatestarr01, $verbosesub01)


    function moduleverify 

    {

        param($module01, $verbosesub01)

        

        $modules02 = Get-Module $module01 -ListAvailable | Select-Object Name,Version | Sort-Object Version

        

        $found01 = 0

        $foundsameversion01 = 0


        foreach($module02 in $modules02)

        {

            ### find module in installed modules

            if($module02.name -eq $module01)

            {

                $found01++

                $msg01 = "Module found...: " + $module02.name + "   " + $module02.version.tostring()

                if($verbosesub01 -eq 1){write-host $msg01 -ForegroundColor Yellow}


                ### check version

                if($module02.version -eq $versionminimum01)

                {

                    # uninstall cause it's not the right version

                    $foundsameversion01 = 1

                }

            }

        }

        return $found01, $foundsameversion01, $modules02

    }


    #$module = Get-Module AzureIoT* -ListAvailable | Select-Object Name,Version | Sort-Object Version

    

    for($i=0; $i -lt $modulearr01.count; $i++)

    {

        $module01 = $modulearr01[$i]

        $versionminimum01 = $versionminimumarr01[$i]

        $versionupgradetolatest01 = $versionupgradetolatestarr01[$i]

        

        $msg01 = "Module requis to find...: " + $module01 + "   " + $versionminimum01.tostring()

        if($verbosesub01 -eq 1){write-host $msg01 -ForegroundColor Yellow}


        $found01, $foundsameversion01, $modules02 = moduleverify $module01 $verbosesub01


        #write-host $found01 $foundsameversion01


        # found or not found or found same version or foudn a different version

        $msg01 = "found01...: $found01   Foundsameversion01...: $foundsameversion01"

        if($verbosesub01 -eq 1){write-host $msg01 -ForegroundColor Yellow}


        # manage if found and if same version


        # if more than one module with same name is found, we flush them all

        if($found01 -gt 1)

        {

            $msg01 = "Module found MULTIPLE VERSIONS, uninstalling bad versions..."

            if($verbosesub01 -eq 1){write-host $msg01 -ForegroundColor Yellow}            


            for($i2=0; $i2 -lt $modules02.count; $i2++)

            {

                $modulename02 = $modules02[$i2].name

                $versionminimum02 = $modules02[$i2].version


                ### find module in installed modules

                if($modulename02 -eq $module01)

                {

                    ### check version

                    if($versionminimum02 -ne $versionminimum01)

                    {

                        $msg01 = "Module found to ininstall...: " + $modulename02 + "   " + $versionminimum02.tostring()

                        if($verbosesub01 -eq 1){write-host $msg01 -ForegroundColor Yellow}


                        # uninstall cause it's not the right version

                        Uninstall-Module $module01 -requiredversion $versionminimum02 -force

                    }

                }

            }

        }


        # install module if not found

        if($found01 -eq 0)

        {

            install-module $module01 -requiredversion $versionminimum01 -force

        }


        # upgrade module to the right version (dunno if this will install back versions)

        if(($found01 -eq 1) -and ($foundsameversion01 -eq 0) -and ($versionupgradetolatest01 -eq $true))

        {

            update-module $module01 -force -RequiredVersion $versionminimum01

        }


        $found01, $foundsameversion01, $modules02 = moduleverify $module01 $verbosesub01

        

        $msg01 = "found01...: $found01   Foundsameversion01...: $foundsameversion01"

        if($verbosesub01 -eq 1){write-host $msg01 -ForegroundColor Yellow}


        if(($found01 -eq 1) -and ($foundsameversion01 -eq 1))

        {

            $msg01 = "Module installed...: " + $module01 + "   " + $versionminimum01.tostring()

            if($verbosesub01 -eq 1){write-host $msg01 -ForegroundColor green}

        }

        else

        {

            Write-Error $_

        }


        #$msg01 = $modules01.version

        #Write-Host $msg01 -ForegroundColor Yellow


    } # module wanted array


} # function



###############################################################################################

# main

###############################################################################################

# param modules to install or upgrade or replace

[string[]]$modulearr01 = "AzureIoT"

[string[]]$versionminimumarr01 = "1.0.0.5"

[string[]]$versionupgradetolatestarr01 = $false

#[string[]]$modulearr01+= "module22"

#[string[]]$requiredversionarr01+= "2"

#[string[]]$versionupgradetolatestarr01+= $false


# test

$verbosesub01 = 1

#$dummy = moduleverifgetupdate $modulearr01 $versionminimumarr01 $versionupgradetolatestarr01 $verbosesub01


try

{

    $dummy = moduleverifgetupdate $modulearr01 $versionminimumarr01 $versionupgradetolatestarr01 $verbosesub01

}

catch

{

    write-host "ERROR module install   $_"-ForegroundColor Red

    break all

}


$testcommands01 = 

@"

# fake install to check for double when installing

install-module azureiot -requiredversion "1.0.0.1" -force

Get-Module "azureiot" -ListAvailable | Select-Object Name,Version | Sort-Object Version

"@


No comments:

Post a Comment