Add-Type -path "c:\Oracle\Oracle.ManagedDataAccess.dll" fails in Powershell - 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.

Related

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

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?

Issue with System.IO.Compression

I have a PowerShell v2 script with .Net 4.5 which compresses old files. Like this:
[Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem")
...
$files = Get-Childitem ...
...
$zip = [System.IO.Compression.ZipFile]::Open($arcpath + $curname, "Create")
foreach ($onefile in $files) {
[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zip, $onefile.FullName, $onefile.Name, "Optimal") | out-null
}
It worked fine until I have installed .Net 4.6.1 (which is needed for another project).
Now it throws an error:
The following exception occurred while retrieving member "Open": "Could not load
file or assembly 'System.IO.Compression, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089' or one of its dependencies.
Reference assemblies should not be loaded for execution. They can only be loaded
in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)"
At line:1 char:7
+ $zip = <<<< [System.IO.Compression.ZipFile]::Open('C:\Temp\1.zip', "Create")
+ CategoryInfo : NotSpecified: (:) [], ExtendedTypeSystemException
+ FullyQualifiedErrorId : CatchFromBaseGetMember
I installed WMF 5.1 to use native ps5 compression functions. But it also throws an error:
Compress-Archive -Path $files -DestinationPath C:\Temp\Script\test.zip -CompressionLevel Optimal
Add-Type : Could not load file or assembly 'System.IO.Compression, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies.
Reference assemblies should not be loaded for execution. They can only be loaded
in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)
At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:469 char:9
+ Add-Type -AssemblyName System.IO.Compression
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Add-Type], BadImageFormatException
+ FullyQualifiedErrorId : System.BadImageFormatException,Microsoft.PowerShell.Commands.AddTypeCommand
How can I fix it?
Deinstalled NET 4.6.1, installed NET 4.6.2. Issue was fixed.
Finally the issue was discovered. We use a custom solution which is using NLog. NLog adds a reference to System.IO.Compression to the solution. During the installation of the solution it deploys the dll (probably the old one or in some improper way). We excluded dll from the installer and now everything work fine.

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

Powershell load dll got error: Add-Type : Could not load file or assembly 'WebDriver.dll' or one of its dependencies. Operation is not supported

I want to use PowerShell with selenium and download selenium from http://www.java2s.com/Code/Jar/s/Downloadseleniumremotedriver2350jar.htm . When I try to load one of the dlls, I got errors. Hope someone can help me with it.
This is my system info.
OS Name: Microsoft Windows 7 Enterprise
OS Version: 6.1.7601 Service Pack 1 Build 7601
OS Manufacturer: Microsoft Corporation
This is my PowerShell info.
PS C:\Users\test\Downloads\selenium-dotnet-2.35.0\net40> $psversiontable
Name Value
---- -----
PSVersion 4.0
WSManStackVersion 3.0
SerializationVersion 1.1.0.1
CLRVersion 4.0.30319.18052
BuildVersion 6.3.9421.0
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion 2.2
PS C:\Users\test\Downloads\selenium-dotnet-2.35.0\net40>
This is the errors I got when trying to load the dll.
PS C:\Users\test\Downloads\selenium-dotnet-2.35.0\net40> Add-Type -Path .\WebDriver.dll
Add-Type : Could not load file or assembly 'file:///C:\Users\test\Downloads\selenium-dotnet-2.35.0\net40\WebDriver.dll' or one of its dependencies. Operation is
not supported. (Exception from HRESULT: 0x80131515)
At line:1 char:1
+ Add-Type -Path .\WebDriver.dll
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Add-Type], FileLoadException
+ FullyQualifiedErrorId : System.IO.FileLoadException,Microsoft.PowerShell.Commands.AddTypeCommand
PS C:\Users\test\Downloads\selenium-dotnet-2.35.0\net40> [reflection.assembly]::LoadFrom(".\WebDriver.dll")
Exception calling "LoadFrom" with "1" argument(s): "Could not load file or assembly 'file:///C:\Users\test\WebDriver.dll' or one of its dependencies. The system
cannot find the file specified."
At line:1 char:1
+ [reflection.assembly]::LoadFrom(".\WebDriver.dll")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : FileNotFoundException
PS C:\Users\test\Downloads\selenium-dotnet-2.35.0\net40> [reflection.assembly]::LoadFrom("WebDriver.dll")
Exception calling "LoadFrom" with "1" argument(s): "Could not load file or assembly 'file:///C:\Users\test\WebDriver.dll' or one of its dependencies. The system
cannot find the file specified."
At line:1 char:1
+ [reflection.assembly]::LoadFrom("WebDriver.dll")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : FileNotFoundException
PS C:\Users\test\Downloads\selenium-dotnet-2.35.0\net40> [reflection.assembly]::LoadFrom("C:\Users\test\Downloads\selenium-dotnet-2.35.0\net40\WebDriver.dll")
Exception calling "LoadFrom" with "1" argument(s): "Could not load file or assembly 'file:///C:\Users\test\Downloads\selenium-dotnet-2.35.0\net40\WebDriver.dll' or
one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)"
At line:1 char:1
+ [reflection.assembly]::LoadFrom("C:\Users\test\Downloads\selenium-dotnet-2.35.0 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : FileLoadException
PS C:\Users\test\Downloads\selenium-dotnet-2.35.0\net40> [reflection.assembly]::LoadFile("C:\Users\test\Downloads\selenium-dotnet-2.35.0\net40\WebDriver.dll")
Exception calling "LoadFile" with "1" argument(s): "An attempt was made to load an assembly from a network location which would have caused the assembly to be sandboxed
in previous versions of the .NET Framework. This release of the .NET Framework does not enable CAS policy by default, so this load may be dangerous. If
this load is not intended to sandbox the assembly, please enable the loadFromRemoteSources switch. See http://go.microsoft.com/fwlink/?LinkId=155569 for more
information."
At line:1 char:1
+ [reflection.assembly]::LoadFile("C:\Users\test\Downloads\selenium-dotnet-2.35.0 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : NotSupportedException
PS C:\Users\test\Downloads\selenium-dotnet-2.35.0\net40>
If you downloaded the .DLLs from the internet, Powershell by default will not trust them. You can do one of two things here:
Unblock the content. Here's a guide with some elaboration on the problem, but essentially you just need to right-click on the downloaded file, select Properties, and click 'Unblock' in the general tab. You can unblock content directly from powershell by using the Unblock-File cmdlet.
Change your execution policy. This can also allow malicious scripts that you download to run, so be cautious.
Specifically reviewing and unblocking content you trust seems the better route, especially for your case since you seem like you would only be doing this once and you trust the package.
Edit: If that didn't solve that, the other experience I've had with this type of error is when Powershell didn't the same version of the .NET runtime as the assembly. See this question for some context, where Powershell 2.0 ran the .NET 2 runtime and the asker needed .NET 4 for some assemblies.
You're not using the same version of PS so your mileage may vary, but I would try creating/editing a config file per the linked answer to support the runtime of the DLLs you are using.
This worked for me: (from https://stackoverflow.com/a/19957173/107161)
In files:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe.config C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<runtime>
<loadFromRemoteSources enabled="true"/>
</runtime>
</configuration>
Run as Administrator has solved the issue, although my get-executionpolicy is allsigned