Nuget Cannot Be Imported Again It Was Already Imported

Sometimes the logic for the bit of piece of work you're doing in PowerShell won't be packaged into a handy PowerShell module with cmdlets to perform the operations yous need to perform. But what if there is a .NET parcel listed on NuGet that does comprise the assemblies you need for your work? How can nosotros import that into PowerShell and make use of its classes and methods?
This is the conundrum we recently came beyond while trying to update credentials for an on-prem Ability BI data source using PowerShell. The tricky part in that scenario was some RSA-OAEP encryption logic that needed to exist applied to the information source authentication credentials before sending the request payload to the Update Gateway Data Source API endpoint. The Power BI team had recently provided a brand new v3 .Cyberspace Power BI SDK which included helper classes for easier encryption when creating the credential objects, merely these classes hadn't been translated into corresponding PowerShell cmdlets to be used as part of our deployment process. Trying to recreate those encryption helpers in PowerShell would take taken quite a chip of try, and then we really wanted to utilize this new SDK.
Once you know the method, it's really quite easy. Information technology's how we came to this method that prompted us to write this blog, for the benefit of others that may want to do the same thing.
Installing and loading a NuGet packet in a PowerShell session
If you run Get-PackageSource
in any of the latest versions of Windows PowerShell or PowerShell Cadre, nuget.org
should be a registered package source. Peachy.
And so the following should be plenty to install the Power BI package forth with its required dependencies in a PowerShell session.
Install-Bundle -Name Microsoft.PowerBi.Api -ProviderName NuGet -Scope CurrentUser -RequiredVersion 3.18.1 -Destination . -Force
However, in that location is currently an event with the NugGet provider inside PowerShell when information technology comes to installing packages with dependencies which leads to a dependency loop for some packages. Some folks accept managed to ready this by installing specific versions of certain dependencies before installing their target package, but I've not had the same luck with the Power BI library. Then how else can this be solved?
Using the -SkipDependencies switch
The Install-Package
cmdlet offers a -SkipDependencies
flag which lets you install a package without installing its dependencies along with it. Then this control does the play a trick on:
Install-Packet -Name Microsoft.PowerBi.Api -ProviderName NuGet -Telescopic CurrentUser -RequiredVersion three.eighteen.1 -SkipDependencies -Destination . -Force
Now to load that assembly into the session (the exact syntax here volition be determined by where you specified the -Destination
of the parcel):
$pbipath = Resolve-Path ".\Microsoft.PowerBI.Api.3.18.one\lib\netstandard2.0\Microsoft.PowerBI.Api.dll" [Organisation.Reflection.Assembly]::LoadFrom($pbipath)
Using classes from the imported associates
With this setup, we can start consuming the classes from that associates, for example:
$gatewayKeyObj = [Microsoft.PowerBI.Api.Models.GatewayPublicKey]::new("foo", "bar")
However, should you be using any classes that depend on other assemblies, yous'll run into an mistake. For example,
$basicCredentials = [Microsoft.PowerBI.Api.Models.Credentials.BasicCredentials]::new("ab", "cd")
returns the error:
MethodInvocationException: Exception calling ".ctor" with "2" argument(s): "Could not load file or assembly 'Microsoft.Residue.ClientRuntime, Version=2.0.0.0, Civilisation=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified."
As you've probably guessed, this is because of the -SkipDependencies
argument we specified when installing the initial package. We haven't installed the assemblies that some of the classes in the Ability BI assembly depend on. Then we do the same installation once more, but for the depended-upon package:
Install-Package -Name Microsoft.Rest.ClientRuntime -ProviderName NuGet -Scope CurrentUser -RequiredVersion 2.iii.22 -SkipDependencies -Destination . -Force $crpath = Resolve-Path ".\Microsoft.Balance.ClientRuntime.2.3.22\lib\netstandard2.0\Microsoft.Rest.ClientRuntime.dll" [System.Reflection.Associates]::LoadFrom($crpath)
And once that's in the session, the previous command which was declining should run without a hitch.
This process then but needs to be repeated for each of the bits of code you're consuming that depend on other assemblies. Luckily, the Ability BI associates doesn't depend on many others (either the net48
library or the netstandard2.0
library) and then the PowerShell doesn't go also messy. But if your target assembly has a large gear up of dependencies, fix yourself for a lot of boilerplate code. You can bank check the dependencies of your package on the NuGet site, or using a tool similar the NuGet Parcel Explorer.
Hope this helps!
Source: https://endjin.com/blog/2020/12/how-to-consume-a-nuget-package-in-powershell
0 Response to "Nuget Cannot Be Imported Again It Was Already Imported"
Post a Comment