gpt4 book ai didi

azure - 使用 PowerShell 隐式导入较旧的模块版本

转载 作者:行者123 更新时间:2023-12-03 06:45:09 26 4
gpt4 key购买 nike

我想使用Implicitly Importing使用 PowerShell 模块 Az 时。该模块有相当多的子模块,由于时间原因,我不想在脚本开始时导入特定版本。该脚本将在不受我控制的代理上运行,因此可能会在我不知情的情况下安装更新的 Az 模块。如果是这样,我的脚本仍应使用旧版本并进行隐式导入。但我没有找到以这种方式加载旧版本的方法。隐式导入始终使用最新版本。然后我可以通过导入旧版本来覆盖它(如下例所示),但这正是我不想做的。

PS > Get-Module Impl*
PS > GetImplicitValue()
Implicit Importing from V2
PS > Get-Module Impl*

ModuleType Version PreRelease Name ExportedCommands
---------- ------- ---------- ---- ----------------
Script 2.1.2 ImplicitlyImporting GetImplicitValue

PS > Import-Module ImplicitlyImporting -Force -RequiredVersion 1.0.2
PS > GetImplicitValue()
Implicit Importing from V1
PS > Get-Module Impl*

ModuleType Version PreRelease Name ExportedCommands
---------- ------- ---------- ---- ----------------
Script 1.0.2 ImplicitlyImporting GetImplicitValue
Script 2.1.2 ImplicitlyImporting GetImplicitValue

有什么想法吗?提前致谢。

最佳答案

隐式意味着导入最新版本,因此您不希望在可能需要旧版本时隐式导入。

您可以通过指定所需的子模块来减少导入 Az 模块的时间:

# takes 0.8 seconds for two modules
Import-Module Az.Accounts -RequiredVersion 2.8.0
Import-Module Az.Security -RequiredVersion 1.3.0

# takes 70 seconds for 76 modules
Import-Module Az

否则,您可以检查并明确指定要加载的版本:

# Check if Az is already loaded, and what version
$loaded = Get-Module az
if ($loaded) {
if ($Loaded.Version -eq [System.Version]'8.0.0') {'Az loaded and correct version'}
else {'Wrong version of Az loaded'}
}

# If not loaded, then check if Az is installed, and what versions
if (-not (Get-Module az)) {

# check which Az is installed
$installed = Get-InstalledModule az -AllVersions

# import correct module version
if ($installed.Version -eq [System.Version]'8.0.0') {
'Correct Version Installed'
Import-Module Az -RequiredVersion 8.0.0
}

else {
Write-Error 'Required version of Az not installed'
break
}
}

正如您在答案 here 中看到的那样,您必须明确显式导入模块或其命令才能使用特定版本

关于azure - 使用 PowerShell 隐式导入较旧的模块版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74183925/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com