Powershell 7进口模块不会在会话中持续

发布于 2025-01-22 18:24:50 字数 946 浏览 0 评论 0 原文

如果我打开新的 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 的会话,我不必导入模块,命令已经存在。

有人能够解释发生了什么事吗?

谢谢

If I open a new Powershell 7 session and run get-command Get-Website
the response I get is

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.

If I then run import-Module WebAdministration and again run get-command Get-Website, this time I get the expectefd output of

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Function        Get-Website                                        1.0        WebAdministration

If I close the Powershell 7 session, open a new one and run get-command Get-Website, the new session does not recognise the command.

If I run a session of Windows Powershell, I do not have to import the module, the command is already there.

Anyone able to explain what is going on?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

書生途 2025-01-29 18:24:50

您不是唯一遇到此问题的人。不幸的是,我无法谈论那里的任何解决方案是否有效,因为询问者或其他任何人都没有确认它们。

注意:我最初怀疑这可能是通过 WindowsCompatibility 模块自动导入的问题,但是看来。此外,从PowerShell 7开始, WindowsCompatibility 模块功能是


无论出于何种原因?view = powershell-7.2#module-auto-loading“ rel =“ nofollow noreferrer”>在 powershell core 中自动导入。这样做的原因有几个,但是它们主要在模块侧,而您不能在不修改模块的情况下更改行为。

您可以尝试设置 $ psmoduleautoloadingpreference ='all''在您当前的PowerShell会话中,但通常不需要从 modulequalified 的默认值中更改。有关 $ psmoduleautoloadingpreference 的更多信息,可以是

如果那不起作用,则必须在每个会话中手动导入模块。


幸运的是,其中绝大多数人不需要手动导入模块。对于无法自动导入的那些,您必须在每个新会话中使用 Import-Module ModuleName 。您可以通过将 import-module cmdlet添加到以下配置文件之一的位置来简化这一点:

$profile.CurrentUserCurrentHost
$profile.CurrentUserAllHosts
$profile.AllUsersCurrentHost
$profile.AllUsersAllHosts

对于脚本, import module 通常应在脚本中进行,以防止需要配置文件在希望被禁用的配置文件加载的情况下,例如以下列方式调用 powershell powershell.exe -noprofile ....

在您的情况下,它将像这样:

Import-Module WebAdministration

这是有关 $ 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.

Note: I had originally suspected this might be a problem with automatic importing through the WindowsCompatibility module, but it appears that it does not interfere with auto-importing of modules. In addition, as of PowerShell 7 the WindowsCompatibility module features are baked into PowerShell itself.

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 of ModuleQualified. 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 the Import-Module cmdlet to one of the following profile locations:

$profile.CurrentUserCurrentHost
$profile.CurrentUserAllHosts
$profile.AllUsersCurrentHost
$profile.AllUsersAllHosts

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:

Import-Module WebAdministration

Here is some additional information about the $profile variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文