{"id":17867084,"url":"https://github.com/jaykul/classmoduletest","last_synced_at":"2025-04-02T22:14:30.642Z","repository":{"id":146118522,"uuid":"66790409","full_name":"Jaykul/ClassModuleTest","owner":"Jaykul","description":null,"archived":false,"fork":false,"pushed_at":"2016-08-31T04:35:04.000Z","size":5,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-08T12:31:18.811Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PowerShell","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Jaykul.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-08-28T20:56:22.000Z","updated_at":"2017-03-18T01:36:30.000Z","dependencies_parsed_at":"2023-05-16T17:45:19.118Z","dependency_job_id":null,"html_url":"https://github.com/Jaykul/ClassModuleTest","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jaykul%2FClassModuleTest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jaykul%2FClassModuleTest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jaykul%2FClassModuleTest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jaykul%2FClassModuleTest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Jaykul","download_url":"https://codeload.github.com/Jaykul/ClassModuleTest/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246899668,"owners_count":20851898,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-28T09:43:37.139Z","updated_at":"2025-04-02T22:14:30.588Z","avatar_url":"https://github.com/Jaykul.png","language":"PowerShell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Using PowerShell v5.0 Classes in a Script Module\nI've been working at building a PowerShell script module that leverages classes in an effort to learn the patterns of object-oriented development in PowerShell v5.0. I've run into several problems, however, that have left me doubting the practicality of using classes in their current state. This project will demonstrate several approaches I've taken and the problems I've encountered with each. This is not an attempt to provide a solution to those problems, but hopefully will open the discussion about improving the implementation of classes in the next version of PowerShell. Otherwise, consider this a vehicle to discuss the correct way to use classes in a module and to point out where I've gone wrong and what to expect with each approach. What is the **endorsed** way to do this?\n\nFor reference, the original project is [here](https://github.com/mattmcnabb/OneLogin).\n\n## Project Details\nThis project contains 5 PowerShell modules, each demonstrating a different approach. A description of each approach and roadblocks with each is included below.\n\n## Scenarios\n\n### Scenario1\nDefine classes in separate script files and dot-source them in the psm1 file.\n\nWhen the module is imported this error is returned:\n\n```console\nUnable to find type [TestObject1].\nAt C:\\ClassModuleTest\\Scenario1\\Scenario1.psm1:6 char:17\n+     [OutputType([TestObject1])]\n+                 ~~~~~~~~~~~~~\n    + CategoryInfo          : InvalidOperation: (TestObject1:TypeName) [], RuntimeException\n    + FullyQualifiedErrorId : TypeNotFound\n\nUnable to find type [TestObject1].\nAt C:\\ClassModuleTest\\Scenario1\\Scenario1.psm1:30 char:9\n+         [TestObject1]\n+         ~~~~~~~~~~~~~\n    + CategoryInfo          : InvalidOperation: (TestObject1:TypeName) [], RuntimeException\n    + FullyQualifiedErrorId : TypeNotFound\n```\n\n### Scenario2\nDefine classes directly in the psm1 file\n\nNeed further testing as this seems to be working as expected. I'll test in the production module again and see what happens.\n\n### Scenario3\nDefine classes in separate script files and add them to ScriptsToProcess\n\nSeems to be working at a basic level in this project, but I had problems when I tried this in a production module. I test by using a simple debug script that imports the module and runs a few of the functions. I have a helper function that casts the custom object output from Invoke-RestMethod to a custom class type. It fails with this error:\n\n```console\nCannot convert the \"TestObject\" value of type \"System.String\" to type \"System.Type\".\nAt .\\ConvertTo-TestObject.ps1:19 char:13\n+             $InputObject -as $TypeName\n+             ~~~~~~~~~~~~~~~~~~~~~~~~~~\n    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException\n    + FullyQualifiedErrorId : InvalidCastFromStringToType\n```\n\nThis problem does not occur if I dot-source the debug script. This indicates that this may be caused by a class scoping issue.\n\n### Scenario4\nDefine classes in a nested Module\n\nImports fine but when I run this command\n\n```PowerShell\nPS\u003e New-TestObject4\n```\n\nI get this error\n\n```console\nUnable to find type [TestObject4].\nAt C:\\ClassModuleTest\\Scenario4\\Scenario4.psm1:4 char:17\n+     [OutputType([TestObject4])]\n+                 ~~~~~~~~~~~~~\n    + CategoryInfo          : InvalidOperation: (TestObject4:TypeName) [], RuntimeException\n    + FullyQualifiedErrorId : TypeNotFound\n```\n\n### Scenario5\nCreate a master module with both the working module and the classes module as nested modules\n\nWhen I attempt to import the module\n\n```console\nAt C:\\ClassModuleTest\\Scenario5\\Scenario5Module\\Scenario5Module.psm1:1 char:1\n+ using module TestObject5\n+ ~~~~~~~~~~~~~~~~~~~~~~~~\nCould not find the module 'TestObject5'.\n    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException\n    + FullyQualifiedErrorId : ModuleNotFoundDuringParse\n\nipmo : The module to process 'Scenario5Module.psm1', listed in field 'ModuleToProcess/RootModule' of module manifest\n'C:\\ClassModuleTest\\Scenario5\\Scenario5Module\\Scenario5Module.psd1' was not processed because no valid module was found in any module\ndirectory.\nAt line:1 char:1\n+ ipmo C:\\ClassModuleTest\\Scenario5 -Force\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n    + CategoryInfo          : ResourceUnavailable: (Scenario5Module:String) [Import-Module], PSInvalidOperationException\n    + FullyQualifiedErrorId : Modules_ModuleFileNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjaykul%2Fclassmoduletest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjaykul%2Fclassmoduletest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjaykul%2Fclassmoduletest/lists"}