From 94a54e492a18fa4a7ab45fed321146046579c10b Mon Sep 17 00:00:00 2001 From: Daniel Siegenthaler Date: Tue, 15 Jan 2019 20:27:52 +0100 Subject: [PATCH 1/2] Changing names from OneGet to PowerShellGet, some code/whitespace cleanup --- .../MSFT_PSModule/MSFT_PSModule.psm1 | 479 +++++++++--------- .../MSFT_PSModule/MSFT_PSModule.strings.psd1 | 37 -- .../en-US/MSFT_PSModule.strings.psd1 | 36 ++ .../en-US/PowerShellGet.strings.psd1 | 22 + .../DSCResources/OneGetHelper.strings.psd1 | 22 - ...etHelper.psm1 => PowerShellGetHelper.psm1} | 203 ++++---- 6 files changed, 385 insertions(+), 414 deletions(-) delete mode 100644 PowerShellGet/DSCResources/MSFT_PSModule/MSFT_PSModule.strings.psd1 create mode 100644 PowerShellGet/DSCResources/MSFT_PSModule/en-US/MSFT_PSModule.strings.psd1 create mode 100644 PowerShellGet/DSCResources/MSFT_PSModule/en-US/PowerShellGet.strings.psd1 delete mode 100644 PowerShellGet/DSCResources/OneGetHelper.strings.psd1 rename PowerShellGet/DSCResources/{OneGetHelper.psm1 => PowerShellGetHelper.psm1} (50%) diff --git a/PowerShellGet/DSCResources/MSFT_PSModule/MSFT_PSModule.psm1 b/PowerShellGet/DSCResources/MSFT_PSModule/MSFT_PSModule.psm1 index c6cf548f..e5e40c3d 100644 --- a/PowerShellGet/DSCResources/MSFT_PSModule/MSFT_PSModule.psm1 +++ b/PowerShellGet/DSCResources/MSFT_PSModule/MSFT_PSModule.psm1 @@ -10,49 +10,51 @@ # THE SOFTWARE. # -Import-LocalizedData -BindingVariable LocalizedData -filename MSFT_PSModule.strings.psd1 -Import-Module -Name "$PSScriptRoot\..\OneGetHelper.psm1" +Import-LocalizedData -BindingVariable LocalizedData -filename MSFT_PSModule.strings.psd1 +$script:localizedData = Get-LocalizedData ` + -ResourceName 'MSFT_PSModule' ` + -ResourcePath (Split-Path -Parent $Script:MyInvocation.MyCommand.Path) -#DSC Resource for the $CurrentProviderName -$CurrentProviderName="PowerShellGet" +Import-Module -Name "$PSScriptRoot\..\PowerShellGetHelper.psm1" +# DSC Resource for the $CurrentProviderName. +$CurrentProviderName = "PowerShellGet" -#Return the current state of the resource +# Return the current state of the resource. function Get-TargetResource { <# .SYNOPSIS + This DSC resource provides a mechanism to download PowerShell modules from the PowerShell + Gallery and install it on your computer. - This DSC resource provides a mechanism to download PowerShell modules from the PowerShell - Gallery and install it on your computer. - - Get-TargetResource returns the current state of the resource. + Get-TargetResource returns the current state of the resource. .PARAMETER Name - Specifies the name of the PowerShell module to be installed or uninstalled. + Specifies the name of the PowerShell module to be installed or uninstalled. .PARAMETER Repository - Specifies the name of the module source repository where the module can be found. + Specifies the name of the module source repository where the module can be found. .PARAMETER RequiredVersion - Provides the version of the module you want to install or uninstall. + Provides the version of the module you want to install or uninstall. .PARAMETER MaximumVersion - Provides the maximum version of the module you want to install or uninstall. + Provides the maximum version of the module you want to install or uninstall. .PARAMETER MinimumVersion - Provides the minimum version of the module you want to install or uninstall. + Provides the minimum version of the module you want to install or uninstall. .PARAMETER Force - Forces the installation of modules. If a module of the same name and version already exists on the computer, - this parameter overwrites the existing module with one of the same name that was found by the command. - + Forces the installation of modules. If a module of the same name and version already exists on the computer, + this parameter overwrites the existing module with one of the same name that was found by the command. + .PARAMETER AllowClobber - Allows the installation of modules regardless of if other existing module on the computer have cmdlets - of the same name - + Allows the installation of modules regardless of if other existing module on the computer have cmdlets + of the same name. + .PARAMETER SkipPublisherCheck - Allows the installation of modules that have not been catalog signed + Allows the installation of modules that have not been catalog signed. #> [CmdletBinding()] @@ -64,8 +66,8 @@ function Get-TargetResource $Name, [System.String] - $Repository="PSGallery", - + $Repository = "PSGallery", + [System.String] $RequiredVersion, @@ -80,44 +82,44 @@ function Get-TargetResource [Switch] $AllowClobber, - + [Switch] $SkipPublisherCheck ) - #Initialize the $Ensure variable + # Initialize the $Ensure variable. $ensure = 'Absent' - + $extractedArguments = ExtractArguments -FunctionBoundParameters $PSBoundParameters ` - -ArgumentNames ("Name", "Repository", "MinimumVersion", "MaximumVersion", "RequiredVersion") + -ArgumentNames ("Name", "Repository", "MinimumVersion", "MaximumVersion", "RequiredVersion") - #Get the module with the right version and repository properties + # Get the module with the right version and repository properties. $modules = Get-RightModule @extractedArguments -ErrorAction SilentlyContinue -WarningAction SilentlyContinue - #If the module is found, the count > 0 + # If the module is found, the count > 0 if ($modules.count -gt 0) { $ensure = 'Present' - Write-Verbose -Message ($localizedData.ModuleFound -f $($Name)) + Write-Verbose -Message ($localizedData.ModuleFound -f $($Name)) } else { Write-Verbose -Message ($localizedData.ModuleNotFound -f $($Name)) } - + Write-Debug -Message "Ensure of $($Name) module is $($ensure)" if ($ensure -eq 'Absent') { return @{ - Ensure = $ensure - Name = $Name - } + Ensure = $ensure + Name = $Name + } } else { - #Find a module with the latest version and return its properties + # Find a module with the latest version and return its properties. $latestModule = $modules[0] foreach ($module in $modules) @@ -128,23 +130,23 @@ function Get-TargetResource } } - #Check if the repository matches + # Check if the repository matches. $repositoryName = Get-ModuleRepositoryName -Module $latestModule -ErrorAction SilentlyContinue -WarningAction SilentlyContinue - $installationPolicy = Get-InstallationPolicy -RepositoryName $repositoryName -ErrorAction SilentlyContinue -WarningAction SilentlyContinue - + $installationPolicy = Get-InstallationPolicy -RepositoryName $repositoryName -ErrorAction SilentlyContinue -WarningAction SilentlyContinue + return @{ - Ensure = $ensure - Name = $Name - Repository = $repositoryName - Description = $latestModule.Description - Guid = $latestModule.Guid - ModuleBase = $latestModule.ModuleBase - ModuleType = $latestModule.ModuleType - Author = $latestModule.Author - InstalledVersion = $latestModule.Version - InstallationPolicy=if($installationPolicy) {"Trusted"}else{"Untrusted"} - } + Ensure = $ensure + Name = $Name + Repository = $repositoryName + Description = $latestModule.Description + Guid = $latestModule.Guid + ModuleBase = $latestModule.ModuleBase + ModuleType = $latestModule.ModuleType + Author = $latestModule.Author + InstalledVersion = $latestModule.Version + InstallationPolicy = if ($installationPolicy) {"Trusted"}else {"Untrusted"} + } } } @@ -152,63 +154,63 @@ function Test-TargetResource { <# .SYNOPSIS + This DSC resource provides a mechanism to download PowerShell modules from the PowerShell + Gallery and install it on your computer. - This DSC resource provides a mechanism to download PowerShell modules from the PowerShell - Gallery and install it on your computer. - - Test-TargetResource validates whether the resource is currently in the desired state. + Test-TargetResource validates whether the resource is currently in the desired state. .PARAMETER Ensure - Determines whether the module to be installed or uninstalled. + Determines whether the module to be installed or uninstalled. .PARAMETER Name - Specifies the name of the PowerShell module to be installed or uninstalled. + Specifies the name of the PowerShell module to be installed or uninstalled. .PARAMETER Repository - Specifies the name of the module source repository where the module can be found. + Specifies the name of the module source repository where the module can be found. .PARAMETER InstallationPolicy - Determines whether you trust the source repository where the module resides. + Determines whether you trust the source repository where the module resides. .PARAMETER RequiredVersion - Provides the version of the module you want to install or uninstall. + Provides the version of the module you want to install or uninstall. .PARAMETER MaximumVersion - Provides the maximum version of the module you want to install or uninstall. + Provides the maximum version of the module you want to install or uninstall. .PARAMETER MinimumVersion - Provides the minimum version of the module you want to install or uninstall. + Provides the minimum version of the module you want to install or uninstall. .PARAMETER Force - Forces the installation of modules. If a module of the same name and version already exists on the computer, - this parameter overwrites the existing module with one of the same name that was found by the command. - + Forces the installation of modules. If a module of the same name and version already exists on the computer, + this parameter overwrites the existing module with one of the same name that was found by the command. + .PARAMETER AllowClobber - Allows the installation of modules regardless of if other existing module on the computer have cmdlets - of the same name - + Allows the installation of modules regardless of if other existing module on the computer have cmdlets + of the same name. + .PARAMETER SkipPublisherCheck - Allows the installation of modules that have not been catalog signed + Allows the installation of modules that have not been catalog signed. #> + [CmdletBinding()] [OutputType([System.Boolean])] param ( - [ValidateSet("Present","Absent")] + [ValidateSet("Present", "Absent")] [System.String] - $Ensure="Present", + $Ensure = "Present", [parameter(Mandatory = $true)] [System.String] $Name, [System.String] - $Repository="PSGallery", + $Repository = "PSGallery", - [ValidateSet("Trusted","Untrusted")] + [ValidateSet("Trusted", "Untrusted")] [System.String] - $InstallationPolicy="Untrusted", - + $InstallationPolicy = "Untrusted", + [System.String] $RequiredVersion, @@ -220,10 +222,10 @@ function Test-TargetResource [Switch] $Force, - + [Switch] $AllowClobber, - + [Switch] $SkipPublisherCheck ) @@ -231,84 +233,82 @@ function Test-TargetResource Write-Debug -Message "Calling Test-TargetResource" $extractedArguments = ExtractArguments -FunctionBoundParameters $PSBoundParameters ` - -ArgumentNames ("Name", "Repository", "MinimumVersion", "MaximumVersion", "RequiredVersion") + -ArgumentNames ("Name", "Repository", "MinimumVersion", "MaximumVersion", "RequiredVersion") $status = Get-TargetResource @extractedArguments - #The ensure returned from Get-TargetResource is not equal to the desired $Ensure - # + # The ensure returned from Get-TargetResource is not equal to the desired $Ensure. if ($status.Ensure -ieq $Ensure) { - Write-Verbose -Message ($localizedData.InDesiredState -f $Name) - return $true + Write-Verbose -Message ($localizedData.InDesiredState -f $Name) + return $true } else { - Write-Verbose -Message ($localizedData.NotInDesiredState -f $Name) + Write-Verbose -Message ($localizedData.NotInDesiredState -f $Name) return $false - } + } } - + function Set-TargetResource { <# .SYNOPSIS + This DSC resource provides a mechanism to download PowerShell modules from the PowerShell + Gallery and install it on your computer. - This DSC resource provides a mechanism to download PowerShell modules from the PowerShell - Gallery and install it on your computer. - - Set-TargetResource sets the resource to the desired state. "Make it so". + Set-TargetResource sets the resource to the desired state. "Make it so". .PARAMETER Ensure - Determines whether the module to be installed or uninstalled. + Determines whether the module to be installed or uninstalled. .PARAMETER Name - Specifies the name of the PowerShell module to be installed or uninstalled. + Specifies the name of the PowerShell module to be installed or uninstalled. .PARAMETER Repository - Specifies the name of the module source repository where the module can be found. + Specifies the name of the module source repository where the module can be found. .PARAMETER InstallationPolicy - Determines whether you trust the source repository where the module resides. + Determines whether you trust the source repository where the module resides. .PARAMETER RequiredVersion - Provides the version of the module you want to install or uninstall. + Provides the version of the module you want to install or uninstall. .PARAMETER MaximumVersion - Provides the maximum version of the module you want to install or uninstall. + Provides the maximum version of the module you want to install or uninstall. .PARAMETER MinimumVersion - Provides the minimum version of the module you want to install or uninstall. + Provides the minimum version of the module you want to install or uninstall. .PARAMETER Force - Forces the installation of modules. If a module of the same name and version already exists on the computer, - this parameter overwrites the existing module with one of the same name that was found by the command. - + Forces the installation of modules. If a module of the same name and version already exists on the computer, + this parameter overwrites the existing module with one of the same name that was found by the command. + .PARAMETER AllowClobber - Allows the installation of modules regardless of if other existing module on the computer have cmdlets - of the same name - + Allows the installation of modules regardless of if other existing module on the computer have cmdlets + of the same name. + .PARAMETER SkipPublisherCheck - Allows the installation of modules that have not been catalog signed + Allows the installation of modules that have not been catalog signed. #> [CmdletBinding()] param ( - [ValidateSet("Present","Absent")] + [ValidateSet("Present", "Absent")] [System.String] - $Ensure="Present", + $Ensure = "Present", [parameter(Mandatory = $true)] [System.String] $Name, [System.String] - $Repository="PSGallery", + $Repository = "PSGallery", - [ValidateSet("Trusted","Untrusted")] + [ValidateSet("Trusted", "Untrusted")] [System.String] - $InstallationPolicy="Untrusted", + $InstallationPolicy = "Untrusted", [System.String] $RequiredVersion, @@ -329,167 +329,156 @@ function Set-TargetResource $SkipPublisherCheck ) - - #Validate the repository argument + # Validate the repository argument if ($PSBoundParameters.ContainsKey("Repository")) { - ValidateArgument -Argument $Repository -Type "PackageSource" -ProviderName $CurrentProviderName -Verbose + ValidateArgument -Argument $Repository -Type "PackageSource" -ProviderName $CurrentProviderName -Verbose } - if($Ensure -ieq "Present") - { - - #Version check + if ($Ensure -ieq "Present") + { + # Version check $extractedArguments = ExtractArguments -FunctionBoundParameters $PSBoundParameters ` - -ArgumentNames ("MinimumVersion","MaximumVersion", "RequiredVersion") + -ArgumentNames ("MinimumVersion", "MaximumVersion", "RequiredVersion") ValidateVersionArgument @extractedArguments $extractedArguments = ExtractArguments -FunctionBoundParameters $PSBoundParameters ` - -ArgumentNames ("Name","Repository", "MinimumVersion", "MaximumVersion","RequiredVersion") - + -ArgumentNames ("Name", "Repository", "MinimumVersion", "MaximumVersion", "RequiredVersion") + Write-Verbose -Message ($localizedData.StartFindmodule -f $($Name)) - - - $modules = Find-Module @extractedArguments -ErrorVariable ev + $modules = Find-Module @extractedArguments -ErrorVariable ev if (-not $modules) - { - - ThrowError -ExceptionName "System.InvalidOperationException" ` - -ExceptionMessage ($localizedData.ModuleNotFoundInRepository -f $Name, $ev.Exception) ` - -ErrorId "ModuleNotFoundInRepository" ` - -ErrorCategory InvalidOperation + { + ThrowError -ExceptionName "System.InvalidOperationException" ` + -ExceptionMessage ($localizedData.ModuleNotFoundInRepository -f $Name, $ev.Exception) ` + -ErrorId "ModuleNotFoundInRepository" ` + -ErrorCategory InvalidOperation } - + $trusted = $null $moduleFound = $null foreach ($m in $modules) { - #Check for the installation policy - $trusted = Get-InstallationPolicy -RepositoryName $m.Repository -ErrorAction SilentlyContinue -WarningAction SilentlyContinue - - #Stop the loop if found a trusted repository + # Check for the installation policy. + $trusted = Get-InstallationPolicy -RepositoryName $m.Repository -ErrorAction SilentlyContinue -WarningAction SilentlyContinue + + # Stop the loop if found a trusted repository. if ($trusted) - { + { $moduleFound = $m break; - } + } } - - #The respository is trusted, so we install it + # The respository is trusted, so we install it. if ($trusted) { - Write-Verbose -Message ($localizedData.StartInstallModule -f $Name, $moduleFound.Version.toString(), $moduleFound.Repository ) - - #Extract the installation options - $extractedSwitches = ExtractArguments -FunctionBoundParameters $PSBoundParameters -ArgumentNames ("Force","AllowClobber", "SkipPublisherCheck") - + Write-Verbose -Message ($localizedData.StartInstallModule -f $Name, $moduleFound.Version.toString(), $moduleFound.Repository) + + # Extract the installation options. + $extractedSwitches = ExtractArguments -FunctionBoundParameters $PSBoundParameters -ArgumentNames ("Force", "AllowClobber", "SkipPublisherCheck") + $moduleFound | Install-Module -ErrorVariable ev } - #The repository is untrusted but user's installation policy is trusted, so we install it with a warning + # The repository is untrusted but user's installation policy is trusted, so we install it with a warning. elseif ($InstallationPolicy -ieq 'Trusted') - { + { Write-Warning -Message ($localizedData.InstallationPolicyWarning -f $Name, $modules[0].Repository, $InstallationPolicy) - # Extract installation options (Force implied by InstallationPolicy) + # Extract installation options (Force implied by InstallationPolicy). $extractedSwitches = ExtractArguments -FunctionBoundParameters $PSBoundParameters -ArgumentNames ("AllowClobber", "SkipPublisherCheck") - - #if all the repositories are untrusted, we choose the first one + + # If all the repositories are untrusted, we choose the first one. $modules[0] | Install-Module @extractedSwitches -Force -ErrorVariable ev } - #Both user and repository is untrusted + # Both user and repository is untrusted else { ThrowError -ExceptionName "System.InvalidOperationException" ` - -ExceptionMessage ($localizedData.InstallationPolicyFailed -f $InstallationPolicy, "Untrusted") ` - -ErrorId "InstallationPolicyFailed" ` - -ErrorCategory InvalidOperation - } + -ExceptionMessage ($localizedData.InstallationPolicyFailed -f $InstallationPolicy, "Untrusted") ` + -ErrorId "InstallationPolicyFailed" ` + -ErrorCategory InvalidOperation + } - if ($ev) + if ($ev) { ThrowError -ExceptionName "System.InvalidOperationException" ` - -ExceptionMessage ($localizedData.FailtoInstall -f $Name, $ev.Exception) ` - -ErrorId "FailtoInstall" ` - -ErrorCategory InvalidOperation + -ExceptionMessage ($localizedData.FailtoInstall -f $Name, $ev.Exception) ` + -ErrorId "FailtoInstall" ` + -ErrorCategory InvalidOperation } else { Write-Verbose -Message ($localizedData.InstalledSuccess -f $($Name)) } - } - #Ensure=Absent - else - { - + } + # Ensure=Absent + else + { + $extractedArguments = ExtractArguments -FunctionBoundParameters $PSBoundParameters ` - -ArgumentNames ("Name", "Repository", "MinimumVersion", "MaximumVersion", "RequiredVersion") + -ArgumentNames ("Name", "Repository", "MinimumVersion", "MaximumVersion", "RequiredVersion") - - #Get the module with the right version and repository properties + # Get the module with the right version and repository properties. $modules = Get-RightModule @extractedArguments -ErrorVariable ev if ((-not $modules) -or $ev) - { + { ThrowError -ExceptionName "System.InvalidOperationException" ` - -ExceptionMessage ($localizedData.ModuleWithRightPropertyNotFound -f $Name, $ev.Exception) ` - -ErrorId "ModuleWithRightPropertyNotFound" ` - -ErrorCategory InvalidOperation + -ExceptionMessage ($localizedData.ModuleWithRightPropertyNotFound -f $Name, $ev.Exception) ` + -ErrorId "ModuleWithRightPropertyNotFound" ` + -ErrorCategory InvalidOperation } - + foreach ($module in $modules) - { - #Get the path where the module is installed - $path=$module.ModuleBase + { + # Get the path where the module is installed. + $path = $module.ModuleBase Write-Verbose -Message ($localizedData.StartUnInstallModule -f $($Name)) - - #There is no Uninstall-Module cmdlet exists, so we will remove the ModuleBase folder as an uninstall operation + + # There is no Uninstall-Module cmdlet exists, so we will remove the ModuleBase folder as an uninstall operation. Microsoft.PowerShell.Management\Remove-Item -Path $path -Force -Recurse -ErrorVariable ev - - if($ev) - { + + if ($ev) + { ThrowError -ExceptionName "System.InvalidOperationException" ` - -ExceptionMessage ($localizedData.FailtoUninstall -f $module.Name, $ev.Exception) ` - -ErrorId "FailtoUninstall" ` - -ErrorCategory InvalidOperation + -ExceptionMessage ($localizedData.FailtoUninstall -f $module.Name, $ev.Exception) ` + -ErrorId "FailtoUninstall" ` + -ErrorCategory InvalidOperation } else { - Write-Verbose -Message ($localizedData.UnInstalledSuccess -f $($module.Name)) + Write-Verbose -Message ($localizedData.UnInstalledSuccess -f $($module.Name)) } - - }#foreach - - } #Ensure=Absent + } # foreach + } # Ensure=Absent } - Function Get-RightModule { <# .SYNOPSIS - - This is a helper function. It returns the modules that meet the specified versions and the repository requirements + This is a helper function. It returns the modules that meet the specified versions and the repository requirements. .PARAMETER Name - Specifies the name of the PowerShell module. + Specifies the name of the PowerShell module. .PARAMETER RequiredVersion - Provides the version of the module you want to install or uninstall. + Provides the version of the module you want to install or uninstall. .PARAMETER MaximumVersion - Provides the maximum version of the module you want to install or uninstall. + Provides the maximum version of the module you want to install or uninstall. .PARAMETER MinimumVersion - Provides the minimum version of the module you want to install or uninstall. - + Provides the minimum version of the module you want to install or uninstall. + .PARAMETER Repository - Specifies the name of the module source repository where the module can be found. + Specifies the name of the module source repository where the module can be found. #> param @@ -512,54 +501,49 @@ Function Get-RightModule $Repository ) - Write-Verbose -Message ($localizedData.StartGetModule -f $($Name)) - + $modules = Microsoft.PowerShell.Core\Get-Module -Name $Name -ListAvailable -ErrorAction SilentlyContinue -WarningAction SilentlyContinue - + if (-not $modules) - { + { return $null } - # - #As Get-Module does not take RequiredVersion, MinimumVersion, MaximumVersion, or Repository, below we need to check - #whether the modules are containing the right version and repository location. - - $extractedArguments = ExtractArguments -FunctionBoundParameters $PSBoundParameters ` - -ArgumentNames ("MaximumVersion","MinimumVersion", "RequiredVersion") + # As Get-Module does not take RequiredVersion, MinimumVersion, MaximumVersion, or Repository, below we need to check + # whether the modules are containing the right version and repository location. - $returnVal =@() + $extractedArguments = ExtractArguments -FunctionBoundParameters $PSBoundParameters ` + -ArgumentNames ("MaximumVersion", "MinimumVersion", "RequiredVersion") + $returnVal = @() foreach ($m in $modules) - { + { $versionMatch = $false $installedVersion = $m.Version - #Case 1 - a user provides none of RequiredVersion, MinimumVersion, MaximumVersion - + # Case 1 - a user provides none of RequiredVersion, MinimumVersion, MaximumVersion if ($extractedArguments.Count -eq 0) { - $versionMatch = $true + $versionMatch = $true } - # - #Case 2 - a user provides RequiredVersion - # + + # Case 2 - a user provides RequiredVersion elseif ($extractedArguments.ContainsKey("RequiredVersion")) { - #Check if it matches with the installedversion + # Check if it matches with the installedversion $versionMatch = ($installedVersion -eq [System.Version]$RequiredVersion) - } + } else - { - #Case 3 - a user provides MinimumVersion + { + + # Case 3 - a user provides MinimumVersion if ($extractedArguments.ContainsKey("MinimumVersion")) { $versionMatch = ($installedVersion -ge [System.Version]$extractedArguments['MinimumVersion']) } - # - #Case 4 - a user provides MaximumVersion - # + + # Case 4 - a user provides MaximumVersion if ($extractedArguments.ContainsKey("MaximumVersion")) { $isLessThanMax = ($installedVersion -le [System.Version]$extractedArguments['MaximumVersion']) @@ -573,72 +557,69 @@ Function Get-RightModule $versionMatch = $isLessThanMax } } - #Case 5 - Both MinimumVersion and MaximumVersion are provided. it's covered by the above - - #Do not return $false yet to allow the foreach to continue + + # Case 5 - Both MinimumVersion and MaximumVersion are provided. It's covered by the above. + # Do not return $false yet to allow the foreach to continue if (-not $versionMatch) { - Write-Verbose -Message ($localizedData.VersionMismatch -f $($Name), $($installedVersion)) + Write-Verbose -Message ($localizedData.VersionMismatch -f $($Name), $($installedVersion)) $versionMatch = $false - } + } } - #Case 6 - Version matches but need to check if the module is from the right repository - # + # Case 6 - Version matches but need to check if the module is from the right repository. if ($versionMatch) - { - #a user does not provide Repository, we are good + { + # a user does not provide Repository, we are good if (-not $PSBoundParameters.ContainsKey("Repository")) - { - Write-Verbose -Message ($localizedData.ModuleFound -f "$($Name) $($installedVersion)") - $returnVal+=$m - + { + Write-Verbose -Message ($localizedData.ModuleFound -f "$($Name) $($installedVersion)") + $returnVal += $m } else { - #Check if the Repository matches + # Check if the Repository matches $sourceName = Get-ModuleRepositoryName -Module $m if ($Repository -ieq $sourceName) { - Write-Verbose -Message ($localizedData.ModuleFound -f "$($Name) $($installedVersion)") - $returnVal+=$m + Write-Verbose -Message ($localizedData.ModuleFound -f "$($Name) $($installedVersion)") + $returnVal += $m } else { - Write-Verbose -Message ($localizedData.RepositoryMismatch -f $($Name), $($sourceName)) - } - } + Write-Verbose -Message ($localizedData.RepositoryMismatch -f $($Name), $($sourceName)) + } + } } + } # foreach - } #foreach - - return $returnVal + return $returnVal } - + Function Get-ModuleRepositoryName { <# .SYNOPSIS - - This is a helper function that returns the module's repository name + This is a helper function that returns the module's repository name. .PARAMETER Module - Specifies the name of the PowerShell module. + Specifies the name of the PowerShell module. #> - Param + + param ( [parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] - [System.Object]$Module + [System.Object] + $Module ) - - #RepositorySourceLocation property is supported in PS V5 only. To work with the earlier PS version, we need to do a different way. - #PSGetModuleInfo.xml exists for any PS modules downloaded through PSModule provider. + # RepositorySourceLocation property is supported in PS V5 only. To work with the earlier PS version, we need to do a different way. + # PSGetModuleInfo.xml exists for any PS modules downloaded through PSModule provider. $psGetModuleInfoFileName = "PSGetModuleInfo.xml" - + $psGetModuleInfoPath = Microsoft.PowerShell.Management\Join-Path -Path $Module.ModuleBase -ChildPath $psGetModuleInfoFileName Write-Verbose -Message ($localizedData.FoundModulePath -f $($psGetModuleInfoPath)) @@ -648,7 +629,7 @@ Function Get-ModuleRepositoryName $psGetModuleInfo = Microsoft.PowerShell.Utility\Import-Clixml -Path $psGetModuleInfoPath return $psGetModuleInfo.Repository - } + } } Export-ModuleMember -function Get-TargetResource, Set-TargetResource, Test-TargetResource diff --git a/PowerShellGet/DSCResources/MSFT_PSModule/MSFT_PSModule.strings.psd1 b/PowerShellGet/DSCResources/MSFT_PSModule/MSFT_PSModule.strings.psd1 deleted file mode 100644 index 7bae9ca6..00000000 --- a/PowerShellGet/DSCResources/MSFT_PSModule/MSFT_PSModule.strings.psd1 +++ /dev/null @@ -1,37 +0,0 @@ -# -# Copyright (c) Microsoft Corporation. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -# THE SOFTWARE. -# -ConvertFrom-StringData @' -###PSLOC - FailtoUninstall=Failed to uninstall the module '{0}'. Message: {1} - FailtoInstall=Failed to install the module '{0}'. Message: {1} - InDesiredState=Resource '{0}' is in the desired state - NotInDesiredState=Resource '{0}' is not in the desired state - ModuleFound=Module '{0}' found in the node - ModuleNotFound=Module '{0}' not found in the node - ModuleWithRightPropertyNotFound=Module '{0}' with the right version or other properties not found in the node. Message: {1} - ModuleNotFoundInRepository=Module '{0}' with the right version or other properties not found in the repository. Message: {1} - StartGetModule=Begin invoking get-module '{0}' - StartFindModule=Begin invoking find-module '{0}' - StartInstallModule=Begin invoking install-module '{0}' version '{1}' from '{2}' repository - StartUnInstallModule=Begin invoking uninstall of the module '{0}' - InstalledSuccess=Successfully installed the module '{0}' - UnInstalledSuccess=Successfully uninstalled the module '{0}' - VersionMismatch=The installed Module '{0}' has the version: '{1}' - RepositoryMismatch=The installed Module '{0}' is from '{1}' repository - FoundModulePath=Found the module path:'{0}' - MultipleModuleFound=Total: '{0}' modules found with the same name. Please use RequiredVersion for filtering. Message: {1} - InstallationPolicyWarning=You are installing the module '{0}' from an untrusted repository' {1}'. Your current InstallationPolicy is '{2}'. If you trust the repository, set the policy to "Trusted". "Untrusted" otherwise. - InstallationPolicyFailed=Failed in the installation policy. Your current InstallationPolicy is '{0}' and the repository is '{1}'. If you trust the repository, set the policy to "Trusted". "Untrusted" otherwise. -###PSLOC - -'@ - diff --git a/PowerShellGet/DSCResources/MSFT_PSModule/en-US/MSFT_PSModule.strings.psd1 b/PowerShellGet/DSCResources/MSFT_PSModule/en-US/MSFT_PSModule.strings.psd1 new file mode 100644 index 00000000..0eb5b1f7 --- /dev/null +++ b/PowerShellGet/DSCResources/MSFT_PSModule/en-US/MSFT_PSModule.strings.psd1 @@ -0,0 +1,36 @@ +# +# Copyright (c) Microsoft Corporation. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. +# +# culture = "en-US" +ConvertFrom-StringData -StringData @' +###PSLOC + FailtoUninstall = Failed to uninstall the module '{0}'. Message: {1} + FailtoInstall = Failed to install the module '{0}'. Message: {1} + InDesiredState = Resource '{0}' is in the desired state. + NotInDesiredState = Resource '{0}' is not in the desired state. + ModuleFound = Module '{0}' found in the node. + ModuleNotFound = Module '{0}' not found in the node. + ModuleWithRightPropertyNotFound = Module '{0}' with the right version or other properties not found in the node. Message: {1} + ModuleNotFoundInRepository = Module '{0}' with the right version or other properties not found in the repository. Message: {1} + StartGetModule = Begin invoking Get-Module '{0}' + StartFindModule = Begin invoking Find-Module '{0}' + StartInstallModule = Begin invoking Install-Module '{0}' version '{1}' from '{2}' repository. + StartUnInstallModule = Begin invoking uninstall of the module '{0}' + InstalledSuccess = Successfully installed the module '{0}' + UnInstalledSuccess = Successfully uninstalled the module '{0}' + VersionMismatch = The installed Module '{0}' has the version: '{1}' + RepositoryMismatch = The installed Module '{0}' is from '{1}' repository. + FoundModulePath = Found the module path: '{0}' + MultipleModuleFound = Total: '{0}' modules found with the same name. Please use -RequiredVersion for filtering. Message: {1} + InstallationPolicyWarning = You are installing the module '{0}' from an untrusted repository' {1}'. Your current InstallationPolicy is '{2}'. If you trust the repository, set the policy to "Trusted". "Untrusted" otherwise. + InstallationPolicyFailed = Failed in the installation policy. Your current InstallationPolicy is '{0}' and the repository is '{1}'. If you trust the repository, set the policy to "Trusted". "Untrusted" otherwise. +###PSLOC +'@ diff --git a/PowerShellGet/DSCResources/MSFT_PSModule/en-US/PowerShellGet.strings.psd1 b/PowerShellGet/DSCResources/MSFT_PSModule/en-US/PowerShellGet.strings.psd1 new file mode 100644 index 00000000..ec703201 --- /dev/null +++ b/PowerShellGet/DSCResources/MSFT_PSModule/en-US/PowerShellGet.strings.psd1 @@ -0,0 +1,22 @@ +# +# Copyright (c) Microsoft Corporation. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. +# +# culture = "en-US" +ConvertFrom-StringData -StringData @' +###PSLOC + InValidUri = InValid Uri: '{0}'. A sample valid uri: https://www.powershellgallery.com/api/v2/. + PathDoesNotExist = Path: '{0}' does not exist. + VersionError = MinimumVersion should be less than the MaximumVersion. The MinimumVersion or MaximumVersion cannot be used with the RequiredVersion in the same command. + UnexpectedArgument = Unexpected argument type: '{0}'. + SourceNotFound = Source '{0}' not found. Please make sure you register it. + CallingFunction = "Call a function '{0}'". +###PSLOC +'@ diff --git a/PowerShellGet/DSCResources/OneGetHelper.strings.psd1 b/PowerShellGet/DSCResources/OneGetHelper.strings.psd1 deleted file mode 100644 index 37420224..00000000 --- a/PowerShellGet/DSCResources/OneGetHelper.strings.psd1 +++ /dev/null @@ -1,22 +0,0 @@ -# -# Copyright (c) Microsoft Corporation. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -# THE SOFTWARE. -# -# culture="en-US" -ConvertFrom-StringData @' -###PSLOC - InValidUri=InValid Uri: '{0}'. A sample valid uri: https://www.powershellgallery.com/api/v2/. - PathDoesNotExist=Path: '{0}' does not exist - VersionError=MinimumVersion should be less than the maximumVersion. The MinimumVersion or maximumVersion cannot be used with the RequiredVersion in the same command. - UnexpectedArgument=Unexpected argument type: '{0}' - SourceNotFound=Source '{0}' not found. Please make sure you register it. - CallingFunction="Call a function '{0}'". -###PSLOC -'@ diff --git a/PowerShellGet/DSCResources/OneGetHelper.psm1 b/PowerShellGet/DSCResources/PowerShellGetHelper.psm1 similarity index 50% rename from PowerShellGet/DSCResources/OneGetHelper.psm1 rename to PowerShellGet/DSCResources/PowerShellGetHelper.psm1 index b6409bb5..033f20e1 100644 --- a/PowerShellGet/DSCResources/OneGetHelper.psm1 +++ b/PowerShellGet/DSCResources/PowerShellGetHelper.psm1 @@ -9,85 +9,83 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. # -#Helper functions for PackageManagement DSC Resouces +# Helper functions for PackageManagement DSC Resouces +# Import Localization Strings +$script:localizedData = Get-LocalizedData ` + -ResourceName 'PowerShellGet' ` + -ResourcePath (Split-Path -Parent $Script:MyInvocation.MyCommand.Path) -Import-LocalizedData -BindingVariable LocalizedData -filename OneGetHelper.strings.psd1 - - - Function ExtractArguments +Function ExtractArguments { <# .SYNOPSIS - - This is a helper function that extract the parameters from a given table. + This is a helper function that extract the parameters from a given table. .PARAMETER FunctionBoundParameters - Specifies the hashtable containing a set of parameters to be extracted + Specifies the hashtable containing a set of parameters to be extracted. .PARAMETER ArgumentNames - Specifies A list of arguments you want to extract + Specifies a list of arguments you want to extract. #> - Param + param ( [parameter(Mandatory = $true)] [System.Collections.Hashtable] $FunctionBoundParameters, - #A list of arguments you want to extract [parameter(Mandatory = $true)] - [System.String[]]$ArgumentNames + [System.String[]] + $ArgumentNames ) - Write-Verbose -Message ($LocalizedData.CallingFunction -f $($MyInvocation.mycommand)) + Write-Verbose -Message ($localizedData.CallingFunction -f $($MyInvocation.mycommand)) - $returnValue=@{} + $returnValue = @{} foreach ($arg in $ArgumentNames) { - if($FunctionBoundParameters.ContainsKey($arg)) + if ($FunctionBoundParameters.ContainsKey($arg)) { - #Found an argument we are looking for, so we add it to return collection - $returnValue.Add($arg,$FunctionBoundParameters[$arg]) + # Found an argument we are looking for, so we add it to return collection. + $returnValue.Add($arg, $FunctionBoundParameters[$arg]) } } return $returnValue - } +} -function ThrowError +Function ThrowError { <# .SYNOPSIS - - This is a helper function that throws an error. + This is a helper function that throws an error. .PARAMETER ExceptionName - Specifies the type of errors, e.g. System.ArgumentException + Specifies the type of errors, e.g. System.ArgumentException. .PARAMETER ExceptionMessage - Specifies the exception message + Specifies the exception message. .PARAMETER ErrorId - Specifies an identifier of the error + Specifies an identifier of the error. .PARAMETER ErrorCategory - Specifies the error category, e.g., InvalidArgument defined in System.Management.Automation. - + Specifies the error category, e.g., InvalidArgument defined in System.Management.Automation. #> param - ( + ( [parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] - [System.String] + [System.String] $ExceptionName, [parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [System.String] - $ExceptionMessage, + $ExceptionMessage, [parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] @@ -99,11 +97,12 @@ function ThrowError [System.Management.Automation.ErrorCategory] $ErrorCategory ) - - Write-Verbose -Message ($LocalizedData.CallingFunction -f $($MyInvocation.mycommand)) - - $exception = New-Object -TypeName $ExceptionName -ArgumentList $ExceptionMessage; - $errorRecord = New-Object -TypeName System.Management.Automation.ErrorRecord -ArgumentList ($exception, $ErrorId, $ErrorCategory, $null) + + Write-Verbose -Message ($localizedData.CallingFunction -f $($MyInvocation.mycommand)) + + $exception = New-Object -TypeName $ExceptionName -ArgumentList $ExceptionMessage; + $errorRecord = New-Object -TypeName System.Management.Automation.ErrorRecord -ArgumentList ($exception, $ErrorId, $ErrorCategory, $null) + throw $errorRecord } @@ -111,14 +110,13 @@ Function ValidateArgument { <# .SYNOPSIS - - This is a helper function that validates the arguments. + This is a helper function that validates the arguments. .PARAMETER Argument - Specifies the argument to be validated. + Specifies the argument to be validated. .PARAMETER Type - Specifies the type of argument. + Specifies the type of argument. #> [CmdletBinding()] @@ -126,148 +124,143 @@ Function ValidateArgument ( [parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] - [string]$Argument, + [System.String] + $Argument, [parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] - [String]$Type, + [System.String] + $Type, [parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] - [String]$ProviderName + [System.String] + $ProviderName ) - Write-Verbose -Message ($LocalizedData.CallingFunction -f $($MyInvocation.mycommand)) + Write-Verbose -Message ($localizedData.CallingFunction -f $($MyInvocation.mycommand)) switch ($Type) { - "SourceUri" { # Checks whether given URI represents specific scheme - # Most common schemes: file, http, https, ftp - $scheme =@('http', 'https', 'file', 'ftp') + # Most common schemes: file, http, https, ftp + $scheme = @('http', 'https', 'file', 'ftp') $newUri = $Argument -as [System.URI] - $returnValue = ($newUri -and $newUri.AbsoluteURI -and ($scheme -icontains $newuri.Scheme)) + $returnValue = ($newUri -and $newUri.AbsoluteURI -and ($scheme -icontains $newuri.Scheme)) if ($returnValue -eq $false) - { - ThrowError -ExceptionName "System.ArgumentException" ` - -ExceptionMessage ($LocalizedData.InValidUri -f $Argument)` - -ErrorId "InValidUri" ` - -ErrorCategory InvalidArgument - } - - #Check whether it's a valid uri. Wait for the response within 2mins. - <#$result = Invoke-WebRequest $newUri -TimeoutSec 120 -UseBasicParsing -ErrorAction SilentlyContinue - - if ($null -eq (([xml]$result.Content).service )) { ThrowError -ExceptionName "System.ArgumentException" ` - -ExceptionMessage ($LocalizedData.InValidUri -f $Argument)` - -ErrorId "InValidUri" ` - -ErrorCategory InvalidArgument - }#> - + -ExceptionMessage ($LocalizedData.InValidUri -f $Argument)` + -ErrorId "InValidUri" ` + -ErrorCategory InvalidArgument + } } "DestinationPath" { $returnValue = Test-Path -Path $Argument + if ($returnValue -eq $false) { ThrowError -ExceptionName "System.ArgumentException" ` - -ExceptionMessage ($LocalizedData.PathDoesNotExist -f $Argument)` - -ErrorId "PathDoesNotExist" ` - -ErrorCategory InvalidArgument + -ExceptionMessage ($LocalizedData.PathDoesNotExist -f $Argument)` + -ErrorId "PathDoesNotExist" ` + -ErrorCategory InvalidArgument } } "PackageSource" - { - #Argument can be either the package source Name or source Uri. + { + # Argument can be either the package source Name or source Uri. - #Check if the source is a uri - $uri = $Argument -as [System.URI] + # Check if the source is a Uri. + $uri = $Argument -as [System.URI] - if($uri -and $uri.AbsoluteURI) + if ($uri -and $uri.AbsoluteURI) { - # Check if it's a valid Uri + # Check if it's a valid Uri. ValidateArgument -Argument $Argument -Type "SourceUri" -ProviderName $ProviderName } else { - #Check if it's a registered package source name + # Check if it's a registered package source name. $source = PackageManagement\Get-PackageSource -Name $Argument -ProviderName $ProviderName -verbose -ErrorVariable ev + if ((-not $source) -or $ev) { - #We do not need to throw error here as Get-PackageSource does already - Write-Verbose -Message ($LocalizedData.SourceNotFound -f $source) + # We do not need to throw error here as Get-PackageSource does already. + Write-Verbose -Message ($LocalizedData.SourceNotFound -f $source) } } } default { ThrowError -ExceptionName "System.ArgumentException" ` - -ExceptionMessage ($LocalizedData.UnexpectedArgument -f $Type)` - -ErrorId "UnexpectedArgument" ` - -ErrorCategory InvalidArgument + -ExceptionMessage ($LocalizedData.UnexpectedArgument -f $Type)` + -ErrorId "UnexpectedArgument" ` + -ErrorCategory InvalidArgument } - } + } } Function ValidateVersionArgument { <# .SYNOPSIS - - This is a helper function that does the version validation. + This is a helper function that does the version validation. .PARAMETER RequiredVersion - Provides the required version. + Provides the required version. .PARAMETER MaximumVersion - Provides the maximum version. + Provides the maximum version. .PARAMETER MinimumVersion - Provides the minimum version. + Provides the minimum version. #> [CmdletBinding()] param ( - [string]$RequiredVersion, - [string]$MinimumVersion, - [string]$MaximumVersion + [System.String] + $RequiredVersion, + [System.String] + $MinimumVersion, + + [System.String] + $MaximumVersion ) - - Write-Verbose -Message ($LocalizedData.CallingFunction -f $($MyInvocation.mycommand)) + + Write-Verbose -Message ($localizedData.CallingFunction -f $($MyInvocation.mycommand)) $isValid = $false - - #Case 1: No further check required if a user provides either none or one of these: minimumVersion, maximumVersion, and requiredVersion + + # Case 1: No further check required if a user provides either none or one of these: minimumVersion, maximumVersion, and requiredVersion. if ($PSBoundParameters.Count -le 1) { return $true } - #Case 2: #If no RequiredVersion is provided + # Case 2: #If no RequiredVersion is provided. if (-not $PSBoundParameters.ContainsKey('RequiredVersion')) { - #If no RequiredVersion, both MinimumVersion and MaximumVersion are provided. Otherwise fall into the Case #1 + # If no RequiredVersion, both MinimumVersion and MaximumVersion are provided. Otherwise fall into the Case #1. $isValid = $PSBoundParameters['MinimumVersion'] -le $PSBoundParameters['MaximumVersion'] } - - #Case 3: RequiredVersion is provided. + + # Case 3: RequiredVersion is provided. # In this case MinimumVersion and/or MaximumVersion also are provided. Otherwise fall in to Case #1. - # This is an invalid case. When RequiredVersion is provided, others are not allowed. so $isValid is false, which is already set in the init + # This is an invalid case. When RequiredVersion is provided, others are not allowed. so $isValid is false, which is already set in the init. if ($isValid -eq $false) { ThrowError -ExceptionName "System.ArgumentException" ` - -ExceptionMessage ($LocalizedData.VersionError)` - -ErrorId "VersionError" ` - -ErrorCategory InvalidArgument + -ExceptionMessage ($LocalizedData.VersionError)` + -ErrorId "VersionError" ` + -ErrorCategory InvalidArgument } } @@ -275,19 +268,17 @@ Function Get-InstallationPolicy { <# .SYNOPSIS - - This is a helper function that retrives the InstallationPolicy from the given repository. + This is a helper function that retrives the InstallationPolicy from the given repository. .PARAMETER RepositoryName - Provides the repository Name. - + Provides the repository Name. #> - Param + param ( [parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] - [System.String]$RepositoryName + [System.String] $RepositoryName ) Write-Verbose -Message ($LocalizedData.CallingFunction -f $($MyInvocation.mycommand)) @@ -295,7 +286,7 @@ Function Get-InstallationPolicy $repositoryobj = PackageManagement\Get-PackageSource -Name $RepositoryName -ErrorAction SilentlyContinue -WarningAction SilentlyContinue if ($repositoryobj) - { + { return $repositoryobj.IsTrusted - } + } } From ccc72521fcabc837ef289045e016d4898cc29f2e Mon Sep 17 00:00:00 2001 From: Daniel Siegenthaler Date: Wed, 16 Jan 2019 04:27:11 +0100 Subject: [PATCH 2/2] Some small changes in modules --- .../DSCResources/MSFT_PSModule/MSFT_PSModule.psm1 | 10 +++++----- PowerShellGet/DSCResources/PowerShellGetHelper.psm1 | 6 ++++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/PowerShellGet/DSCResources/MSFT_PSModule/MSFT_PSModule.psm1 b/PowerShellGet/DSCResources/MSFT_PSModule/MSFT_PSModule.psm1 index e5e40c3d..df602590 100644 --- a/PowerShellGet/DSCResources/MSFT_PSModule/MSFT_PSModule.psm1 +++ b/PowerShellGet/DSCResources/MSFT_PSModule/MSFT_PSModule.psm1 @@ -112,7 +112,7 @@ function Get-TargetResource if ($ensure -eq 'Absent') { - return @{ + $returnValue = @{ Ensure = $ensure Name = $Name } @@ -135,7 +135,7 @@ function Get-TargetResource $installationPolicy = Get-InstallationPolicy -RepositoryName $repositoryName -ErrorAction SilentlyContinue -WarningAction SilentlyContinue - return @{ + $returnValue = @{ Ensure = $ensure Name = $Name Repository = $repositoryName @@ -148,6 +148,7 @@ function Get-TargetResource InstallationPolicy = if ($installationPolicy) {"Trusted"}else {"Untrusted"} } } + return $returnValue } function Test-TargetResource @@ -481,6 +482,7 @@ Function Get-RightModule Specifies the name of the module source repository where the module can be found. #> + [CmdletBinding()] param ( [parameter(Mandatory = $true)] @@ -593,7 +595,6 @@ Function Get-RightModule } } } # foreach - return $returnVal } @@ -607,6 +608,7 @@ Function Get-ModuleRepositoryName Specifies the name of the PowerShell module. #> + [CmdletBinding()] param ( [parameter(Mandatory = $true)] @@ -617,9 +619,7 @@ Function Get-ModuleRepositoryName # RepositorySourceLocation property is supported in PS V5 only. To work with the earlier PS version, we need to do a different way. # PSGetModuleInfo.xml exists for any PS modules downloaded through PSModule provider. - $psGetModuleInfoFileName = "PSGetModuleInfo.xml" - $psGetModuleInfoPath = Microsoft.PowerShell.Management\Join-Path -Path $Module.ModuleBase -ChildPath $psGetModuleInfoFileName Write-Verbose -Message ($localizedData.FoundModulePath -f $($psGetModuleInfoPath)) diff --git a/PowerShellGet/DSCResources/PowerShellGetHelper.psm1 b/PowerShellGet/DSCResources/PowerShellGetHelper.psm1 index 033f20e1..0c22eba6 100644 --- a/PowerShellGet/DSCResources/PowerShellGetHelper.psm1 +++ b/PowerShellGet/DSCResources/PowerShellGetHelper.psm1 @@ -28,7 +28,8 @@ Function ExtractArguments .PARAMETER ArgumentNames Specifies a list of arguments you want to extract. #> - + + [CmdletBinding()] param ( [parameter(Mandatory = $true)] @@ -52,7 +53,6 @@ Function ExtractArguments $returnValue.Add($arg, $FunctionBoundParameters[$arg]) } } - return $returnValue } @@ -75,6 +75,7 @@ Function ThrowError Specifies the error category, e.g., InvalidArgument defined in System.Management.Automation. #> + [CmdletBinding()] param ( [parameter(Mandatory = $true)] @@ -274,6 +275,7 @@ Function Get-InstallationPolicy Provides the repository Name. #> + [CmdletBinding()] param ( [parameter(Mandatory = $true)]