Add-Type htmlagilitypack.dll fails - help?

Topics: Developer Forum, Project Management Forum, User Forum
Aug 30, 2013 at 2:55 PM
I've tried doing add-type .\htmlagilitypack.dll with powershell v2 and powershell v3 but continue to get the below error. I've also tried loading the DLL from the net20, net40-client, and net45 folders. All produce the below error.

PS D:\htmlagilitypack\net40> Add-Type .\HtmlAgilityPack.dll
Add-Type : c:\Users\XXXX\AppData\Local\Temp\bww4lrx3.0.cs(1) : Unexpected character '\'
c:\Users\XXXX\AppData\Local\Temp\bww4lrx3.0.cs(1) : >>> .\HtmlAgilityPack.dll
At line:1 char:1
  • Add-Type .\HtmlAgilityPack.dll
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : InvalidData: (c:\Users...d character '\':CompilerError) [Add-Type], Exception
    • FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand
Add-Type : c:\Users\XXXX\AppData\Local\Temp\bww4lrx3.0.cs(1) : A namespace cannot directly contain members such
as fields or methods
c:\Users\XXXX\AppData\Local\Temp\bww4lrx3.0.cs(1) : >>> .\HtmlAgilityPack.dll
At line:1 char:1
  • Add-Type .\HtmlAgilityPack.dll
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : InvalidData: (c:\Users..elds or methods:CompilerError) [Add-Type], Exception
    • FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand
Add-Type : Cannot add type. There were compilation errors.
At line:1 char:1
  • Add-Type .\HtmlAgilityPack.dll
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : InvalidData: (:) [Add-Type], InvalidOperationException
    • FullyQualifiedErrorId : COMPILER_ERRORS,Microsoft.PowerShell.Commands.AddTypeCommand
Mar 20, 2014 at 11:36 AM
Edited Mar 20, 2014 at 11:38 AM
I used this code and it works at home (Windows 8.1) . Now I am at work (Windows 8.1 in domain) and I get the same error .

Now I am trying something smart to detect the CLRversion and to loadt the corresponding Assembly, but without success.
$CLRVersion = "Net"+ $PSVersionTable.CLRVersion.Major + $PSVersionTable.CLRVersion.Minor
$HtmlAgilityPack = Join-Path (Join-Path (Split-Path $MyInvocation.MyCommand.Path) $CLRVersion) HtmlAgilityPack.dll
Add-Type -Path $HtmlAgilityPack
HappyWindowsAdmin, try to use the following construct:
[system.reflection.assembly]::LoadFile('c:\Data\Development\ServiceStar\HtmlAgilityPack.dll')
You must use an absolute path here.

Solved!!!!!

The assembly I am trying is from an internet location. PowerShell does not like to load assemblies from the Internet. To solve , do this:
  • Open properties of the DDL file
  • On the General page, under Security, clock [Unblock]
Jul 12, 2014 at 1:08 PM
Sikkepitje, thanks for sharing the resolution... I was hitting the head for last two hours!

Thanks a lot!!