我编写了一个脚本,该脚本同时使用两个模块SharePointPnPPowerShellOnline
和MicrosoftPowerBIMgmt
:
$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文件夹。
不是我喜欢做的事情,但是很高兴看到我的脚本按预期工作。
我试图:
[System.Reflection.Assembly]::LoadFrom('C:\Users\MYUSER\Desktop\PDF\NewtonSoft.JSON.dll')
添加到脚本Add-Type -Path 'C:\Users\MYUSER\Desktop\PDF\NewtonSoft.JSON.dll'
添加到脚本因此,我想到了这个问题:
与强制覆盖实际文件相比,如何使SharePointPnPPowerShellOnline
不能阻止MicrosoftPowerBIMgmt
以更优雅的方式加载Newtonsoft.JSON.dll
的较新版本?