Install the NuGet package provider if needed (#52129)

* Install the NuGet package provider if needed

* Add the changelog fragment file
pull/52351/head
Wojciech Sciesinski 7 years ago committed by Toshio Kuratomi
parent 396a2f7471
commit b7b4182bbd

@ -0,0 +1,2 @@
bugfixes:
- win_psmodule - the NuGet package provider will be updated, if needed, to avoid issue under adding a repository

@ -25,8 +25,8 @@ Function Install-NugetProvider {
param(
[bool]$CheckMode
)
$PackageProvider = Get-PackageProvider -ListAvailable|?{($_.name -eq 'Nuget') -and ($_.version -ge "2.8.5.201")}
if (!($PackageProvider)){
$PackageProvider = Get-PackageProvider -ListAvailable| Where-Object {($_.name -eq 'Nuget') -and ($_.version -ge "2.8.5.201")}
if (-not ($PackageProvider)){
try{
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force -ErrorAction Stop -WhatIf:$CheckMode | out-null
$result.changed = $true
@ -50,10 +50,11 @@ Function Install-Repository {
$Repo = (Get-PSRepository).SourceLocation
# If repository isn't already present, try to register it as trusted.
if ($Repo -notcontains $Url){
if ($Repo -notcontains $Url){
try {
if (!($CheckMode)) {
Register-PSRepository -Name $Name -SourceLocation $Url -InstallationPolicy Trusted -ErrorAction Stop
if (-not ($CheckMode)) {
Install-NugetProvider
Register-PSRepository -Name $Name -SourceLocation $Url -InstallationPolicy Trusted -ErrorAction Stop
}
$result.changed = $true
$result.repository_changed = $true
@ -76,9 +77,10 @@ Function Remove-Repository{
# Try to remove the repository
if ($Repo -contains $Name){
try {
try {
if (!($CheckMode)) {
Unregister-PSRepository -Name $Name -ErrorAction Stop
Install-NugetProvider
Unregister-PSRepository -Name $Name -ErrorAction Stop
}
$result.changed = $true
$result.repository_changed = $true
@ -98,13 +100,13 @@ Function Install-PsModule {
[bool]$AllowClobber,
[bool]$CheckMode
)
if (Get-Module -Listavailable|?{$_.name -eq $Name}){
if (Get-Module -Listavailable| Where-Object {$_.name -eq $Name}){
$result.output = "Module $($Name) already present"
}
else {
else {
try{
# Install NuGet Provider if needed
Install-NugetProvider -CheckMode $CheckMode;
Install-NugetProvider -CheckMode $CheckMode
$ht = @{
Name = $Name;
@ -115,16 +117,16 @@ Function Install-PsModule {
# If specified, use repository name to select module source
if ($Repository) {
$ht["Repository"] = "$Repository";
$ht["Repository"] = "$Repository"
}
# Check Powershell Version (-AllowClobber was introduced in PowerShellGet 1.6.0)
if ("AllowClobber" -in ((Get-Command PowerShellGet\Install-Module | Select -ExpandProperty Parameters).Keys)) {
$ht['AllowClobber'] = $AllowClobber;
if ("AllowClobber" -in ((Get-Command PowerShellGet\Install-Module | Select-Object -ExpandProperty Parameters).Keys)) {
$ht['AllowClobber'] = $AllowClobber
}
Install-Module @ht | out-null;
Install-Module @ht | out-null
$result.output = "Module $($Name) installed"
$result.changed = $true
}
@ -142,7 +144,7 @@ Function Remove-PsModule {
[bool]$CheckMode
)
# If module is present, unistalls it.
if (Get-Module -Listavailable|?{$_.name -eq $Name}){
if (Get-Module -Listavailable| Where-Object {$_.name -eq $Name}){
try{
Uninstall-Module -Name $Name -Confirm:$false -Force -ErrorAction Stop -WhatIf:$CheckMode | out-null
$result.output = "Module $($Name) removed"
@ -168,16 +170,16 @@ if ($PsVersion.Major -lt 5){
if ($state -eq "present") {
if (($repo) -and ($url)) {
Install-Repository -Name $repo -Url $url -CheckMode $check_mode
Install-Repository -Name $repo -Url $url -CheckMode $check_mode
}
else {
$ErrorMessage = "Repository Name and Url are mandatory if you want to add a new repository"
}
Install-PsModule -Name $Name -Repository $repo -CheckMode $check_mode -AllowClobber $allow_clobber;
Install-PsModule -Name $Name -Repository $repo -CheckMode $check_mode -AllowClobber $allow_clobber
}
else {
if ($repo) {
else {
if ($repo) {
Remove-Repository -Name $repo -CheckMode $check_mode
}
Remove-PsModule -Name $Name -CheckMode $check_mode

@ -53,7 +53,6 @@ lib/ansible/modules/windows/win_pagefile.ps1 PSAvoidUsingPositionalParameters
lib/ansible/modules/windows/win_pagefile.ps1 PSAvoidUsingWMICmdlet
lib/ansible/modules/windows/win_pagefile.ps1 PSUseDeclaredVarsMoreThanAssignments
lib/ansible/modules/windows/win_pagefile.ps1 PSUseSupportsShouldProcess
lib/ansible/modules/windows/win_psmodule.ps1 PSAvoidUsingCmdletAliases
lib/ansible/modules/windows/win_rabbitmq_plugin.ps1 PSAvoidUsingCmdletAliases
lib/ansible/modules/windows/win_rabbitmq_plugin.ps1 PSAvoidUsingInvokeExpression
lib/ansible/modules/windows/win_region.ps1 PSAvoidUsingEmptyCatchBlock

Loading…
Cancel
Save