PS 5.1 (RTM) - Cannot import classes from dot sourcing

Welcome Forums General PowerShell Q&A PS 5.1 (RTM) - Cannot import classes from dot sourcing

This topic contains 10 replies, has 4 voices, and was last updated by

 
Participant
1 year, 10 months ago.

  • Author
    Posts
  • #62373

    Participant
    Points: 0
    Rank: Member

    Is this a bug? The code works perfectly fine in PS 5.0 (RTM). If someone else can confirm then I'll post to UserVoice.

    TestClass.ps1

    class MyClass
    {
        $prop1
        $prop2
    }

    Test-TestClass.ps1

    . C:\Temp\TestClass.ps1
    
    function Test-MyClass
    {
        [CmdletBinding()]
        [OutputType([MyClass],[System.Object[]])]
        Param
        ()
    
        Write-Host -Object 'Test 123'
    }
    
    Test-MyClass

    Unable to find type [MyClass].
    At C:\Temp\Test-MyClass.ps1:6 char:17
    + [OutputType([MyClass],[System.Object[]])]
    + ~~~~~~~~~
    + CategoryInfo : InvalidOperation: (MyClass:TypeName) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound

  • #62377

    Participant
    Points: 0
    Rank: Member

    A work around is to add the .ps1 files (with classes defined) to ScriptsToProcess in your .psd1

    The concern is that working code in 5.0 is breaking in 5.1

    Edit: Cancel that. ScriptsToProcess doesn't work either.

  • #62473

    Participant
    Points: 0
    Rank: Member
  • #62512

    Participant
    Points: 1
    Rank: Member

    This works for me:

    TestClass.ps1:

    class MyClass
    {
        $prop1
        $prop2
    }

    Test-TestClass.ps1

    Import-Module .\TestClass.ps1
    
    function Test-MyClass
    {
        [CmdletBinding()]
        [OutputType([MyClass],[System.Object[]])]
        Param
        ()
    
        Write-Host -Object 'Test 123'
    }
    
    Test-MyClass

    Output:

    PS C:\Users\Steve> .\Test-TestClass.ps1
    Test 123
    PS C:\Users\Steve>

    Version Info:

    PS C:\Users\Steve> $PSVersionTable
    
    Name                           Value
    ----                           -----
    PSVersion                      5.1.14393.693
    PSEdition                      Desktop
    PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
    BuildVersion                   10.0.14393.693
    CLRVersion                     4.0.30319.42000
    WSManStackVersion              3.0
    PSRemotingProtocolVersion      2.3
    SerializationVersion           1.1.0.1
  • #62515

    Participant
    Points: 0
    Rank: Member

    Thanks testing Steve. Looks like you have an older build than mine. I have build v5.1.14409.1005. Can you tell me what OS you ran the test on?

  • #62518

    Participant
    Points: 1
    Rank: Member

    Windows 10 Pro (Desktop) and Windows 10 Enterprise (Work PC)

    I'll try updating my version to RTM.... guess I shoulda checked there first =)

  • #62524

    Participant
    Points: 0
    Rank: Member

    Hmm I wonder if the problem only exists on Win7/8.1/2012R2. If you had either of those available it would be a good test.

  • #62526

    Participant
    Points: 1
    Rank: Member

    I can try tomorrow on Server 2012R2, but I believe it's running the same version as my PC. I'll let you know.

    My point was that I have had success using Import-Module over Invocation.

  • #62547

    Participant
    Points: 22
    Rank: Member

    I cannot reproduce this on Windows 8.1. Zuldan's code works fine.
    Here is my $PSVersionTable:

    Name                       Value
    ----                       -----
    PSVersion                  5.1.14409.1005
    PSEdition                  Desktop
    PSCompatibleVersions       {1.0, 2.0, 3.0, 4.0...}
    BuildVersion               10.0.14409.1005
    CLRVersion                 4.0.30319.42000
    WSManStackVersion          3.0
    PSRemotingProtocolVersion  2.3
    SerializationVersion       1.1.0.1
    
  • #62907

    Participant
    Points: 0
    Rank: Member

    I cannot reproduce the issue on Windows 7 SP1 using the original code by Zuldan. My $PSVersionTable is identical to George's

  • #63031

    Participant
    Points: 0
    Rank: Member

    Thanks for everyone testing and providing feedback I still cannot get classes import. I've run the test code above against 29x Windows 2008 R2 servers and every single one is failing. If no one else is seeing this issue then it has to be something in my environment.

    Could you guys tell me what version of .NET you're using? I'm using .NET 4.6.2 on all 2008 R2 servers with PS v5.1.14409.1005.

The topic ‘PS 5.1 (RTM) - Cannot import classes from dot sourcing’ is closed to new replies.