无法识别Powershell导入的模块命令
我一直在编写powershell脚本。因为我想稍微模块化/组织我的代码,所以我决定尝试制作一些模块来拆分我的脚本 我正在运行powershell 5.1.14393.1066 现在我有一个主脚本,可以导入它需要的模块,但它似乎找不到我的函数无法识别Powershell导入的模块命令,powershell,Powershell,我一直在编写powershell脚本。因为我想稍微模块化/组织我的代码,所以我决定尝试制作一些模块来拆分我的脚本 我正在运行powershell 5.1.14393.1066 现在我有一个主脚本,可以导入它需要的模块,但它似乎找不到我的函数Is Template Name Set: Is-Template-Name-Set : The term 'Is-Template-Name-Set' is not recognized as the name of a cmdlet, function,
Is Template Name Set
:
Is-Template-Name-Set : The term 'Is-Template-Name-Set' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Users\niels\test1\TemplateScript.ps1:6 char:5
+ If (Is-Template-Name-Set) {
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Is-Template-Name-Set:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
是模板名称集
是在UtilityTemplateModule中定义的。模块文件(UtilityTemplateModule.psm1
):
UtilityTemplateModule说明(UtilityTemplateModule.psd1
文件):
我的主要剧本:
param([String]$templateName="")
Import-Module BuildTemplateModule
Import-Module CdnTemplateModule
Import-Module UtilityTemplateModule
If (Is-TemplateNameSet -templateName $templateName) {
echo "Template name is set!"
$templateId = GetTemplateId -templateName $templateName
if (-Not $templateId -eq -1) {
Restore-Packages
Run-Build-Tasks
Minify-Require-Js
Clean-Downloaded-Dependencies
Copy-Offline-Page
#Deploy $response.templateId
}
} else {
echo "Template name not set!"
exit
}
我对任何powershell工作都是新手,我不知道为什么该函数无法识别。我认为我做得对,导出模块本身中的函数,还导出了描述文件(?.psd1文件)中的所有内容
如果您有任何建议,我们将不胜感激。根据您对system32 powershell模块目录的评论,我认为您可能将模块放在了错误的目录中 Per:
$PSHome\Modules(%Windir%\System32\WindowsPowerShell\v1.0\Modules)
此位置为Windows附带的模块保留。请勿将模块安装到此位置
我建议将您的模块移动到:
$Home\Documents\WindowsPowerShell\Modules (%UserProfile%\Documents\WindowsPowerShell\Modules)
或
在缺少的路径中创建任何目录(例如在文档
下,\WindowsPowerShell\Modules
文件夹默认情况下通常不存在)
或者,您可以将模块放在自定义路径中,并将该路径添加到
PSModulePath
环境变量。模块名为UtilityTemplateModule
还是UtilityModule
?你似乎两个都提到了,很抱歉不一致,该模块位于system32 powershell模块目录中名为UtilityTemplateModule.psm1
的文件夹中,该文件夹名为UtilityTemplateModule
。我认为这可能是错误的Export ModuleMember-Function'Get-*'
,因为我认为您不必对其使用通配符字符串。但是可能是错误的。为了测试将Export-ModuleMember-Function“Is-*”
更改为Export-ModuleMember-Function“Is-Template Name Set”
通常更好的做法是在PSD1中的CmdletsToExport='*'下列出每个函数(而不是*)因为这样可以自动发现它们,并且不必显式加载模块。我将模块移动到%ProgramFiles%\WindowsPowerShell\modules
,但它仍然找不到它们。当我运行GetModule-listavable
时,它们会出现。您是否在同一控制台/ISE窗口中工作了很长时间?如果是这样,导入模块
命令可能没有实际加载模块的最新版本。您可以通过添加-force
或先对其执行删除模块
来解决此问题。只是一个想法。刚刚重新启动了ISE窗口,但也不是这样。
param([String]$templateName="")
Import-Module BuildTemplateModule
Import-Module CdnTemplateModule
Import-Module UtilityTemplateModule
If (Is-TemplateNameSet -templateName $templateName) {
echo "Template name is set!"
$templateId = GetTemplateId -templateName $templateName
if (-Not $templateId -eq -1) {
Restore-Packages
Run-Build-Tasks
Minify-Require-Js
Clean-Downloaded-Dependencies
Copy-Offline-Page
#Deploy $response.templateId
}
} else {
echo "Template name not set!"
exit
}
$Home\Documents\WindowsPowerShell\Modules (%UserProfile%\Documents\WindowsPowerShell\Modules)
$Env:ProgramFiles\WindowsPowerShell\Modules (%ProgramFiles%\WindowsPowerShell\Modules)