powershell - 为什么 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
似乎是什么问题?
解决方案
创建一个模块清单:
# 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
推荐阅读
- python - 如何获取前 6 个文本
- html - 如何防止第一个弹性项目在最后一个项目占用剩余空间时折叠
- selenium - 无法为 Google 搜索输入框找到正确的 ID 选择或 Xpath
- opencart-3 - Opencart 事件系统和重定向外部链接
- flutter - Flutter中如何观察Button的onPressed函数?
- php - 连接失败:SQLSTATE[HY000] [2054] 服务器请求客户端未知的身份验证方法。端口繁忙还是源代码问题?
- node.js - 机器人在放手 5 秒后不会删除消息
- mysql - Mysql Client 切换到 (poll & recvfrom) 进行高负载下的 I/O 数据读取
- javascript - 设置 iframe 内容的方法
- c++ - 在 ubuntu 20.04 上安装 tflite c++