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

This topic contains 10 replies, has 4 voices, and was last updated by Profile photo of Zuldan Zuldan 1 month, 4 weeks ago.

  • Author
    Posts
  • #62373
    Profile photo of Zuldan
    Zuldan
    Participant

    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
    Profile photo of Zuldan
    Zuldan
    Participant

    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
    Profile photo of Zuldan
    Zuldan
    Participant
  • #62512
    Profile photo of Steve
    Steve
    Participant

    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
    Profile photo of Zuldan
    Zuldan
    Participant

    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
    Profile photo of Steve
    Steve
    Participant

    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
    Profile photo of Zuldan
    Zuldan
    Participant

    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
    Profile photo of Steve
    Steve
    Participant

    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
    Profile photo of George Chakhidze
    George Chakhidze
    Participant

    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
    Profile photo of Sergei Ryabkov
    Sergei Ryabkov
    Participant

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

  • #63031
    Profile photo of Zuldan
    Zuldan
    Participant

    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.

You must be logged in to reply to this topic.