【发布时间】:2015-08-08 20:16:45
【问题描述】:
我正在尝试加载执行自定义 cmdlet 函数的 PowerShell 模块,但我无法加载它...我已经应用了severalpreviousquestions 的解决方案,但现在我'我只是在转圈。以下是我到目前为止所做的规范以及返回的特定错误。请注意,由于我是 PowerShell 和一般编程的新手,所以我的问题不是文件路径问题,而是我的实际函数中的逻辑错误,这不会让我感到惊讶:
-
我为自定义 cmdlet 创建了一个配置文件函数,它允许我 从两个不同的路径打开项目文件:
function push-project( $project ) { pushd ~/Documents/Visual Studio 2015/Projects/$project; pushd ~/Documents/GitHub/$project; } New-Alias -Name pp -Value push-project; -
我通过将函数保存为
ProfileFunctions.psm1创建了一个模块 在以下目录中:~\Documents\WindowsPowerShell\Modules\ProfileFunctions\ProfileFunctions.psm1 -
要调用该函数,根据其语法,我在 PS 控制台窗口中输入
pp $projectName,但返回的错误是标准无法识别的:pp : The term 'pp' 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 line:1 char:1- pp MyFirstApp
- ~~
- CategoryInfo : ObjectNotFound: (pp:String) [], CommandNotFoundException
- FullyQualifiedErrorId : CommandNotFoundException
【问题讨论】:
-
如果你
Import-Module ProfileFunctions会发生什么? -
如果你使用常规的函数名是否有效?
-
@Eris,我在尝试导入时收到“不存在有效的模块文件”错误。所以我猜 PS 甚至一开始都没有创建一个。
-
如果你的模块存在,当你运行
Get-Module -ListAvailable时它会被列出来。也就是说,如果您的目标只是将该功能加载到您的 powershell 环境中,我宁愿只使用 $profile (您知道我的意思吗)?还是你有其他理由使用模块? -
@ElliotEwert 我在我的个人资料中使用自定义函数。所以在这里讨论一些话题,但如果它有帮助 - 创建一个配置文件(如果你没有)你可以运行
New-Item -Type file $profile,然后例如ise $profile编辑该文件。如果您在其中添加函数和别名,它应该可以工作。每次打开 Powershell 时,Powershell 都会自动加载 $profile。它可能会阻止您使用您可以使用Set-ExecutionPolicy RemoteSigned设置的执行策略。抱歉,如果您已经知道这一切。
标签: powershell module