Skip to content

Instantly share code, notes, and snippets.

@gitfvb
Last active October 17, 2022 09:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gitfvb/224d364175f46cc7ec052e7761085aa6 to your computer and use it in GitHub Desktop.
Save gitfvb/224d364175f46cc7ec052e7761085aa6 to your computer and use it in GitHub Desktop.
Install npgsql in PowerShell from Nuget

Good reference here: https://copdips.com/2018/05/setting-up-powershell-gallery-and-nuget-gallery-for-powershell.html

Here can you see your current clients to gather scripts, modules and packages

Get-PackageProvider

and you will get something like

PS C:\Users\Florian> Get-PackageSource

Name                             ProviderName     IsTrusted  Location
----                             ------------     ---------  --------
MyNuGet                          NuGet            False      https://www.nuget.org/api/v2
Nuget                            NuGet            False      http://www.nuget.org/api/v2
LocalRepo                        PowerShellGet    False      C:\Users\Florian\Downloads\20220920
PSGallery                        PowerShellGet    False      https://www.powershellgallery.com/api/v2

Add NuGet to you repositories, if not done yet. You can choose to add a -trusted flag to the command if you trust any package in that repository

Register-PackageSource -Name Nuget -Location "https://www.nuget.org/api/v2" –ProviderName Nuget

And check if this was successful

Get-PackageSource

Then you can install your scripts. To avoid dependency loops, load all packages first, then make them unique by name and version and install them like here

$pkg = Find-Package npgsql -IncludeDependencies -Verbose
$pkg | Select Name, Version -Unique | % { Install-Package -Name $_.Name -Scope CurrentUser -Source NuGet -Verbose -RequiredVersion $_.Version -SkipDependencies }
@gitfvb
Copy link
Author

gitfvb commented Oct 16, 2022

To use it, find out the installation location of the package with `Get-Package Npgsql" and load the needed dll version corresponding to your dll version like:
[System.Reflection.Assembly]::LoadFrom("C:\Users\Florian\AppData\Local\PackageManagement\NuGet\Packages\Npgsql.6.0.7\lib\net6.0\Npgsql.dll")

Then open the connection to the database:

$connString = "Host=host.port:5432;Username=user;Password=password;Database=db"
$conn = [Npgsql.NpgsqlConnection]::new($conn)
$conn.open()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment