Powershell 模块未加载
Powershell module not loading
我正在尝试加载一个执行自定义 cmdlet 函数的 PowerShell 模块,但我无法加载它...我已经应用了 several previous questions 的解决方案,但现在我只是兜圈子。以下是我到目前为止所做的规范以及 returns 的具体错误。请注意,由于我是 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</pre>
为了调用该函数,根据其语法,我在 PS 控制台 window 中输入了 pp $projectName
,但是 returns 的错误是无法识别的标准:
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</p>
<ul>
<li>pp MyFirstApp</li>
<li>~~</li>
<li>CategoryInfo : ObjectNotFound: (pp:String) [], CommandNotFoundException</li>
<li>FullyQualifiedErrorId : CommandNotFoundException
我将您的代码复制并粘贴到我这里的 Windows 8.1 机器中。它工作得很好(除了不存在的文件夹名称,因为我没有 Visual Studio)。
我脑海中浮现的可能会阻止您的模块工作的事情:
该文件实际名为 ProfileFunctions.psm1.ps1
或 ProfileFunctions.psm1.txt
或其他名称。
Modules 文件夹保存在其他人的文档文件夹中,或者 Public 文档文件夹中。
您不小心在文件夹名称中输入了 space(必须是 WindowsPowerShell
,而不是 Windows PowerShell
)。
我认为你的问题是 Alias pp 没有从模块中导出。
您可以按预期在模块外部定义别名,也可以使用它从模块中显式导出别名。
Export-ModuleMember -Function pushproject -Alias pp
在本文中查找更多详细信息Unable to Create a Powershell Alias in a script Module
我正在尝试加载一个执行自定义 cmdlet 函数的 PowerShell 模块,但我无法加载它...我已经应用了 several previous questions 的解决方案,但现在我只是兜圈子。以下是我到目前为止所做的规范以及 returns 的具体错误。请注意,由于我是 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</pre>
为了调用该函数,根据其语法,我在 PS 控制台 window 中输入了
pp $projectName
,但是 returns 的错误是无法识别的标准: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</p> <ul> <li>pp MyFirstApp</li> <li>~~</li> <li>CategoryInfo : ObjectNotFound: (pp:String) [], CommandNotFoundException</li> <li>FullyQualifiedErrorId : CommandNotFoundException
我将您的代码复制并粘贴到我这里的 Windows 8.1 机器中。它工作得很好(除了不存在的文件夹名称,因为我没有 Visual Studio)。
我脑海中浮现的可能会阻止您的模块工作的事情:
该文件实际名为 ProfileFunctions.psm1.ps1
或 ProfileFunctions.psm1.txt
或其他名称。
Modules 文件夹保存在其他人的文档文件夹中,或者 Public 文档文件夹中。
您不小心在文件夹名称中输入了 space(必须是 WindowsPowerShell
,而不是 Windows PowerShell
)。
我认为你的问题是 Alias pp 没有从模块中导出。
您可以按预期在模块外部定义别名,也可以使用它从模块中显式导出别名。
Export-ModuleMember -Function pushproject -Alias pp
在本文中查找更多详细信息Unable to Create a Powershell Alias in a script Module