@@ -100,10 +100,11 @@ protected override void ProcessRecord()
100
100
}
101
101
else if ( ! Utils . TryParseVersionOrVersionRange ( Version , out _versionRange ) )
102
102
{
103
- var exMessage = "Argument for -Version parameter is not in the proper format." ;
104
- var ex = new ArgumentException ( exMessage ) ;
105
- var IncorrectVersionFormat = new ErrorRecord ( ex , "IncorrectVersionFormat" , ErrorCategory . InvalidArgument , null ) ;
106
- ThrowTerminatingError ( IncorrectVersionFormat ) ;
103
+ ThrowTerminatingError ( new ErrorRecord (
104
+ new ArgumentException ( "Argument for -Version parameter is not in the proper format." ) ,
105
+ "IncorrectVersionFormat" ,
106
+ ErrorCategory . InvalidArgument ,
107
+ this ) ) ;
107
108
}
108
109
109
110
Name = Utils . ProcessNameWildcards ( Name , removeWildcardEntries : false , out string [ ] errorMsgs , out bool _ ) ;
@@ -124,13 +125,12 @@ protected override void ProcessRecord()
124
125
return ;
125
126
}
126
127
127
- if ( ! UninstallPkgHelper ( ) )
128
+ if ( ! UninstallPkgHelper ( out List < ErrorRecord > errRecords ) )
128
129
{
129
- // any errors should be caught lower in the stack
130
- var exMessage = "Did not successfully uninstall package" ;
131
- var ex = new Exception ( exMessage ) ;
132
- var UninstallPackageError = new ErrorRecord ( ex , "UninstallResourceError" , ErrorCategory . InvalidOperation , null ) ;
133
- WriteError ( UninstallPackageError ) ;
130
+ foreach ( var err in errRecords )
131
+ {
132
+ WriteError ( err ) ;
133
+ }
134
134
}
135
135
break ;
136
136
@@ -142,20 +142,20 @@ protected override void ProcessRecord()
142
142
version : inputObjectVersion ,
143
143
versionRange : out _versionRange ) )
144
144
{
145
- var exMessage = String . Format ( "Version '{0}' for resource '{1}' cannot be parsed." , inputObj . Version . ToString ( ) , inputObj . Name ) ;
146
- var ex = new ArgumentException ( exMessage ) ;
147
- var ErrorParsingVersion = new ErrorRecord ( ex , "ErrorParsingVersion" , ErrorCategory . ParserError , null ) ;
148
- WriteError ( ErrorParsingVersion ) ;
145
+ WriteError ( new ErrorRecord (
146
+ new ArgumentException ( $ "Error parsing version '{ inputObj . Version } ' for resource '{ inputObj . Name } '.") ,
147
+ "ErrorParsingVersion" ,
148
+ ErrorCategory . ParserError ,
149
+ this ) ) ;
149
150
}
150
151
151
152
Name = new string [ ] { inputObj . Name } ;
152
- if ( ! String . IsNullOrWhiteSpace ( inputObj . Name ) && ! UninstallPkgHelper ( ) )
153
+ if ( ! String . IsNullOrWhiteSpace ( inputObj . Name ) && ! UninstallPkgHelper ( out List < ErrorRecord > InputObjErrRecords ) )
153
154
{
154
- // specific errors will be displayed lower in the stack
155
- var exMessage = String . Format ( string . Format ( "Did not successfully uninstall package {0}" , inputObj . Name ) ) ;
156
- var ex = new ArgumentException ( exMessage ) ;
157
- var UninstallResourceError = new ErrorRecord ( ex , "UninstallResourceError" , ErrorCategory . InvalidOperation , null ) ;
158
- WriteError ( UninstallResourceError ) ;
155
+ foreach ( var err in InputObjErrRecords )
156
+ {
157
+ WriteError ( err ) ;
158
+ }
159
159
}
160
160
}
161
161
break ;
@@ -175,26 +175,40 @@ protected override void EndProcessing()
175
175
176
176
#region Private methods
177
177
178
- private bool UninstallPkgHelper ( )
178
+ private bool UninstallPkgHelper ( out List < ErrorRecord > errRecords )
179
179
{
180
180
var successfullyUninstalled = false ;
181
-
182
181
GetHelper getHelper = new GetHelper ( this ) ;
183
182
List < string > dirsToDelete = getHelper . FilterPkgPathsByName ( Name , _pathsToSearch ) ;
184
183
int totalDirs = dirsToDelete . Count ;
184
+ errRecords = new List < ErrorRecord > ( ) ;
185
+
186
+ if ( totalDirs == 0 ) {
187
+ string message = Version == null || Version . Trim ( ) . Equals ( "*" ) ?
188
+ string . Format ( "Cannot uninstall resource '{0}' because it does not exist" , String . Join ( ", " , Name ) ) :
189
+
190
+ string . Format ( "Cannot uninstall verison '{0}' of resource '{1}' because it does not exist" , Version , String . Join ( ", " , Name ) ) ;
185
191
186
- // Checking if module or script
187
- // a module path will look like:
192
+
193
+ errRecords . Add ( new ErrorRecord (
194
+ new PackageNotFoundException ( message ) ,
195
+ "ResourceNotInstalled" ,
196
+ ErrorCategory . ObjectNotFound ,
197
+ this ) ) ;
198
+
199
+ return successfullyUninstalled ;
200
+ }
201
+
202
+ // A module path will look like:
188
203
// ./Modules/TestModule/0.0.1
189
204
// note that the xml file is located in this path, eg: ./Modules/TestModule/0.0.1/PSModuleInfo.xml
190
- // a script path will look like:
205
+ // A script path will look like:
191
206
// ./Scripts/TestScript.ps1
192
- // note that the xml file is located in ./Scripts/InstalledScriptInfos, eg: ./Scripts/InstalledScriptInfos/TestScript_InstalledScriptInfo.xml
193
-
194
- string pkgName ;
207
+ // Note that the xml file is located in ./Scripts/InstalledScriptInfos, eg: ./Scripts/InstalledScriptInfos/TestScript_InstalledScriptInfo.xml
195
208
196
209
// Counter for tracking current dir out of total
197
210
int currentUninstalledDirCount = 0 ;
211
+ string pkgName ;
198
212
foreach ( string pkgPath in getHelper . FilterPkgPathsByVersion ( _versionRange , dirsToDelete , selectPrereleaseOnly : Prerelease ) )
199
213
{
200
214
currentUninstalledDirCount ++ ;
@@ -210,10 +224,18 @@ private bool UninstallPkgHelper()
210
224
if ( pkgPath . EndsWith ( PSScriptFileExt ) )
211
225
{
212
226
successfullyUninstalled = UninstallScriptHelper ( pkgPath , pkgName , out errRecord ) ;
227
+ if ( errRecord != null )
228
+ {
229
+ errRecords . Add ( errRecord ) ;
230
+ }
213
231
}
214
232
else
215
233
{
216
234
successfullyUninstalled = UninstallModuleHelper ( pkgPath , pkgName , out errRecord ) ;
235
+ if ( errRecord != null )
236
+ {
237
+ errRecords . Add ( errRecord ) ;
238
+ }
217
239
}
218
240
219
241
// if we can't find the resource, write non-terminating error and return
@@ -222,17 +244,19 @@ private bool UninstallPkgHelper()
222
244
if ( errRecord == null )
223
245
{
224
246
string message = Version == null || Version . Trim ( ) . Equals ( "*" ) ?
225
- string . Format ( "Could not find any version of the resource '{0}' in any path" , pkgName ) :
226
- string . Format ( "Could not find verison '{0}' of the resource '{1}' in any path" , Version , pkgName ) ;
247
+ string . Format ( "Cannot uninstall resource '{0}' because it does not exist." , pkgName ) :
248
+
249
+ string . Format ( "Cannot uninstall version '{0}' of resource '{1}' because it does not exist." , Version , pkgName ) ;
227
250
228
- errRecord = new ErrorRecord (
251
+
252
+ errRecords . Add ( new ErrorRecord (
229
253
new PSInvalidOperationException ( message ) ,
230
254
"ErrorRetrievingSpecifiedResource" ,
231
255
ErrorCategory . ObjectNotFound ,
232
- this ) ;
256
+ this ) ) ;
233
257
}
234
258
235
- WriteError ( errRecord ) ;
259
+ return successfullyUninstalled ;
236
260
}
237
261
}
238
262
@@ -266,7 +290,7 @@ private bool UninstallModuleHelper(string pkgPath, string pkgName, out ErrorReco
266
290
try
267
291
{
268
292
Utils . DeleteDirectory ( pkgPath ) ;
269
- WriteVerbose ( string . Format ( "Successfully uninstalled '{0}' from path '{1}'" , pkgName , dir . FullName ) ) ;
293
+ WriteVerbose ( string . Format ( "Successfully uninstalled '{0}' from path '{1}'. " , pkgName , dir . FullName ) ) ;
270
294
271
295
successfullyUninstalledPkg = true ;
272
296
@@ -280,20 +304,20 @@ private bool UninstallModuleHelper(string pkgPath, string pkgName, out ErrorReco
280
304
}
281
305
catch ( Exception e )
282
306
{
283
- // write error
284
- var exMessage = String . Format ( "Parent directory '{0 }' could not be deleted: {1}" , dir . Parent . FullName , e . Message ) ;
285
- var ex = new ArgumentException ( exMessage ) ;
286
- var ErrorDeletingParentDirectory = new ErrorRecord ( ex , "ErrorDeletingParentDirectory" , ErrorCategory . InvalidArgument , null ) ;
287
- errRecord = ErrorDeletingParentDirectory ;
307
+ errRecord = new ErrorRecord (
308
+ new ArgumentException ( $ "Parent directory '{ dir . Parent . FullName } ' could not be deleted: { e . Message } " ) ,
309
+ "ErrorDeletingParentDirectory" ,
310
+ ErrorCategory . InvalidArgument ,
311
+ this ) ;
288
312
}
289
313
}
290
314
catch ( Exception err )
291
315
{
292
- // write error
293
- var exMessage = String . Format ( "Directory '{0 }' could not be deleted: {1}" , dir . FullName , err . Message ) ;
294
- var ex = new ArgumentException ( exMessage ) ;
295
- var ErrorDeletingDirectory = new ErrorRecord ( ex , "ErrorDeletingDirectory" , ErrorCategory . PermissionDenied , null ) ;
296
- errRecord = ErrorDeletingDirectory ;
316
+ errRecord = new ErrorRecord (
317
+ new ArgumentException ( $ "Parent directory ' { dir . FullName } ' could not be deleted: { err . Message } " ) ,
318
+ "ErrorDeletingDirectory" ,
319
+ ErrorCategory . PermissionDenied ,
320
+ null ) ;
297
321
}
298
322
299
323
return successfullyUninstalledPkg ;
@@ -331,18 +355,20 @@ private bool UninstallScriptHelper(string pkgPath, string pkgName, out ErrorReco
331
355
}
332
356
catch ( Exception e )
333
357
{
334
- var exMessage = String . Format ( "Script metadata file '{0}' could not be deleted: {1}" , scriptXML , e . Message ) ;
335
- var ex = new ArgumentException ( exMessage ) ;
336
- var ErrorDeletingScriptMetadataFile = new ErrorRecord ( ex , "ErrorDeletingScriptMetadataFile" , ErrorCategory . PermissionDenied , null ) ;
337
- errRecord = ErrorDeletingScriptMetadataFile ;
358
+ errRecord = new ErrorRecord (
359
+ new ArgumentException ( $ "Script metadata file '{ scriptXML } ' could not be deleted: { e . Message } ") ,
360
+ "ErrorDeletingScriptMetadataFile" ,
361
+ ErrorCategory . PermissionDenied ,
362
+ this ) ;
338
363
}
339
364
}
340
365
catch ( Exception err )
341
366
{
342
- var exMessage = String . Format ( "Script '{0}' could not be deleted: {1}" , pkgPath , err . Message ) ;
343
- var ex = new ArgumentException ( exMessage ) ;
344
- var ErrorDeletingScript = new ErrorRecord ( ex , "ErrorDeletingScript" , ErrorCategory . PermissionDenied , null ) ;
345
- errRecord = ErrorDeletingScript ;
367
+ errRecord = new ErrorRecord (
368
+ new ArgumentException ( $ "Script '{ pkgPath } ' could not be deleted: { err . Message } ") ,
369
+ "ErrorDeletingScript" ,
370
+ ErrorCategory . PermissionDenied ,
371
+ this ) ;
346
372
}
347
373
348
374
return successfullyUninstalledPkg ;
@@ -388,8 +414,7 @@ private bool CheckIfDependency(string pkgName, out ErrorRecord errorRecord)
388
414
catch ( Exception e )
389
415
{
390
416
errorRecord = new ErrorRecord (
391
- new PSInvalidOperationException (
392
- $ "Error checking if resource is a dependency: { e . Message } .") ,
417
+ new PSInvalidOperationException ( $ "Error checking if resource is a dependency: { e . Message } .") ,
393
418
"UninstallPSResourceDependencyCheckError" ,
394
419
ErrorCategory . InvalidOperation ,
395
420
null ) ;
@@ -401,7 +426,8 @@ private bool CheckIfDependency(string pkgName, out ErrorRecord errorRecord)
401
426
402
427
errorRecord = new ErrorRecord (
403
428
new PSInvalidOperationException (
404
- $ "Cannot uninstall '{ pkgName } '. The following package(s) take a dependency on this package: { strUniquePkgNames } . If you would still like to uninstall, rerun the command with -SkipDependencyCheck") ,
429
+ $ "Cannot uninstall '{ pkgName } '. This package depends on the following package(s): '{ strUniquePkgNames } '. If you would still like to uninstall, re-run the command with -SkipDependencyCheck.") ,
430
+
405
431
"UninstallPSResourcePackageIsaDependency" ,
406
432
ErrorCategory . InvalidOperation ,
407
433
null ) ;
0 commit comments