首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未识别Powershell导入的模块命令

未识别Powershell导入的模块命令
EN

Stack Overflow用户
提问于 2017-05-02 13:15:47
回答 1查看 5.2K关注 0票数 3

我一直在写一个powershell脚本。因为我想稍微模块化/组织我的代码,所以我决定尝试制作一些模块来分割我的脚本。

我正在运行powershell 5.1.14393.1066

现在我有了一个主脚本,它导入了它需要的模块,但是它似乎找不到我的函数Is-Template-Name-Set

代码语言:javascript
复制
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

Is-Template-Name-Set是用UtilityTemplateModule定义的。模块文件(UtilityTemplateModule.psm1):

代码语言:javascript
复制
Function Get-ScriptDirectory() {
    return Split-Path $script:MyInvocation.MyCommand.Path
}

Function Is-Template-Name-Set($templateName) {
    $templateName -And $templateName -is [String] -And -Not $templateName -eq ""
}

Export-ModuleMember -Function 'Get-*'
Export-ModuleMember -Function 'Is-*'

UtilityTemplateModule描述(UtilityTemplateModule.psd1文件):

代码语言:javascript
复制
@{
ModuleVersion = '1.0'
GUID = '082fc8f7-4c7f-4d9f-84a8-b36c3610cdfe'
Author = 'Niels Bokmans'
CompanyName = 'Bluenotion'
Copyright = 'Copyright (c) 2017'
Description = 'General utilities used by the other template modules.'
# Functions to export from this module
FunctionsToExport = '*'

# Cmdlets to export from this module
CmdletsToExport = '*'

# Variables to export from this module
VariablesToExport = '*'

# Aliases to export from this module
AliasesToExport = '*'
}

我的主要剧本:

代码语言:javascript
复制
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文件)中的所有内容。

如有任何建议,将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2017-05-02 13:26:26

基于您对system32 powershell模块目录的评论,我认为您可能把模块放到了错误的目录中。

Per:https://msdn.microsoft.com/en-us/library/dd878350(v=vs.85).aspx

$PSHome\Modules (%Windir%\System32\WindowsPowerShell\v1.0\Modules)

此位置为Windows附带的模块保留。不要将模块安装到此位置。

我建议将模块移动到:

代码语言:javascript
复制
$Home\Documents\WindowsPowerShell\Modules (%UserProfile%\Documents\WindowsPowerShell\Modules)

代码语言:javascript
复制
$Env:ProgramFiles\WindowsPowerShell\Modules (%ProgramFiles%\WindowsPowerShell\Modules)

在这些路径中创建缺少的目录(例如,在Documents下,默认情况下\WindowsPowerShell\Modules文件夹不存在)。

或者,您可以将模块放入自定义路径,并将该路径添加到PSModulePath环境变量中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43738896

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档