无法识别 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, 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):

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 文件):

@{
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 = '*'
}

我的主要脚本:

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 模块目录的评论,我认为您可能将模块放在了错误的目录中。

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

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

This location is reserved for modules that ship with Windows. Do not install modules to this location.

我建议将您的模块移动到:

$Home\Documents\WindowsPowerShell\Modules (%UserProfile%\Documents\WindowsPowerShell\Modules)

或者

$Env:ProgramFiles\WindowsPowerShell\Modules (%ProgramFiles%\WindowsPowerShell\Modules)

在任何一个丢失的路径中创建任何目录(例如,在 Documents 下,默认情况下通常不存在 \WindowsPowerShell\Modules 文件夹)。

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

关于无法识别 Powershell 导入的模块命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43738896/

相关文章:

bash - powershell bash 循环随机卡住等待键盘输入

Powershell:仅返回搜索的第二行

powershell - 在 Azure 存储资源管理器的文件共享中移动/删除文件的功能?

Powershell 克隆有序哈希表

powershell - 32 位 Powershell : Use the ServerManager module?

windows - 从 Get-ChildItem -Path 返回对象数组

forms - Windows 窗体在 Powershell 和 Powershell ISE 中看起来不同。为什么?

powershell - 使用Powershell为AD中的每个用户检索管理器名称

sharepoint - 功能激活

powershell - 如何从 Azure 资源管理器 Runbook 登录?