"Unable to load one or more of the requested types" Exception occurred when installing npgsql - postgresql

all.
I installed npgsql on Windows Server 2022 by the following step.
PS > Install-Package -Name Npgsql -Source https://www.nuget.org/api/v2 -SkipDependencies
PS > Add-Type -Path "C:\Program Files\PackageManagement\NuGet\Packages\Npgsql.6.0.5\lib\net6.0\Npgsql.dll"
However, I got the following error.
Add-Type : Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more
information.
At line:1 char:1
+ Add-Type -Path "C:\Program Files\PackageManagement\NuGet\Packages\Npg ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Add-Type], ReflectionTypeLoadException
+ FullyQualifiedErrorId : System.Reflection.ReflectionTypeLoadException,Microsoft.PowerShell.Commands.AddTypeComma
nd
Would you tell me the solution?

Related

PowerShell PackageManagement - How to uninstall a package provider?

Troubleshooting i tried uninstalling nuget. NuGetProvider-2.8.5.208.dll to be specific.
I have never worked with Power Shell before and am totally lost.
I found this question that helped me find nuget and create a command to delete it.
But I'm unable to follow through.
I did start powershell as administrator.
Here the input:
(Get-PackageProvider|where-object{$_.name -eq "nuget"}).ProviderPath|Remove-Item -force Restart-Computer
The error message:
Remove-Item : The input object cannot be bound to any parameters of the command because the command does not accept pipeline input or the input and its properties do not match any of the parameters that accept pipeline input.
and its properties do not match any of the parameters that accept pipeline input.
line:1 character:70
+ ... _.name -eq "nuget"}).ProviderPath|Remove-Item -force Restart-Computer
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (C:\Users\XXX ...GetProvider.dll:String) [Remove-Item], ParameterBindingException
+ FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.RemoveItemCommand
How can i solve this and successfully uninstall it?
Thanks for your answer!
Still no luck. Here the error:
Remove-Item : The element C:\Users\XXX\AppData\Local\PackageManagement\ProviderAssemblies\nuget\2.8.5.208
\Microsoft.PackageManagement.NuGetProvider.dll cannot be removed: Access to the path "C:\Users\XXX\AppData\Local\PackageManagement\ProviderAssemblies\nuget\2.8.5.208\Microsoft.PackageManagement.NuGetProvide
r.dll" denied.
Line:1 Character:44
+ (Get-PackageProvider NuGet).ProviderPath | Remove-Item -Force
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (C:\Users\XXX ...GetProvider.dll:FileInfo) [Remove-Item], Unauthoriz
edAccessException
+ FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand

Error during installing PSWindows Update module

I was going to automate some regular task on client computers using Powershell module. In this way, PSWINDOWSUPDATE is one of bests. But, I receive an error during install that:
PS C:\Users\stackoverflow> install-module -name pswindowsupdate -scope currentuser
PackageManagement\Install-Package : Package 'PSWindowsUpdate' failed to be installed because:
Access to the path 'C:\Users\stackoverflow\AppData\Local\Temp\ipufiq2h\PSWindowsUpdate.dll' is denied.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:1809 char:21
+ ... $null = PackageManagement\Install-Package #PSBoundParameters
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidResult: (PSWindowsUpdate:String) [Install-Package], Exception
+ FullyQualifiedErrorId : Package '{0}' failed to be installed because: {1},Microsoft.PowerShell.PackageManagement
.Cmdlets.InstallPackage
running the command in administrative mode and without -scope switch didn't solve the problem.
Edit1: The problem is solved. But there is another problem. When I call import-module pswindowsupdate following error occurs:
import-module : Errors occurred while loading the format data file:
C:\Users\stackoverflow\Documents\WindowsPowerShell\Modules\pswindowsupdate\2.2.0.2\PSWindowsUpdate.Format.ps1xml, ,
C:\Users\stackoverflow\Documents\WindowsPowerShell\Modules\pswindowsupdate\2.2.0.2\PSWindowsUpdate.Format.ps1xml: The file was skipped because of the following validation exception: File
C:\Users\stackoverflow\Documents\WindowsPowerShell\Modules\pswindowsupdate\2.2.0.2\PSWindowsUpdate.Format.ps1xml cannot be loaded because running scripts is disabled on this system. For more
information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170..
At line:1 char:1
+ import-module pswindowsupdate
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Import-Module], RuntimeException
+ FullyQualifiedErrorId : FormatXmlUpdateException,Microsoft.PowerShell.Commands.ImportModuleCommand
Use Get-ExecutionPolicy to set it e.g. to RemoteSigned:
Get-ExecutionPolicy -List
Set-ExecutionPolicy RemoteSigned
also see here

Compress folder using Powershell from SQLSERVER runtime

