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

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

Related

Trying to use BitsTransfer (Powershell) or bitsadmin to transfer files when booted into WinRE

Trying to use BitsTransfer (Powershell) or bitsadmin to transfer files when booted into WinRE.
I am able to get the file using invoke-webrequest for the same paths. None of the Bits commands give much information other than "The device is not ready". My searches only return information on Windows update.
I have tried restarting services.
When I run Start-BitsTransfer I get:
VERBOSE: Performing the operation "New" on target "BitsTransfer".
Start-BitsTransfer : The device is not ready.
The device is not ready.
At line:1 char:1
Start-BitsTransfer "http://...Source...
+ CategoryInfo : NotSpecified: (:) [Start-BitsTransfer], FileNotFoundException
+ FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.BackgroundIntelligentTransfer.Management.NewBitsTransferCommand
Get-BitsTransfer command gives me:
get-bitstransfer : The device is not ready.
The device is not ready.
At line:1 char:1
get-bitstransfer
+ CategoryInfo : NotSpecified: (:) [Get-BitsTransfer], FileNotFoundException
+ FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.BackgroundIntelligentTransfer.Management.GetBitsTransfer
BitsAdmin.exe gives me:
BITSADMIN version 3.0
BITS administration utility.
(C) Copyright Microsoft Corp.
Unable to connect to BITS - 0x80070015
The device is not ready.

Get-CsOnlinePowerShellEndpoint : Parameter set cannot be resolved using the specified named parameters

We have a Powershell script which is using SkypeForBusiness module. This is being migrated to MicrosoftTeams and we found a problem with Get-CsOnlinePowerShellEndpoint:
PS C:\WINDOWS\system32> Get-CsOnlinePowerShellEndpoint -TargetDomain "xxxxx.onmicrosoft.com"
Get-CsOnlinePowerShellEndpoint : Parameter set cannot be resolved using the specified named parameters.
At line:1 char:1
+ Get-CsOnlinePowerShellEndpoint -TargetDomain "xxxxx.onmicrosoft. ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-CsOnlinePowerShellEndpoint], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.Teams.ConfigApi.Cmdlets.GetPowerShellEndpointCmdlet
The versions we are using are:
Windows Server 2019
PowerShell 5.1.18362.1474
MicrosoftTeams Module: 2.3.1 (version 2.0.0 fails too)
This same command is working fine on the old installation with Windows server 2016, PS 5.1 and SkypeOnlineConnector.
Any help will be much appreciated.
Thanks
A workaround for this to work is to add parameter OverrideDiscoveryUri with $null value. Example:
Get-CsOnlinePowerShellEndpoint -TargetDomain "xxxxx.onmicrosoft.com" -OverrideDiscoveryUri $null
The Stacktrace for the error was pointing to an AmbiguousParameterSet so it seems that there is a mix of parameters, even when the unique mandatory for this cmdlet is TargetDomain.

Set-PnPSite -LogoFilePath throws error? Which version of SharePointPnPPowerShellOnline required? Mine is 3.20.2004.0

Which version of SharePointPnPPowerShellOnline does support below command? Mine is 3.20.2004.0 and trying to update Modern teamsite in sharepoint online environment.
Set-PnPSite -LogoFilePath c:\images\mylogo.png
Set-Variable : A parameter cannot be found that matches parameter name 'LogoFilePath'.
At line:1 char:13
+ set pnpsite -LogoFilePath C:\Project\Images\FPASiteLogo.png
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-Variable], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.SetVariableCommand
Tested the same version PnP PowerShell which is the latest version and LogoFilePath parameter is available, please see the PowerShell snippet:
But the site needs to be a Moder Site:
Reference:
Set-PnPSite

Install MS Access DAO Primary Interop Assembly for Use with PowerShell Script

