Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot load module in PS4 due to "class" keyword #92

Closed
replicaJunction opened this issue Aug 31, 2016 · 5 comments
Closed

Cannot load module in PS4 due to "class" keyword #92

replicaJunction opened this issue Aug 31, 2016 · 5 comments
Assignees
Labels

Comments

@replicaJunction
Copy link

Hello!

After updating to version 1.7.2.3 of this module, I'm no longer able to import the module in a PowerShell 4 session. Here's the error:

Windows PowerShell
Copyright (C) 2013 Microsoft Corporation. All rights reserved.

PS C:\Users\replica> ipmo PoshRsJob
At E:\Stuff\Modules\PoshRsJob\PoshRSJob.psm1:6 char:5
+     class V2UsingVariable {
+     ~~~~~
The 'class' keyword is not supported in this version of the language.
At E:\Stuff\Modules\PoshRsJob\PoshRSJob.psm1:13 char:5
+     class RSRunspacePool{
+     ~~~~~
The 'class' keyword is not supported in this version of the language.
At E:\Stuff\Modules\PoshRsJob\PoshRSJob.psm1:23 char:5
+     class RSJob {
+     ~~~~~
The 'class' keyword is not supported in this version of the language.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ReservedKeywordNotAllowed

ipmo : The module to process 'PoshRSJob.psm1', listed in field 'ModuleToProcess/RootModule' of module manifest
'E:\Stuff\Modules\PoshRsJob\PoshRsJob.psd1' was not processed because no valid module was found in any module directory.
At line:1 char:1
+ ipmo PoshRsJob
+ ~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (PoshRsJob:String) [Import-Module], PSInvalidOperationException
    + FullyQualifiedErrorId : Modules_ModuleFileNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand

PS C:\Users\replica> $psversiontable

Name                           Value
----                           -----
PSVersion                      4.0
WSManStackVersion              3.0
SerializationVersion           1.1.0.1
CLRVersion                     4.0.30319.18444
BuildVersion                   6.3.9600.16406
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion      2.2

The test system is Server 2008 R2. The module still works as expected on a Win7 box with PS 5.0 installed.

I'm not super familiar with classes yet, but would it be possible to move the class definitions into a separate .ps1 file? Then the module file itself could check the PSVersionTable and load either the "class" .ps1 file or the "Add-Type" .ps1 file as appropriate.

Thanks again for the amazing work on this. It's been a huge help!

@proxb
Copy link
Owner

proxb commented Aug 31, 2016

What happens when you run $PSVersionTable.PSEdition? I thought that it if 'Edition' wasn't there in the property (or the property didn't exist) that it would default to using Add-Type to build the necessary classes. Worst case is that I will rely on a combination of version and PSEdition to make this work properly.

@proxb
Copy link
Owner

proxb commented Aug 31, 2016

I think I will just check for $PSVersionTable.PSVersion,Major and if -ge 5 then use classes, otherwise use Add-Type with the C# code. PSEdition is nice, but if it is failing the check on V4 (because it doesn't exist) then I will just avoid it.

@proxb proxb added the bug label Aug 31, 2016
@proxb proxb self-assigned this Aug 31, 2016
@proxb
Copy link
Owner

proxb commented Aug 31, 2016

Ugh, that is annoying. Even though I am not calling to create the class (the If statement should have taken care of that) the parser still sees the keyword and throws an error. It looks like a classes file will be the way to go...or use a here string and call Invoke-Expression.

@proxb proxb closed this as completed in d7707e6 Aug 31, 2016
@proxb proxb reopened this Aug 31, 2016
@proxb
Copy link
Owner

proxb commented Aug 31, 2016

This should be fixed in the latest update.

@replicaJunction
Copy link
Author

The PSEdition property doesn't exist at all on my system, as you suspected.

Windows PowerShell
Copyright (C) 2013 Microsoft Corporation. All rights reserved.

PS C:\Users\replica> $PSVersionTable.PSEdition
PS C:\Users\replica> 

Interesting to hear that it's a parser problem...I was actually just thinking of "borrowing" your approach on this module for custom classes / objects. Sending that to Invoke-Expression is a really cool workaround.

Anyway, the latest version works great. Thanks for the fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants