有没有一种简单的方法可以将版本作为参数传递给Get-Module?
我安装了两个不同版本的Azure PowerShell:
C:\WINDOWS\system32> get-module -name azure -listavailable
Directory: C:\Program Files\WindowsPowerShell\Modules\...
ModuleType Version Name
---------- ------- ----
Manifest 1.0.4 Azure
Directory: C:\Program Files (x86)\Microsoft SDKs\Azure\...
ModuleType Version Name
---------- ------- ----
Manifest 1.0.2 Azure
Run Code Online (Sandbox Code Playgroud)
我希望使用如下命令删除其中一个:
Get-Module -name azure -version 1.0.2 | remove-module
Run Code Online (Sandbox Code Playgroud)
请参阅: get-help Remove-Module -full
-FullyQualifiedName [<String[]>]
Removes modules with names that are specified in the form of
ModuleSpecification objects (described by the Remarks section of Module
Specification Constructor (Hashtable) on MSDN). For example, the
FullyQualifiedName parameter accepts a module name that is specified in the
format @{ModuleName = "modulename"; ModuleVersion = "version_number"} or
@{ModuleName = "modulename"; ModuleVersion = "version_number"; Guid =
"GUID"}. ModuleName and ModuleVersion are required, but Guid is optional.
You cannot specify the FullyQualifiedName parameter in the same command as a
Name parameter; the two parameters are mutually exclusive.
Run Code Online (Sandbox Code Playgroud)
注意:
FullyQualifiedName参数接受以@ {ModuleName ="modulename";格式指定的模块名称; ModuleVersion ="version_number"}
基于此,以下内容应该是您所追求的:
Remove-Module -FullyQualifiedName @{ModuleName = "Azure"; ModuleVersion = "1.0.2"}
Run Code Online (Sandbox Code Playgroud)
改进的答案(编辑)
对此进行了更多的调查,并且在删除使用时有一些怪癖ModuleVersion(可以是a string或a [version]).
如果指定ModuleVersion,Remove-Module将删除具有该版本及更高版本的所有匹配模块.
要获得明确的匹配,您还必须通过guid.
Get-Module 'Azure' | where {([string]($_.Version)).StartsWith('1.0.2')} | Remove-Module
Run Code Online (Sandbox Code Playgroud)
由于输入很多,我建议您在配置文件中添加一个功能,以便更轻松.
function Remove-ModuleWithVersion
{
param (
[Parameter(Mandatory=$true)]
[string]
$Module,
[Parameter(Mandatory=$true)]
[string]
$VersionToMatch
)
Get-Module $Module | where {([string]($_.Version)).StartsWith($VersionToMatch)} | Remove-Module -Verbose
}
Run Code Online (Sandbox Code Playgroud)
并打电话给:
Remove-ModuleWithVersion 'Azure' '1.0.2'
Run Code Online (Sandbox Code Playgroud)
测试和分析
我不妨分享一下我如何测试这个来得出我的结论.我会给读者留下一些细节进行自我调查......
创建两个模块,并使用不同的函数名称从每个模块导出一个函数,以使测试更容易.
D:\ test\modtest\v1\ModTest.psd1,版本为1.1.0.1
D:\ test\modtest\v1\ModTest.psm1
function Show-Hello1
{
"Hello v1.1"
}
Export-ModuleMember -Function Show-Hello1
Run Code Online (Sandbox Code Playgroud)
D:\ test\modtest\v2\ModTest.psd1,版本1.2.0.2
D:\ test\modtest\v2\ModTest.psm1
function Show-Hello2
{
"Hello v1.2"
}
Export-ModuleMember -Function Show-Hello2
Run Code Online (Sandbox Code Playgroud)
创建一个函数来加载模块,调用导出的函数,显示模块之前,使用指定的参数删除模块,之后显示模块.
function Invoke-LoadAndRemove($minor, $useString, $guid = $null)
{
"`n------`n$($minor), $($useString), '$($guid)'"
"`nimport modules..."
ipmo D:\test\modtest\v1\ModTest.psd1 -Force #-Verbose
ipmo D:\test\modtest\v2\ModTest.psd1 -Force #-Verbose
"call exported functions..."
Show-Hello1
Show-Hello2
"modules before..."
Get-Module *mod*
if ($useString)
{
$ver = "1.$minor.0.$minor"
}
else
{
$ver = [version]::new(1, $minor, 0, $minor)
}
"`nremoving version: $ver, -> $($ver.GetType()), useGuid=$($useGuid)"
if ($useGuid)
{
Remove-Module -FullyQualifiedName @{ModuleName = "ModTest"; ModuleVersion = $ver} -Verbose
}
else
{
Remove-Module -FullyQualifiedName @{ModuleName = "ModTest"; ModuleVersion = $ver; Guid = $guid} -Verbose
}
"modules after..."
Get-Module *mod*
"done."
}
Run Code Online (Sandbox Code Playgroud)
调用各种方法来演示Remove-Module的工作原理......
Invoke-LoadAndRemove 2 $true
Invoke-LoadAndRemove 1 $true
Invoke-LoadAndRemove 2 $false
Invoke-LoadAndRemove 1 $false
Invoke-LoadAndRemove 1 $false 'b213dea3-4ae3-4fde-a9c6-0ac4a8d1890c'
Invoke-LoadAndRemove 2 $true '02fdc44a-2c32-4f7b-8573-b1317b03269a'
Run Code Online (Sandbox Code Playgroud)
这是一部分,我将它留给读者进一步分析输出以验证本文的结论.
也就是说,这是我注意到的,这让我进一步调查:
modules before...
Script 1.2.0.2 ModTest Show-Hello2
Script 1.1.0.1 ModTest Show-Hello1
removing version: 1.2.0.2, -> string, useGuid=
VERBOSE: Performing the operation "Remove-Module" on target "ModTest (Path: 'D:\test\modtest\v2\ModTest.psm1')".
VERBOSE: Removing the imported "Show-Hello2" function.
modules after...
Script 1.1.0.1 ModTest Show-Hello1
done.
------
modules before...
Script 1.2.0.2 ModTest Show-Hello2
Script 1.1.0.1 ModTest Show-Hello1
removing version: 1.1.0.1, -> string, useGuid=
VERBOSE: Performing the operation "Remove-Module" on target "ModTest (Path: 'D:\test\modtest\v2\ModTest.psm1')".
VERBOSE: Performing the operation "Remove-Module" on target "ModTest (Path: 'D:\test\modtest\v1\ModTest.psm1')".
VERBOSE: Removing the imported "Show-Hello2" function.
VERBOSE: Removing the imported "Show-Hello1" function.
modules after...
done.
Run Code Online (Sandbox Code Playgroud)
请注意,在删除1.2.0.2时,它按预期工作.删除1.1.0.1时,将删除1.1.0.1和1.2.0.2!
| 归档时间: |
|
| 查看次数: |
4228 次 |
| 最近记录: |