如何更改模块的加载顺序-或指定要加载哪个模块的DLL?

时间:2019-11-27 13:44:52

标签: powershell

我编写了一个脚本,该脚本同时使用两个模块SharePointPnPPowerShellOnlineMicrosoftPowerBIMgmt

$User = Read-Host "Username"
$PW = Read-Host $User -AsSecureString
$PW2 = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($PW))
$SecPasswd = ConvertTo-SecureString $PW2 -AsPlainText -Force
$myCred = New-Object System.Management.Automation.PSCredential ($User,$SecPasswd)
$siteurl = "https://company.sharepoint.com/sites/oursite/"
$listid = "b59ff639-1e0c-4566-a157-0bfuscat3"

Connect-PowerBIServiceAccount -Credential $myCred
Connect-PnPOnline -Url $siteurl -Credentials $myCred

[Newtonsoft.Json.JsonConvert].Module # Check version of Newtonsoft.JSON
Get-PowerBIWorkspace
#Get-PnPListItem -List $listid

它们都包含Newtonsoft.JSON.dll,但版本不同。 MicrosoftPowerBIMgmt使用版本12.0.2.23222,而SharePointPnPPowerShellOnline使用版本11.0.2.21924

运行脚本时,得到以下输出:

PS C:\Users\MYUSER> C:\Users\MYUSER\Desktop\PDF\debug\JSONproblem.ps1
Username: myuser@company.com


Environment : Public
TenantId    : a70a7a87-f963-4568-99a0-6cdd04c30251
ClientId    : myuser@company.com
Password    : ****************


MDStreamVersion    : 131072
FullyQualifiedName : C:\Program Files\WindowsPowerShell\Modules\SharePointPnPPowerShellOnline\3.15.1911.0\NewtonSoft.Json.dll
ModuleVersionId    : 07e38931-19a9-45b2-9a35-e81930b1c8ad
MetadataToken      : 1
ScopeName          : Newtonsoft.Json.dll
Name               : NewtonSoft.Json.dll
Assembly           : Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
CustomAttributes   : {}
ModuleHandle       : System.ModuleHandle

Get-PowerBIWorkspace : Attempted to access an element as a type incompatible with the array.
At C:\Users\MYUSER\Desktop\PDF\debug\JSONproblem.ps1:13 char:1
+ Get-PowerBIWorkspace
+ ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (Microsoft.Power...owerBIWorkspace:GetPowerBIWorkspace) [Get-PowerBIWorkspace], ArrayTypeMismatchException
    + FullyQualifiedErrorId : Attempted to access an element as a type incompatible with the array.,Microsoft.PowerBI.Commands.Workspaces.GetPowerBIWorkspace

在该错误消息中使用谷歌搜索术语使我了解到NewtonSoft.JSON是罪魁祸首,并向我展示了解决此问题的几种方法-不幸的是,我唯一能使用的方法是最坚定的修复方法:

将版本12 DLL从MicrosoftPowerBIMgmt文件夹复制到SharePointPnPPowerShellOnline文件夹。

不是我喜欢做的事情,但是很高兴看到我的脚本按预期工作。

我试图:

  1. 卸载两个模块,然后以两个可能的顺序重新安装它们。
  2. [System.Reflection.Assembly]::LoadFrom('C:\Users\MYUSER\Desktop\PDF\NewtonSoft.JSON.dll')添加到脚本
  3. Add-Type -Path 'C:\Users\MYUSER\Desktop\PDF\NewtonSoft.JSON.dll'添加到脚本
  4. Followed this article which aims to solve the exact problem I have (conflicting NewtonSoft.JSON versions) but it still won't work!
  5. Google实现目标的其他方法–我什么也没发现

因此,我想到了这个问题:

与强制覆盖实际文件相比,如何使SharePointPnPPowerShellOnline不能阻止MicrosoftPowerBIMgmt以更优雅的方式加载Newtonsoft.JSON.dll的较新版本?

0 个答案:

没有答案