I need to zip a folder using powershell from within SQLPS runtime. I tried the following from PS SQLSERVER:\> prompt:
$dest = "C:\SomePath\FolderToBeZipped"
$zipfile = $dest + ".zip"
Add-Type -assembly "system.io.compression.filesystem"
[io.compression.zipfile]::CreateFromDirectory($dest,$zipfile)
When run, I get the below error :
Add-Type : Cannot add type. The assembly 'system.io.compression.filesystem' could not be found.
At C:\SomePath\Compress.ps1:10 char:9
+ Add-Type <<<< -assembly "system.io.compression.filesystem"
+ CategoryInfo : ObjectNotFound: (system.io.compression.filesystem:String) [Add-Type], Exception
+ FullyQualifiedErrorId : ASSEMBLY_NOT_FOUND,Microsoft.PowerShell.Commands.AddTypeComm
and
Add-Type : Cannot add type. One or more required assemblies are missing.
At C:\SomePath\Compress.ps1:10 char:9
+ Add-Type <<<< -assembly "system.io.compression.filesystem"
+ CategoryInfo : InvalidData: (:) [Add-Type], InvalidOperationException
+ FullyQualifiedErrorId : ASSEMBLY_LOAD_ERRORS,Microsoft.PowerShell.Commands.AddTypeCommand
But if I exit out of PS SQLSERVER:\> runtime and execute from normal PS command prompt it is able to compress successfully. Please help how to compress from SQLSERVER powershell runtime. I even tried to invoke a separate .ps1 script (for compressing) using Invoke-Expression from within PS SQLSERVER:\> runtime, but failed. Please help

Script Only Runns Sucessfully 50-60% of the time

I'm sending a zip and the powershell script to computers in our organisation through our management software and then executing the powershell script through command prompt. The script unzips the folder, installs a program and adds some registry keys to the registry.
If the machine doesnt say that a policy is preventing execution of the script, it runs as expected, the program is installed and the registry keys are imported. But if it does give the warning and I set the policy to unrestricted I get the below error error. I am on Windows 8.1 and using Powershell 4.0
Id not be found.
At C:\Temp\tps1.ps1:5 char:9
+ Add-Type <<<< -assembly "system.io.compression.filesystem"
+ CategoryInfo : ObjectNotFound: (system.io.compression.filesyste
m:String) [Add-Type], Exception
+ FullyQualifiedErrorId : ASSEMBLY_NOT_FOUND,Microsoft.PowerShell.Commands
.AddTypeCommand
Add-Type : Cannot add type. One or more required assemblies are missing.
At C:\Temp\tps1.ps1:5 char:9
+ Add-Type <<<< -assembly "system.io.compression.filesystem"
+ CategoryInfo : InvalidData: (:) [Add-Type], InvalidOperationExc
eption
+ FullyQualifiedErrorId : ASSEMBLY_LOAD_ERRORS,Microsoft.PowerShell.Comman
ds.AddTypeCommand
Unable to find type [io.compression.zipfile]: make sure that the assembly conta ining this type is loaded.
At C:\Temp\tps1.ps1:7 char:25
+ [io.compression.zipfile] <<<< ::ExtractToDirectory($BackUpPath, $destination) + CategoryInfo : InvalidOperation: (io.compression.zipfile:String
) [], RuntimeException
+ FullyQualifiedErrorId : TypeNotFound
anyone have any ideas?
on systems where .NET 4.5 is not installed, use:
$BackUpPath = "C:\Temp\Install.zip"
$Destination = "C:\Temp"
$shell_app=new-object -com shell.application
$zip_file = $shell_app.namespace($BackUpPath )
$destination = $shell_app.namespace($Destination )
$destination.Copyhere($zip_file.items(),0x4)
on systems with.NET 4.5 use:
Add-Type -assembly "system.io.compression.filesystem"
[io.compression.zipfile]::ExtractToDirectory($BackUpPath, $destination)
A script on technet combines the above 2 methods.
On PowerShell V5 , this is much simplified:
Expand-Archive $BackUpPath -destination $Destination
there are other ways to unzip files using 3rd party libraries such as 7-zip, etc but that creates a dependency.
Note: Powershell execution policies can be set using group policy. see link here

Add-Type -path "c:\Oracle\Oracle.ManagedDataAccess.dll" fails in Powershell

I'm attempting to follow the instructions according to this post for making an Oracle connection in PowerShell. When I try to load the Oracle .dll I get a operation not supported error.
PS C:\windows\system32> Add-Type -path "C:\Oracle\Oracle.ManagedDataAccess.dll"
Add-Type : Could not load file or assembly 'file:///C:\Oracle\Oracle.ManagedDataAccess.dll' or one of its dependencies.
Operation is not supported. (Exception from HRESULT: 0x80131515)
At line:1 char:9
+ Add-Type <<<< -path "C:\Oracle\Oracle.ManagedDataAccess.dll"
+ CategoryInfo : NotSpecified: (:) [Add-Type], FileLoadException
+ FullyQualifiedErrorId : System.IO.FileLoadException,Microsoft.PowerShell.Commands.AddTypeCommand
you can try this way:
[System.Reflection.Assembly]::LoadFrom("C:\Oracle\Oracle.ManagedDataAccess.dll")
verify to have access to folder and unlocked the file.
Was resolved by adding the correct entries in TNSNAMES.ORA and restarting powershell.