Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clean up bad Azure PowerShell uninstall?

Background: I had installed Azure-PowerShell 1.x via the MSI, and subsequently added some Azure Resource Management modules via the command prompt. Everything was working fine; then yesterday afternoon the ISE inexplicably disappeared. In an attempt to remedy that, I planned to uninstall the MSI & then reinstall. (I did not know that it is necessary to first unstall the manually-added modules.) The uninstall appeared to run fine, but it didn't remove the manually-installed modules nor did it warn me about them.

Result: The machine no longer has Azure-PowerShell installed. I cannot Install, Uninstall or Repair the installation because some modules remain:

Azure Modules from the PowerShell Gallery are installed on this machine. Please remove these modules before installing this MSI.

Is there a way to "fix" this installation? Either manually remove files/cleanup registry, or force the MSI to install over whatever is there?

This is all running on a VM on Azure. I could delete the entire VM & start from scratch if necessary, but would prefer to avoid that if there's a relatively simple fix.

Thanks!

like image 279
Mark Maslar Avatar asked Dec 10 '15 14:12

Mark Maslar


People also ask

Can I uninstall Microsoft Azure PowerShell?

Uninstall from PowerShell. If you installed Azure PowerShell using PowerShellGet, you can use the Uninstall-Module cmdlet. However, Uninstall-Module only uninstalls one module. To remove Azure PowerShell completely, you must uninstall each module individually.

How do I remove all PowerShell modules?

If you have multiple versions of the same module installed in the PowerShell, and if you want to uninstall all of them then use the -AllVersions Parameter. If you want to uninstall the specific version, we can use -RequiredVersion. To uninstall on the remote computer, use the Invoke-Command method.

How do I Unimport a PowerShell module?

-ModuleInfoSpecifies the module objects to remove. Enter a variable that contains a module object (PSModuleInfo) or a command that gets a module object, such as a Get-Module command. You can also pipe module objects to Remove-Module .

Is AzureRM deprecated?

All versions of the AzureRM PowerShell module are outdated. The Az PowerShell module is now the recommended PowerShell module for interacting with Azure. Because Az PowerShell modules now have all the capabilities of AzureRM PowerShell modules and more, we'll retire AzureRM PowerShell modules on 29 February 2024.


2 Answers

Okay so I tried the above items to remove windows powershell and found that actually it doesn't fully remove powershell.

This is at least on windows 7 it doesn't seem to properly.

If you run uninstall-module Azure or Uninstall-Module AzureRM it will uninstall something, looks like the base module I think.

Then if you do:

Get-Module AzureRM -ListAvailable

it will come back with nothing. So its done right?

No not really.

If you then do:

Get-Module -ListAvailable AzureRM*

you will find any number of sub modules still sitting there. (For some reason wildcards work with Get-Module but not with Uninstall-Module)

Okay so but then just do Uninstall-Module AzureRm* right? No not really

Depending on your powershell version (Or maybe not, I'm not sure) Install-Module just complains that you can't use wildcards in the Uninstall-Module command. (God knows why what's the point of wildcards if not for this?, but then this is windows so I just had to suck it up).

And if you take a look in %System-root%\Program Files\windowspowershell\modules you will still see the modules there.

Why is this? I'm not sure but this is what I had to do to cleanup all the old versions and newer versions of Azure powershell that I had to get back to a clean slate. So to solve the problem of lack of wildcard support I just used a foreach loop as such:

foreach ($module in (Get-Module -ListAvailable AzureRM*).Name |Get-Unique) { write-host "Removing Module $module" Uninstall-module $module }

Be warned Don't try to run this as Visual Studio code or visual studio for that matter, as depending on your lock you may get errors as it tends to pre-load the modules and lock things open. Put it in a file named Removeold-AzureRM.ps1 and run it from a powershell console window like this: .\Remove-AzureRM.ps1

Also Make sure to close down Visual Studio Code and Visual studio before attempting it or you may still get a similar error message.

If you run this after you already uninstalled AzureRM you will find that things stop working and you only have one last resort. Delete all the AzureRM modules in %System-root%\Program Files\windowspowershell\modules

Edit I have re-tested this and the code above still seems to work if you have azurerm 5.0.1 installed and you already removed azurerm so I could be wrong about other versions as well

Now you will at this point for sure have this sorted and you can now reinstall Azure powershell with Install-Module AzureRM.

If you made the mistake of nuking powershellget like me by accident, don't bother trying to reinstall it with WMF 5.1 or 5.0 as that will install fine but you still won't have powershellget, why I'm not sure and again this is windows so lets just suck it up.

Okay so how to fix it then?

Your only recourse is to download the release of powershellget

And and copy the PowerShellGet-1.5.0.0\PowerShellGet to your modules folder. Then Install-Module will work again.

Yeah I know we are all saying isn't it just safer to re-install?

Yes likely but for those of you like me where that was not an option for one reason or another the above is your best bet. I hope this helps someone as this took me at least 3 days to sort out why I kept getting older modules executing when I was sure I had already removed everything.

like image 123
Isaac Kane Egglestone Avatar answered Sep 20 '22 13:09

Isaac Kane Egglestone


To go faster, you can uninstall in parallel:

workflow Uninstall-AzureModules
{
    $Modules = (Get-Module -ListAvailable Azure*).Name |Get-Unique
    Foreach -parallel ($Module in $Modules)
    { 
        Write-Output ("Uninstalling: $Module")
        Uninstall-Module $Module -Force
    }
}
Uninstall-AzureModules
Uninstall-AzureModules   #second invocation to truly remove everything
like image 40
BlueSky Avatar answered Sep 18 '22 13:09

BlueSky