问题描述
我希望能够对PowerShell脚本进行模块化,以便将专用模块放在本地“ lib”文件夹中,然后从脚本中加载它们。示例:
Import-Module $base\lib\constants.psm1 -Force
Import-Module $base\lib\getUserEntries.psm1 -Force
Import-Module $base\lib\utilitiesNetwork.psm1 -Force
$base
的定义是什么?
解决方法
以下内容对我有用,但似乎是人为的-考虑到本用例的普遍性,似乎应该有一种更好的方法。因此,我的脚本之一的顶部将如下所示:
$base=$(if ($psISE) {Split-Path -Path $psISE.CurrentFile.FullPath} else {$(if ($global:PSScriptRoot.Length -gt 0) {$global:PSScriptRoot} else {$global:pwd.Path})})
Import-Module $base\lib\constants.psm1 -Force
Import-Module $base\lib\getUserEntries.psm1 -Force
Import-Module $base\lib\utilitiesNetwork.psm1 -Force
有更好的方法吗?