Skip to content

Using external types / classes in Custom PowerShell Classes (v5) results in Parsing Error #2074

Closed

Description

@Lindmeier

The Issue

I am trying to create a PowerShell-Project with my own custom PowerShell-Classes.
I use explicit type defintion for all attributes of my custom classes. For all integrated .Net-Classes there is now problem, but for other classes (for example external dll) it does not work.
It seems like, that you have to create a Wrapper script which loads the dll / Imports the module and afterwards starts the actual script project (with custom classes) by dotsourcing.

Unfortunately this workaround (Wrapper-Script) does not work out in my scenario --> A automation / orchestration engine only accepts one script...

So is there a way to fix this? The Parser should not throw an error, as you declare (using statement) that you will use classes out of this namespace. If there is something wrong with the class itself.. .this should be a runtime exception, or am I wrong?

Steps to reproduce

using namespace Microsft.ActiveDirectory.Management
#These two do not work, too 
#using module ActiveDirectory
#Import-Module ActiveDirectory

class Test
{
    [Microsoft.ActiveDirectory.Management.ADDomain] $domain;

}
 ...

Expected behavior

no Error Message

Actual behavior

At line:9 char:6
+     [Microsoft.ActiveDirectory.Management.ADDomain] $domain;
+      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [Microsoft.ActiveDirectory.Management.ADDomain].
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : TypeNotFound

In this case with an integrated .NET class it perfectly works.

using namespace System.DirectoryServices.ActiveDirectory 

Class Test 
{ 
    [Domain] $dom = [Domain]::GetCurrentDomain() 
} 

Activity

  1. added
    WG-Language
    Issue-Question
    on Sep 6, 2016
  2. SteveL-MSFT commented on Oct 28, 2016

    @SteveL-MSFT
    Member

    I believe we implemented 'using assembly' specifically for this use case

  3. joeyaiello commented on Feb 16, 2017

    @joeyaiello
    Contributor

    According to Jason Shirk (@lzybkr), using assembly is meant for this, but it has not been implemented for this use case yet.

  4. added
    Issue-Enhancement
    Up-for-Grabs
    and removed
    Issue-Question
    Resolution-Answered
    on Feb 16, 2017
  5. added
    WG-Engine
    and removed
    Up-for-Grabs
    on Feb 16, 2017
  6. added
    Up-for-Grabs
    and removed
    WG-Engine
    on Feb 16, 2017
  7. added this to the milestone on Mar 4, 2017
  8. 12 remaining items

  9. SeeminglyScience commented on May 22, 2019

    @SeeminglyScience
    Contributor

    Rob Holt (@rjmholt) correct me if I'm wrong, but my understanding is that in order for this to work then at least the following would need to be done:

    1. Classes would need to be defined with MetadataBuilder instead of the System.Reflection.Emit API's
    2. Not yet loaded assemblies specified by using assembly would need to be analyzed via a MetadataReader if classes are defined in the file

    Has there been any discussion on if this would even be accepted as a contribution? As you know, code that utilizes those API's would be exponentially more complex than the current code.

    I'd like to take on at some point if no one beats me to it, but I'm unsure if it would be considered worth maintaining.

  10. lzybkr commented on May 22, 2019

    @lzybkr
    Contributor

    Patrick Meinecke (@SeeminglyScience) - Using MetadataReader was my original plan when I introduced using assembly - I even wanted to use it for command analysis to avoid loading assemblies.

    I can't comment on MetadataBuilder. I thought Reflection.Emit was up to the task, but I could easily be mistaken. One important aspect of defining types though - it should ideally be done in a collectible assembly.

  11. SeeminglyScience commented on May 22, 2019

    @SeeminglyScience
    Contributor

    Using MetadataReader was my original plan when I introduced using assembly - I even wanted to use it for command analysis to avoid loading assemblies.

    Yes! I've been on and off working on a metadata only version of CmdletInfo for that 🙂

    I can't comment on MetadataBuilder. I thought Reflection.Emit was up to the task, but I could easily be mistaken.

    It is as long as we can load the assembly prior to actually defining the type which should be doable. Now that I think about it, I think I started down that path because I wanted the exceptions TypeBuilder.CreateType() throws, but at parse time. MetadataBuilder doesn't really do any validation though, so any of that logic would need to be written from scratch utilizing MetadataReader anyway.

    One important aspect of defining types though - it should ideally be done in a collectible assembly.

    Ah yeah, MetadataBuilder is out then. I thought they weren't collectible for some reason.

    Should be a lot less complicated with just MetadataReader then. Thanks Jason Shirk (@lzybkr)!

  12. rjmholt commented on Jan 29, 2020

    @rjmholt
    Collaborator

    Just while I'm compiling some information in this area on this...

    Here's where using assembly is acted on:

    asms.Add(LoadAssembly(usingStmt.Name.Value, usingStmt.Extent.File));

    Here's where that LoadAssembly() call goes:

    private static Assembly LoadAssembly(string assemblyName, string scriptFileName)
    {
    var assemblyFileName = assemblyName;
    Assembly assembly = null;
    try
    {
    if (!string.IsNullOrEmpty(scriptFileName) && !Path.IsPathRooted(assemblyFileName))
    {
    assemblyFileName = Path.GetDirectoryName(scriptFileName) + "\\" + assemblyFileName;
    }
    #if !CORECLR
    if (!File.Exists(assemblyFileName))
    {
    Microsoft.CodeAnalysis.GlobalAssemblyCache.ResolvePartialName(assemblyName, out assemblyFileName);
    }
    #endif
    if (File.Exists(assemblyFileName))
    {
    assembly = Assembly.LoadFrom(assemblyFileName);
    }
    }
    catch
    {
    }
    if (assembly == null)
    {
    throw InterpreterError.NewInterpreterException(
    assemblyName,
    typeof(RuntimeException),
    null,
    "ErrorLoadingAssembly",
    ParserStrings.ErrorLoadingAssembly,
    assemblyName);
    }
    return assembly;
    }

  13. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  14. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  15. added
    Resolution-No Activity
    on Nov 16, 2023
  16. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  17. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

  18. added
    KeepOpen
    and removed
    Resolution-No Activity
    on Nov 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-EnhancementKeepOpenWG-Language

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions