Why can't I use predefined variables in class methods?
问题 I tried to use predefined variables like $PSVersionTable or $PSScriptRoot in a class method. They failed with the error message Variable is not assigned in the method. Example: Class Foo { [String]$Version GetVersion() { If ($PSVersionTable) { $this.Version = $PSVersionTable.PSVersion } } } But why? 回答1: Class Foo { [String] $Version GetVersion() { if ($global:PSVersionTable) { $this.Version = $global:PSVersionTable.PSVersion } } } $foo = [Foo]::new() $foo.GetVersion() Write-Host $foo.Version