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
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.
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
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?
Windows 10 Pro (Desktop) and Windows 10 Enterprise (Work PC)
I’ll try updating my version to RTM… guess I shoulda checked there first =)
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.
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.
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
I cannot reproduce the issue on Windows 7 SP1 using the original code by Zuldan. My $PSVersionTable is identical to George’s
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.