poshbotio / PoshBot

Powershell-based bot framework
MIT License
540 stars 108 forks source link

Update Plugin from PS Gallery Failed #69

Closed vMarkusK closed 6 years ago

vMarkusK commented 6 years ago

!Update-Plugin command for a Module from PS Gallery failes

Expected Behavior

New Version is loaded in PoshBot

Current Behavior

Error: The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)

-> In PowerShell the new Version of the Module is installed.

Possible Solution

My Workaround:

  1. Stop PoshBot (close PS Session)
  2. Uninstall All Plugin Module versions
  3. Start PoshBot
  4. ReInstall Plugin

Steps to Reproduce (for bugs)

1. 2. 3. 4.

Context

Your Environment

devblackops commented 6 years ago

Hi @mycloudrevolution. I wasn't able to reproduce this issue (at least with the PoshBot.XKCD plugin. I ensured the module was completely uninstalled from the system and went through installing an old version and upgrading to various later versions. What was the plugin you tried to update? Is it on the gallery so I can try it?

vMarkusK commented 6 years ago

Hi @devblackops , I verified the update process of "PoshBot.XKCD" in my environment. This plugin update works also fine for me.

The error occured with my own plugin "poshbot.veeam". https://www.powershellgallery.com/packages/poshbot.veeam

Log Message on Install before manual removing older verions (sahe issue lik update): {"DataTime":"2017-11-26 10:24:10Z","Class":"Bot","Method":"ProcessCompletedJobs","Severity":"Error","LogLevel":"Info","Message":"Errors encountered running command [Builtin:Install-Plugin:0.8.0]","Data":[{"CommandName":"Import-Module","Message":"The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)","TargetObject":null,"Position":"At C:\Users\Administrator\Documents\WindowsPowerShell\Modules\PoshBot\0.8.0\PoshBot.psm1:2996 char:13\r\n+ Import-Module -Name $manifestPath -Scope Local -Verbose:$ ...\r\n+ ~~~~~~~~~~~","CategoryInfo":"NotSpecified: (:) [Import-Module], FileLoadException","FullyQualifiedErrorId":"System.IO.FileLoadException,Microsoft.PowerShell.Commands.ImportModuleCommand"},{"CommandName":"Import-Module","Message":"The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)","TargetObject":null,"Position":"At C:\Users\Administrator\Documents\WindowsPowerShell\Modules\PoshBot\0.8.0\PoshBot.psm1:2996 char:13\r\n+ Import-Module -Name $manifestPath -Scope Local -Verbose:$ ...\r\n+ ~~~~~~~~~~~~~~~","CategoryInfo":"NotSpecified: (:) [Import-Module], FileLoadException","FullyQualifiedErrorId":"System.IO.FileLoadException,Microsoft.PowerShell.Commands.ImportModuleCommand"}]} {

devblackops commented 6 years ago

I wonder if it is related the Add-PSSnapin line.

Can you try to recreate the behavior outside of PoshBot? When PoshBot imports the plugin, it runs Import-Module -Name $manifestPath -Scope Local -Verbose:$false -WarningAction SilentlyContinue

Maybe try running the above command from a PowerShell console with the older (and then newer) module manifests?

vMarkusK commented 6 years ago

My steps to reproduce the Issue:

  1. Installed two versions of the module with -Scope CurrentUser
  2. Imported older version of the module
  3. Imported newer version of the module

Error Message:

Add-PSSnapin : An item with the same key has already been added.

OK, loading SnapIns in Modules Sucks... When module is loaded the SnapIn is loaded in a way I dont understand. I am not able to check before load, e.g.:

if ( (Get-PSSnapin -Name VeeamPSSnapin -ErrorAction SilentlyContinue) -eq $null ) { Add-PSsnapin VeeamPSSnapin }

The only solution I found, is loading the SnapIn in every function and not in the root part of the psm1.

If know a better solution please share with me.

devblackops commented 6 years ago

Maybe you can try adding this to the psm1. This should run a script block on module unload that also removes the snapin.

$MyInvocation.MyCommand.ScriptBlock.Module.OnRemove = { Remove-PSSnapin VeeamPSSnapIn }