首页 > 解决方案 > 从另一个文件继承一个类

问题描述

你如何从另一个文件继承和使用一个类?

PowerShell version:
 $PSVersionTable.PSVersion   

Major  Minor  Patch  PreReleaseLabel BuildLabel
-----  -----  -----  --------------- ----------
6      2      1  

我尝试了以下方法:

# $PWD and $local:PSScriptRoot

# using module ../base/base.psm1
# using module $PWD/../base/base.psm1

# using module ..\base\base.psm1
# using module $PWD\..\base\base.psm1

using module ..\base\base.psm1

Class MyClass : base {
...
}

错误:

Unable to find type [base].PowerShell
Ignoring 'TypeNotFound' parse error on type 'base'. Check if the specified type is correct. This can also be due the type not being known at parse time due to types imported by 'using' statements.PSScriptAnalyzer(TypeNotFound)

标签: powershell

解决方案


对于 PS1 文件,只需在行首添加一个点 ( . ):

#LOAD DEV RECIPIES (FUNCTIONS) - not in use (yet)
. "$DeploymentScriptsGitPath\recipies\Recipies-PROD.ps1"

对于 PSM 模块,您需要导入它们:

Import-Module -name c:\work\tools-kit\powershel\modules\sqlserver\ -DisableNameChecking

参考:

get-help import-module -full

推荐阅读