从另一个文件继承一个类

时间:2019-09-17 10:08:37

标签: powershell

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

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)

1 个答案:

答案 0 :(得分:1)

对于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