Powershell 7进口模块不会在会话中持续
如果我打开新的 PowerShell 7 会话并运行获取命令get-website
我得到的响应是,
Get-Command: The term 'Get-Website' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
我将获得ExpecteFD输出
CommandType Name Version Source
----------- ---- ------- ------
Function Get-Website 1.0 WebAdministration
如果我然后运行 import-module webAdministration
并再次运行 get-command get-website
,这次,如果我关闭了PowerShell, 7会话,打开一个新的会议并运行 get-command get-website
,新会话不识别命令。
如果我运行 Windows PowerShell 的会话,我不必导入模块,命令已经存在。
有人能够解释发生了什么事吗?
谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不是唯一遇到此问题的人。不幸的是,我无法谈论那里的任何解决方案是否有效,因为询问者或其他任何人都没有确认它们。
无论出于何种原因?view = powershell-7.2#module-auto-loading“ rel =“ nofollow noreferrer”>在 powershell core 中自动导入。这样做的原因有几个,但是它们主要在模块侧,而您不能在不修改模块的情况下更改行为。
您可以尝试设置
$ psmoduleautoloadingpreference ='all''
在您当前的PowerShell会话中,但通常不需要从modulequalified
的默认值中更改。有关$ psmoduleautoloadingpreference
的更多信息,可以是。如果那不起作用,则必须在每个会话中手动导入模块。
幸运的是,其中绝大多数人不需要手动导入模块。对于无法自动导入的那些,您必须在每个新会话中使用
Import-Module ModuleName
。您可以通过将import-module
cmdlet添加到以下配置文件之一的位置来简化这一点:对于脚本,
import module
通常应在脚本中进行,以防止需要配置文件在希望被禁用的配置文件加载的情况下,例如以下列方式调用 powershell :powershell.exe -noprofile ....
在您的情况下,它将像这样:
这是有关
$ profile
variable 。You are not the only person having this problem. Unfortunately, I can't speak to whether any of the solutions there work, as neither the asker or anyone else has confirmed them.
For whatever reason,
WebAdministration
may not be able to be automatically imported in PowerShell Core. There are a few reasons for this but they are mostly on the module side, and you can't change the behavior without modifying the module.You can try setting
$PSModuleAutoLoadingPreference = 'All'
in your current PowerShell session, but generally that doesn't need to be changed from the default value ofModuleQualified
. More information on$PSModuleAutoLoadingPreference
can be found here.If that doesn't work, you'll have to manually import the module in every session.
Fortunately, manually importing modules isn't required for the vast majority of them. For the ones which can't be automatically imported, you must use
Import-Module MODULENAME
in each new session. You can simplify this by adding theImport-Module
cmdlet to one of the following profile locations:For scripts,
Import-Module
should generally be done inside the script to prevent needing profiles in scenarios where profile loading is desired to be disabled such as when PowerShell is invoked in the following way:powershell.exe -NoProfile ....
In your case, it would like like so:
Here is some additional information about the
$profile
variable.