首页 > 解决方案 > 为什么 import-module 或点源文件在 powershell 脚本中不起作用?

问题描述

我正在使用 PowerShell 扩展在 VS Code 中开发和运行 PS 脚本。我在一个单独的 PS 模块文件中定义了许多函数,我将它们保存在这里:

C:\Users\MyName\Documents\PowerShell\Modules\PowerBiFunctions\PowerBiFunctions.psm1

但是当尝试导入模块时

Import-Module PowerBiFunctions

或与脚本放在同一目录中的副本

Import-Module $PSScriptRoot\PowerBiFunctions.psm1

我收到以下错误:

Import-Module : The specified module 'PowerBiFunctions' was not loaded because no valid module file was found in any module directory.

另外,我制作了一个扩展名为 .ps1 的文件的副本,并放在同一目录中。但是,我似乎无法将它包含在简单的点源中:

.\PowerBiFunctions.ps1

或者

. .\PowerBiFunctions.ps1

似乎是什么问题?

标签: powershellvisual-studio-code

解决方案


创建一个模块清单

# cd to module folder
Set-Location C:\Users\MyName\Documents\PowerShell\Modules\PowerBiFunctions\

# create new manifest file
New-ModuleManifest .\PowerBiFunctions.psd1 -RootModule .\PowerBiFunctions.psm1 -FunctionsToExport list,of,exported,function,names

现在您可以按名称导入它:

Import-Module PowerBiFunctions

推荐阅读