PowerShell cannot load assembly in slt RRS feed

  • Question

  • StackOverflow: https://stackoverflow.com/questions/47097537/powershell-cannot-load-assembly-in-class

    I have the following code:

        using namespace System.Xml.Linq
        using assembly System.Xml.Linq
        class error {
                .PARAMETER type
                The type of the error.
            [string] $type
                .PARAMETER message
                The error message.
            [string] $message
            error() {}
            [XElement] ToXElement() {
                $el = New-Object XElement([XName] "error")
                $attrType = New-Object XAttribute([XName] "type"), $this.type
                $elDesc = New-Object XElement([XName] "desc"), $this.message
                return $el

    When I try to import the module using `using module error.psm1`, PowerShell console would say `Unable to find type [XElement]` and `[XName]`. I've tried both absolute and relative class names but still to no avail. 

    Is there a way I can `using` the module without having to run `Add-Type` before I run the script that uses the module?

    Environment `$PSVersionTable`:

    PS D:\DiscoveryRunner> $PSVersionTable

        Name                           Value
        ----                           -----
        PSVersion                      5.1.14409.1012
        PSEdition                      Desktop
        PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
        BuildVersion                   10.0.14409.1012
        CLRVersion                     4.0.30319.42000
        WSManStackVersion              3.0
        PSRemotingProtocolVersion      2.3

    Friday, November 3, 2017 2:12 PM

All replies

  • To load a script module you must use the module name and not the psm1 file name.

    using module error


    Friday, November 3, 2017 2:42 PM
  • I tried both, same results.
    Friday, November 3, 2017 3:50 PM
  • If the module is not built correctly then it may not load using that method.  The module needs to correctly specify dependencies.

    Note also that "error" is a key word and should not be used for a class.


    Friday, November 3, 2017 3:56 PM
  • The actual class name is not error, I used it as an example.

    The module loads correctly when I manually Add-Type in the console, so I don't see how that's relevant.

    Friday, November 3, 2017 4:14 PM
  • When you load the assembly you also need to add the names space second.


    using namespace System.Xml.Linq
    using assembly System.Xml.Linq

    Should be this:

    using assembly System.Xml.Linq
    using namespace System.Xml.Linq

    PS D:\scripts> using assembly System.Xml.Linq
    PS D:\scripts> using namespace System.Xml.Linq
    PS D:\scripts> class error{
    >>     [string]$type = 'test'
    >>     [string]$message ='new message'
    >>     error() { }
    >>         [XElement]ToXElement() {
    >>         $el = New-Object XElement([XName]'error')
    >>         $attrType = New-Object XAttribute([XName]'type'), $this.type
    >>         $el.Add($attrType)
    >>         $elDesc = New-Object XElement([XName] "desc"), $this.message
    >>         $el.Add($elDesc)
    >>         return $el
    >>     }
    >> }
    PS D:\scripts> $e = [error]::New()
    PS D:\scripts> $e.ToXElement()
    FirstAttribute : type="test"
    HasAttributes  : True
    HasElements    : True
    IsEmpty        : False
    LastAttribute  : type="test"
    Name           : error
    NodeType       : Element
    Value          : new message
    FirstNode      : <desc>new message</desc>
    LastNode       : <desc>new message</desc>
    NextNode       :
    PreviousNode   :
    BaseUri        :
    Document       :
    Parent         :
    LineNumber     : 0
    LinePosition   : 0


    Friday, November 3, 2017 4:19 PM
  • I've tried that order; still fails with the same message. Try `using module error`
    Monday, November 6, 2017 11:29 AM
  • Works perfectly on WMF 5.1.

    As noted you cannot load a module the way you are trying to load it and the "using" commands must be the first commands in a script.


    Monday, November 6, 2017 2:38 PM