PowerShell中无法创建Ethernet/IP类实例的问题求助
PowerShell中无法创建Ethernet/IP类实例问题
问题背景
在PowerShell中尝试创建Ethernet/IP类实例时,即使已在脚本顶部加载程序集,仍遇到程序集未加载或无法实例化的问题,该功能在Visual Studio中可正常运行。
PowerShell测试脚本
# Load the EEIP.dll Add-Type -Path C:\myDlls\EIP\EEIP.dll Add-Type Sres.Net.EEIP # Define PLC connection parameters $IPAddress = "192.168.1.190" $Timeout = 2000 # Timeout value in milliseconds try { # Create an instance of the Ethernet/IP communication class $plc = New-Object EthernetIP.EEIPClient } catch { Write-Host "Error communicating with PLC:" $_.Exception.Message }
Visual Studio正常运行代码
using Sres.Net.EEIP; class Program { static void Main(string[] args) { EEIPClient eeipClient = new EEIPClient(); eeipClient.RegisterSession("192.168.1.190"); eeipClient.UnRegisterSession(); } }
DLL加载错误排查及信息
为排查DLL加载问题,使用以下脚本:
try { Add-Type -Path "C:\myDlls\EIP\EEIP.dll" #Add-Type -Path "C:\OPC\OpcComRcw.dll" } catch [System.Reflection.ReflectionTypeLoadException] { Write-Host "Message: $($_.Exception.Message)" Write-Host "StackTrace: $($_.Exception.StackTrace)" Write-Host "LoaderExceptions: $($_.Exception.LoaderExceptions)" }
得到错误信息:
Add-Type : Could not load file or assembly 'file:///C:\myDlls\EIP\EEIP.dll' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)
已执行的解决步骤及剩余问题
通过右键DLL文件,在属性中勾选解除阻止并重启PowerShell后,成功解决了上述加载错误,但仍然无法创建Ethernet/IP类实例。
内容的提问来源于stack exchange,提问作者user112307
相关产品推荐
相关产品推荐

