PowerShell 5 and classes - Unable to convert value "X" of type "X" to type "X"

I am trying to use PowerShell classes, which is very convenient to group related data together and encounter quite tedious behavior work. Simplified scenario: one PS script that defines the class and another script that uses this class.

Common.ps1

class X
{
    [string] $A
} 

Script1.ps1

. $PSScriptRoot\Common.ps1

[X] $v = New-Object X

Everything is fine - you can run Script1.ps1any number of times without problems - until you change any to Common.ps1. You will encounter the following error.

Cannot convert the "X" value of type "X" to type "X".
At D: \ temp \ PSIssue \ Script1.ps1: 3 char: 1
+ [X] $ v = New-Object X
+ ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : MetadataError: (:) [], ArgumentTransformationMetadataException
    + FullyQualifiedErrorId : RuntimeException

-, ( ) PS , X , X, : - ( .NET - , " " ). Script1.ps1 .

?

+3
1

. , , . PowerShell. Script1.ps1 , . Strong Typing PowerShell :

. $PSScriptRoot\Common.ps1

$v = New-Object X

X Common.ps1 , , .

" ". :

+2

Source: https://habr.com/ru/post/1672478/


All Articles