Repository navigation
Description
Prerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues.
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
Steps to reproduce
I don't know if this is a bug or simply the expected behavior, but it seems strange to me.
In a Powershell class, I want to use a class defined in a DLL imported with Add-Type.
For my first attempt, I simply tried the ClassS1.ps1 script.
ClassS1.ps1
Add-Type -path '.\obs-websocket-dotnet.dll'
[OBSWebsocketDotNet.OBSWebsocket] $obsWS = $null
class ClassS1 {
[OBSWebsocketDotNet.OBSWebsocket] $obsWS = $null
}
Running the ClassS1.ps1 script generates the following error
ParserError: …..\ClassS1.ps1:7
Line |
7 | [OBSWebsocketDotNet.OBSWebsocket] $obsWS = $null
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Unable to find type [OBSWebsocketDotNet.OBSWebsocket].
Using the [OBSWebsocketDotNet.OBSWebsocket] class defined in obs-websocket-dotnet.dll generates an error only when used inside the powershell class definition.
For my second attempt, I tried using a powershell module hosting the powershell class definition.
ClassS2.ps1
using module '.\ClassM2.psm1'
Add-Type -path '.\obs-websocket-dotnet.dll'
[OBSWebsocketDotNet.OBSWebsocket] $obsWS = $null
ClassM2.psm1
class ClassM2 {
[OBSWebsocketDotNet.OBSWebsocket] $obsWS = $null
}
Running the ClassS2.ps1 script generates the following error
.\ClassS2.ps1: At D:\Données\Alain\CloudStation\Eglise\Touch Portal\Tests\Classes\ClassM2.psm1:3 char:3
-
[OBSWebsocketDotNet.OBSWebsocket] $obsWS = $null -
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [OBSWebsocketDotNet.OBSWebsocket].
This error is not surprising because the DLL is only imported after the using module statement, module that needs the [OBSWebsocketDotNet.OBSWebsocket] class.
In order to bypass the requirement to use the using statement before any other statement, I made a third attempt by placing the Add-Type statement in another AddType.psm1 module.
AddType.psm1
Add-Type -path '.\obs-websocket-dotnet.dll'
ClassS3.ps1
using module '.\AddType.psm1'
using module '.\ClassM3.psm1'
[OBSWebsocketDotNet.OBSWebsocket] $obsWS = $null
ClassM3.psm1
class ClassM3 {
[OBSWebsocketDotNet.OBSWebsocket] $obsWS = $null
}
Running the ClassS3.ps1 script does not generate any error
The fourth attempt uses only the script similar to that of the first attempt but using the AddType.psm1 module from the third attempt.
ClassS4.ps1
using module '.\AddType.psm1'
[OBSWebsocketDotNet.OBSWebsocket] $obsWS = $null
class ClassS4 {
[OBSWebsocketDotNet.OBSWebsocket] $obsWS = $null
}
Running the ClassS4.ps1 script does not generate any error
Are these behaviors expected ?
Expected behavior
no error when running ClassS1.ps1Actual behavior
error when running ClassS1.ps1
ParserError: ......\ClassS1.ps1:7
Line |
7 | [OBSWebsocketDotNet.OBSWebsocket] $obsWS = $null
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Unable to find type [OBSWebsocketDotNet.OBSWebsocket].Error details
Exception :
Type : System.Management.Automation.ParseException
Errors :
Extent : OBSWebsocketDotNet.OBSWebsocket
ErrorId : TypeNotFound
Message : Unable to find type [OBSWebsocketDotNet.OBSWebsocket].
Message : At ......\ClassS1.ps1:7 char:3
+ [OBSWebsocketDotNet.OBSWebsocket] $obsWS = $null
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [OBSWebsocketDotNet.OBSWebsocket].
ErrorRecord :
Exception :
Type : System.Management.Automation.ParentContainsErrorRecordException
Message : At ......\ClassS1.ps1:7 char:3
+ [OBSWebsocketDotNet.OBSWebsocket] $obsWS = $null
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [OBSWebsocketDotNet.OBSWebsocket].
HResult : -2146233087
CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
FullyQualifiedErrorId : TypeNotFound
InvocationInfo :
ScriptLineNumber : 7
OffsetInLine : 3
HistoryId : 2
ScriptName : ......\ClassS1.ps1
Line : [OBSWebsocketDotNet.OBSWebsocket] $obsWS = $null
Statement : OBSWebsocketDotNet.OBSWebsocket
PositionMessage : At ......\ClassS1.ps1:7 char:3
+ [OBSWebsocketDotNet.OBSWebsocket] $obsWS = $null
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PSScriptRoot : ......\Classes
PSCommandPath : ......\ClassS1.ps1
CommandOrigin : Internal
ScriptStackTrace : at <ScriptBlock>, <No file>: line 1
TargetSite :
Name : Create
DeclaringType : [System.Management.Automation.ScriptBlock]
MemberType : Method
Module : System.Management.Automation.dll
Data : System.Collections.ListDictionaryInternal
Source : System.Management.Automation
HResult : -2146233087
StackTrace :
at System.Management.Automation.ScriptBlock.Create(Parser parser, String fileName, String fileContents)
at System.Management.Automation.ExternalScriptInfo.ParseScriptContents(Parser parser, String fileName, String fileContents, Nullable`1 definingLanguageMode)
at System.Management.Automation.ExternalScriptInfo.get_ScriptBlock()
at System.Management.Automation.CommandDiscovery.CreateCommandProcessorForScript(ExternalScriptInfo scriptInfo, ExecutionContext context, Boolean useNewScope, SessionStateInternal sessionState)
at System.Management.Automation.CommandDiscovery.LookupCommandProcessor(CommandInfo commandInfo, CommandOrigin commandOrigin, Nullable`1 useLocalScope, SessionStateInternal sessionState)
at System.Management.Automation.CommandDiscovery.LookupCommandProcessor(String commandName, CommandOrigin commandOrigin, Nullable`1 useLocalScope)
at System.Management.Automation.ExecutionContext.CreateCommand(String command, Boolean dotSource)
at System.Management.Automation.PipelineOps.AddCommand(PipelineProcessor pipe, CommandParameterInternal[] commandElements, CommandBaseAst commandBaseAst, CommandRedirection[] redirections, ExecutionContext context)
at System.Management.Automation.PipelineOps.InvokePipeline(Object input, Boolean ignoreInput, CommandParameterInternal[][] pipeElements, CommandBaseAst[] pipeElementAsts, CommandRedirection[][] commandRedirections,
FunctionContext funcContext)
at System.Management.Automation.Interpreter.ActionCallInstruction`6.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
CategoryInfo : ParserError: (:) [], ParseException
FullyQualifiedErrorId : TypeNotFound
InvocationInfo :
ScriptLineNumber : 7
OffsetInLine : 3
HistoryId : 2
ScriptName : ......\ClassS1.ps1
Line : [OBSWebsocketDotNet.OBSWebsocket] $obsWS = $null
Statement : OBSWebsocketDotNet.OBSWebsocket
PositionMessage : At ......\ClassS1.ps1:7 char:3
+ [OBSWebsocketDotNet.OBSWebsocket] $obsWS = $null
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PSScriptRoot : ......\Classes
PSCommandPath : ......\ClassS1.ps1
CommandOrigin : Internal
ScriptStackTrace : at <ScriptBlock>, <No file>: line 1Environment data
Name Value
---- -----
PSVersion 7.5.4
PSEdition Core
GitCommitId 7.5.4
OS Microsoft Windows 10.0.19045
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0Visuals
No response
Activity
frogeater commentedon Nov 18, 2025
Copy-Paste error ... the script ClassS4.ps1 generates the same error than ClassS1.ps1.
Therefore to avoid any error working with 3 files (third attempt) is necessary.
mklement0 commentedon Nov 18, 2025
Without looking at all the details of your post, it sounds like the known problem of types referenced in
classdefinitions (via type literals) needing to have been loaded before script execution:frogeater commentedon Nov 20, 2025
OK, you are right. This is exactly my problem.
Many thanks.