I am trying to automate Microsoft Access 2016 with PowerShell, but I appear to be missing an Office Primary Interop Assembly called Microsoft.Office.Interop.Access.Dao. I've tried a few approaches in PowerShell, and these are the errors that I get:
PS > $app.Application.OpenCurrentDatabase()
The following exception occurred while retrieving member "Application": "Could not load file or assembly 'Microsoft.Office.Interop.Access.Dao, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' or one of its dependencies. The system cannot find the file specified."
At line:1 char:1
+ $app.Application.OpenCurrentDatabase()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], ExtendedTypeSystemException
+ FullyQualifiedErrorId : CatchFromBaseGetMember
PS > Add-Type -Assembly "Microsoft.Office.Interop.Access.Dao"
Add-Type : Could not load file or assembly 'Microsoft.Office.Interop.Access.Dao, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' or one of its dependencies. The system cannot find the file specified.
At line:1 char:1
+ Add-Type -Assembly "Microsoft.Office.Interop.Access.Dao"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Add-Type], FileNotFoundException
+ FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.PowerShell.Commands.AddTypeCommand
Please note the difference in version (15.0.0.0 vs 12.0.0.0).
The only version of Office that I have installed on this machine is Office 2016 via Office 365 download. I have run gacutil /l from a Visual Studio Command Prompt, and I see that the other Office PIAs, version 15.0.0.0, are installed.
According to the NuGet Web site, I could download the assembly via NuGet Package Manager Console, if I were working on a Visual Studio Solution, but I cannot install the assembly with PowerShell:
PS > Install-Package Microsoft.Office.Interop.Access.Dao
Install-Package : No match was found for the specified search criteria and package name 'Microsoft.Office.Interop.Access.Dao'. Try Get-PackageSource to see all available registered package sources.
At line:1 char:1
+ Install-Package Microsoft.Office.Interop.Access.Dao
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Microsoft.Power....InstallPackage:InstallPackage) [Install-Package], Exception
+ FullyQualifiedErrorId : NoMatchFoundForCriteria,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackage
My questions are:
How can I install Microsoft.Office.Interop.Access.Dao to the GAC
so I can reference it in PowerShell scripts?
If nuget.org is listed as a PackageSource in PowerShell, why can't I use the Install-Package command to install this assembly?
Why did this assembly not get installed with Office 2016/365?
The accepted answer will provide at least a solution to my first question - How can I install Microsoft.Office.Interop.Access.Dao to the GAC so I can reference it in PowerShell scripts?

Unable to Use Windows Update COM object in RemotePS sessions

Im trying to change the windows update settings in a remote server using powershell, but when i run the below commands it gives me an error.
PS C:\Windows\system32> Enter-PSSession opalisbinary
[opalisbinary]: PS C:\Users\superv\Documents> $AUSettings = (New-Object -com "Microsoft.Update.AutoUpdate").Settings
[opalisbinary]: PS C:\Users\superv\Documents> $AUSettings.NotificationLevel=2
Property 'NotificationLevel' cannot be found on this object; make sure it exists and is settable.
At line:1 char:14
+ $AUSettings. <<<< NotificationLevel=2
+ CategoryInfo : InvalidOperation: (NotificationLevel:String) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound
[opalisbinary]: PS C:\Users\superv\Documents> $AUSettings.IncludeRecommendedUpdates=$true
Property 'IncludeRecommendedUpdates' cannot be found on this object; make sure it exists and is settable.
At line:1 char:14
+ $AUSettings. <<<< IncludeRecommendedUpdates=$true
+ CategoryInfo : InvalidOperation: (IncludeRecommendedUpdates:String) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound
[opalisbinary]: PS C:\Users\superv\Documents> $AUSettings.Save()
You cannot call a method on a null-valued expression.
At line:1 char:18
+ $AUSettings.Save <<<< ()
+ CategoryInfo : InvalidOperation: (Save:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Although when i run it locally on the server it works fine...
Could anyone please help me.
Does it work when you connect via Remote desktop and run the commands? It also depends on the bittnes. I don't know the Microsoft.Update.AutoUpdate, so I'm just guessing, but - is it available for x64 version of PowerShell? In other words if you run it locally in x64 version and x86, do you see the errors?
I am sorry, I could not find a real answer. What I found is this link which states that remote PS sessions can be used with SCCM 2012, but they could not with SCCM 2007.
This does not help you, but it seems that not all COM objects are compatible with remote PS sessions.