DscResources/MSFT_PSRepository/MSFT_PSRepository.psm1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
$resourceModuleRoot = Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent # Import localization helper functions. $helperName = 'PowerShellGet.LocalizationHelper' $dscResourcesFolderFilePath = Join-Path -Path $resourceModuleRoot -ChildPath "Modules\$helperName\$helperName.psm1" Import-Module -Name $dscResourcesFolderFilePath $script:localizedData = Get-LocalizedData -ResourceName 'MSFT_PSRepository' -ScriptRoot $PSScriptRoot # Import resource helper functions $helperName = 'PowerShellGet.ResourceHelper' $dscResourcesFolderFilePath = Join-Path -Path $resourceModuleRoot -ChildPath "Modules\$helperName\$helperName.psm1" Import-Module -Name $dscResourcesFolderFilePath -force <# .SYNOPSIS Returns the current state of the repository. .PARAMETER Name Specifies the name of the repository to manage. #> function Get-TargetResource { <# These suppressions are added because this repository have other Visual Studio Code workspace settings than those in DscResource.Tests DSC test framework. Only those suppression that contradict this repository guideline is added here. #> [Diagnostics.CodeAnalysis.SuppressMessageAttribute('DscResource.AnalyzerRules\Measure-FunctionBlockBraces', '')] [Diagnostics.CodeAnalysis.SuppressMessageAttribute('DscResource.AnalyzerRules\Measure-IfStatement', '')] [CmdletBinding()] [OutputType([System.Collections.Hashtable])] param ( [Parameter(Mandatory = $true)] [System.String] $Name ) $returnValue = @{ Ensure = 'Absent' Name = $Name URL = $null Priority = $null Trusted = $false Registered = $false } Write-Verbose -Message ($localizedData.GetTargetResourceMessage -f $Name) $repository = Get-PSResourceRepository -Name $Name -ErrorAction 'SilentlyContinue' if ($repository) { $returnValue.Ensure = 'Present' $returnValue.URL = $repository.URL $returnValue.Priority = $repository.Priority $returnValue.Trusted = $repository.Trusted $returnValue.Registered = $repository.Registered } else { Write-Verbose -Message ($localizedData.RepositoryNotFound -f $Name) } return $returnValue } <# .SYNOPSIS Determines if the repository is in the desired state. .PARAMETER Ensure If the repository should be present or absent on the server being configured. Default values is 'Present'. .PARAMETER Name Specifies the name of the repository to manage. .PARAMETER URL Specifies the URI for discovering and installing modules from this repository. A URI can be a NuGet server feed, HTTP, HTTPS, FTP or file location. .PARAMETER Trusted Specifies the installation policy. Valid values are 'Trusted' or 'Untrusted'. The default value is 'Untrusted'. #> function Test-TargetResource { <# These suppressions are added because this repository have other Visual Studio Code workspace settings than those in DscResource.Tests DSC test framework. Only those suppression that contradict this repository guideline is added here. #> [Diagnostics.CodeAnalysis.SuppressMessageAttribute('DscResource.AnalyzerRules\Measure-FunctionBlockBraces', '')] [Diagnostics.CodeAnalysis.SuppressMessageAttribute('DscResource.AnalyzerRules\Measure-IfStatement', '')] [CmdletBinding()] [OutputType([System.Boolean])] param ( [Parameter()] [ValidateSet('Present', 'Absent')] [System.String] $Ensure = 'Present', [Parameter(Mandatory = $true)] [System.String] $Name, [Parameter()] [System.String] $URL, [Parameter()] [System.Int32] $Priority, [Parameter()] [System.Boolean] $Trusted = $false ) Write-Verbose -Message ($localizedData.TestTargetResourceMessage -f $Name) $returnValue = $false $getTargetResourceResult = Get-TargetResource -Name $Name if ($Ensure -eq $getTargetResourceResult.Ensure) { if ($getTargetResourceResult.Ensure -eq 'Present' ) { $returnValue = Test-DscParameterState ` -CurrentValues $getTargetResourceResult ` -DesiredValues $PSBoundParameters ` -ValuesToCheck @( 'URL' 'Priority' 'Trusted' ) } else { $returnValue = $true } } if ($returnValue) { Write-Verbose -Message ($localizedData.InDesiredState -f $Name) } else { Write-Verbose -Message ($localizedData.NotInDesiredState -f $Name) } return $returnValue } <# .SYNOPSIS Creates, removes or updates the repository. .PARAMETER Ensure If the repository should be present or absent on the server being configured. Default values is 'Present'. .PARAMETER Name Specifies the name of the repository to manage. .PARAMETER URL Specifies the URI for discovering and installing modules from this repository. A URI can be a NuGet server feed, HTTP, HTTPS, FTP or file location. .PARAMETER Priority Specifies the priority for the URI for the script source location. .PARAMETER Trusted Specifies the installation policy. Valid values are 'Trusted' or 'Untrusted'. The default value is 'Untrusted'. #> function Set-TargetResource { <# These suppressions are added because this repository have other Visual Studio Code workspace settings than those in DscResource.Tests DSC test framework. Only those suppression that contradict this repository guideline is added here. #> [Diagnostics.CodeAnalysis.SuppressMessageAttribute('DscResource.AnalyzerRules\Measure-FunctionBlockBraces', '')] [Diagnostics.CodeAnalysis.SuppressMessageAttribute('DscResource.AnalyzerRules\Measure-IfStatement', '')] [CmdletBinding()] param ( [Parameter()] [ValidateSet('Present', 'Absent')] [System.String] $Ensure = 'Present', [Parameter(Mandatory = $true)] [System.String] $Name, [Parameter()] [System.String] $URL, [Parameter()] [System.String] $Priority = '50', [Parameter()] [System.Boolean] $TrustRepository = $false ) $getTargetResourceResult = Get-TargetResource -Name $Name # Determine if the repository should be present or absent. if ($Ensure -eq 'Present') { $repositoryParameters = New-SplatParameterHashTable ` -FunctionBoundParameters $PSBoundParameters ` -ArgumentNames @( 'Name' 'URL' 'Priority' 'TrustRepository' ) Write-Host("c1") # Determine if the repository is already present. if ($getTargetResourceResult.Ensure -eq 'Present') { Write-Verbose -Message ($localizedData.RepositoryExist -f $Name) Write-Host("c2") # Repository exist, update the properties. Set-PSResourceRepository @repositoryParameters -ErrorAction 'Stop' } else { Write-Host("c3") Write-Verbose -Message ($localizedData.RepositoryDoesNotExist -f $Name) # Repository did not exist, create the repository. Register-PSResourceRepository @repositoryParameters -ErrorAction 'Stop' } } else { if ($getTargetResourceResult.Ensure -eq 'Present') { Write-Verbose -Message ($localizedData.RemoveExistingRepository -f $Name) Write-Host("c4") # Repository did exist, remove the repository. Unregister-PSResourceRepository -Name $Name -ErrorAction 'Stop' } } } |