@@ -74,38 +74,29 @@ function Set-TargetResource
74
74
)
75
75
Write-Verbose " Start Set-TargetResource"
76
76
77
- if ($Ensure -eq " Present" )
78
- {
79
- if ($Credentials -eq $null )
80
- {
81
- if ($priority -eq $null )
82
- {
83
- choco sources add - n" $name " - s" $source "
84
- }
85
- else
86
- {
87
- choco sources add - n" $name " - s" $source " -- priority= $priority
88
- }
89
- }
90
- else
91
- {
92
- $username = $Credentials.UserName
93
- $password = $Credentials.GetNetworkCredential ().Password
94
-
95
- if ($priority -eq $null )
96
- {
97
- choco sources add - n" $name " - s" $source " - u= " $username " - p= " $password "
98
- }
99
- else
100
- {
101
- choco sources add - n" $name " - s" $source " - u= " $username " - p= " $password " -- priority= $priority
102
- }
103
- }
104
- }
105
- else
106
- {
107
- choco sources remove - n" $name "
108
- }
77
+ # Remove source if we'removing or updating it.
78
+ # If the source does not exists, this is a noop.
79
+ choco sources remove - n" $name "
80
+
81
+ if ($Ensure -eq " Present" )
82
+ {
83
+ $args = @ (" sources" , " add" , " -n`" $name `" " , " -s`" $source `" " )
84
+
85
+ if ($null -ne $priority )
86
+ {
87
+ $args += " --priority=$priority "
88
+ }
89
+
90
+ if ($null -ne $Credentials )
91
+ {
92
+ $username = $Credentials.UserName
93
+ $password = $Credentials.GetNetworkCredential ().Password
94
+
95
+ $args += @ (" -u=`" $username `" " , " -p=`" $password `" " )
96
+ }
97
+
98
+ & choco $args
99
+ }
109
100
}
110
101
111
102
function Test-TargetResource
@@ -134,53 +125,57 @@ function Test-TargetResource
134
125
135
126
Write-Verbose " Start Test-TargetResource"
136
127
137
- if ($env: ChocolateyInstall -eq " " -or $env: ChocolateyInstall -eq $null )
138
- {
139
- $exe = (get-command choco).Source
140
- $chocofolder = $exe.Substring (0 , $exe.LastIndexOf (" \" ))
141
-
142
- if ( $chocofolder.EndsWith (" bin" ) )
143
- {
144
- $chocofolder = $chocofolder.Substring (0 , $chocofolder.LastIndexOf (" \" ))
145
- }
146
- }
147
- else
148
- {
149
- $chocofolder = $env: ChocolateyInstall
150
- }
151
- $configfolder = " $chocofolder \config"
152
- $configfile = Get-ChildItem $configfolder | Where-Object {$_.Name -match " chocolatey.config$" }
153
-
154
- $xml = [xml ](Get-Content $configfile.FullName )
155
- $sources = $xml.chocolatey.sources.source
156
-
157
- foreach ($chocosource in $sources )
158
- {
159
- if ($chocosource.id -eq $name -and $ensure -eq ' Present' )
160
- {
161
- if ($chocosource.priority -eq $Priority )
128
+ if ($env: ChocolateyInstall -eq " " -or $null -eq $env: ChocolateyInstall )
129
+ {
130
+ $exe = (get-command choco).Source
131
+ $chocofolder = $exe.Substring (0 , $exe.LastIndexOf (" \" ))
132
+
133
+ if ( $chocofolder.EndsWith (" bin" ) )
134
+ {
135
+ $chocofolder = $chocofolder.Substring (0 , $chocofolder.LastIndexOf (" \" ))
136
+ }
137
+ }
138
+ else
139
+ {
140
+ $chocofolder = $env: ChocolateyInstall
141
+ }
142
+ $configfolder = " $chocofolder \config"
143
+ $configfile = Get-ChildItem $configfolder | Where-Object {$_.Name -match " chocolatey.config$" }
144
+
145
+ $xml = [xml ](Get-Content $configfile.FullName )
146
+ $sources = $xml.chocolatey.sources.source
147
+
148
+ foreach ($chocosource in $sources )
149
+ {
150
+ if ($chocosource.id -eq $name -and $ensure -eq ' Present' )
151
+ {
152
+ $configMatches = $true
153
+ if ($chocosource.value -ne $Source )
162
154
{
163
- return $true
155
+ $configMatches = $false
164
156
}
165
- else
157
+
158
+ if ($chocosource.priority -ne $Priority )
166
159
{
167
- return $false
160
+ $configMatches = $false
168
161
}
169
- }
170
- elseif ($chocosource.id -eq $name -and $ensure -eq ' Absent' )
171
- {
172
- return $false
173
- }
174
- }
175
-
176
- if ($Ensure -eq ' Present' )
177
- {
178
- return $false
179
- }
180
- else
181
- {
182
- return $true
183
- }
162
+
163
+ return $configMatches
164
+ }
165
+ elseif ($chocosource.id -eq $name -and $ensure -eq ' Absent' )
166
+ {
167
+ return $false
168
+ }
169
+ }
170
+
171
+ if ($Ensure -eq ' Present' )
172
+ {
173
+ return $false
174
+ }
175
+ else
176
+ {
177
+ return $true
178
+ }
184
179
}
185
180
186
181
0 commit comments