@@ -245,6 +245,7 @@ private enum ResourceFileType
245
245
private const string InputObjectParameterSet = "InputObjectParameterSet" ;
246
246
private const string RequiredResourceFileParameterSet = "RequiredResourceFileParameterSet" ;
247
247
private const string RequiredResourceParameterSet = "RequiredResourceParameterSet" ;
248
+ private const string CredentialKey = "credential" ;
248
249
List < string > _pathsToInstallPkg ;
249
250
private string _requiredResourceFile ;
250
251
private string _requiredResourceJson ;
@@ -426,50 +427,76 @@ protected override void ProcessRecord()
426
427
private void RequiredResourceHelper ( Hashtable reqResourceHash )
427
428
{
428
429
WriteDebug ( "In InstallPSResource::RequiredResourceHelper()" ) ;
429
- var pkgNames = reqResourceHash . Keys ;
430
-
431
- foreach ( string pkgName in pkgNames )
430
+ foreach ( DictionaryEntry entry in reqResourceHash )
432
431
{
433
- var pkgParamInfo = reqResourceHash [ pkgName ] ;
432
+ InstallPkgParams pkgParams = new InstallPkgParams ( ) ;
433
+ PSCredential pkgCredential = Credential ;
434
434
435
- // Format should now be a hashtable, whether the original input format was json or hashtable
436
- if ( ! ( pkgParamInfo is Hashtable pkgInstallInfo ) )
435
+ // The package name will be the key for the inner hashtable and is present for all scenarios,
436
+ // including the scenario where only package name is specified
437
+ // i.e Install-PSResource -RequiredResource @ { MyPackage = @{} }
438
+ string pkgName = entry . Key . ToString ( ) ;
439
+ // check if pkgName is empty or whitespace
440
+ if ( String . IsNullOrEmpty ( pkgName . Trim ( ) ) )
437
441
{
442
+ var pkgNameEmptyOrWhitespaceError = new ErrorRecord (
443
+ new ArgumentException ( $ "The package name '{ pkgName } ' provided cannot be an empty string or whitespace.") ,
444
+ "pkgNameEmptyOrWhitespaceError" ,
445
+ ErrorCategory . InvalidArgument ,
446
+ this ) ;
447
+
448
+ WriteError ( pkgNameEmptyOrWhitespaceError ) ;
438
449
return ;
439
450
}
440
451
441
- InstallPkgParams pkgParams = new InstallPkgParams ( ) ;
442
- var pkgParamNames = pkgInstallInfo . Keys ;
452
+ string pkgVersion = String . Empty ;
453
+ if ( ! ( entry . Value is Hashtable pkgInstallInfo ) )
454
+ {
455
+ var requiredResourceHashtableInputFormatError = new ErrorRecord (
456
+ new ArgumentException ( $ "The RequiredResource input with name '{ pkgName } ' does not have a valid value, the value must be a hashtable.") ,
457
+ "RequiredResourceHashtableInputFormatError" ,
458
+ ErrorCategory . InvalidArgument ,
459
+ this ) ;
443
460
444
- PSCredential pkgCredential = Credential ;
445
- foreach ( string paramName in pkgParamNames )
461
+ WriteError ( requiredResourceHashtableInputFormatError ) ;
462
+ return ;
463
+ }
464
+
465
+ // scenario where package name and other parameters are provided:
466
+ // Install-PSResource -RequiredResource @ { MyPackage = @{ version = '1.2.3', repository = 'PSGallery' } }
467
+ if ( pkgInstallInfo . Count != 0 )
446
468
{
447
- if ( string . Equals ( paramName , "credential" , StringComparison . InvariantCultureIgnoreCase ) )
448
- {
449
- WriteVerbose ( "Credential specified for required resource" ) ;
450
- pkgCredential = pkgInstallInfo [ paramName ] as PSCredential ;
451
- }
469
+ var pkgParamNames = pkgInstallInfo . Keys ;
452
470
453
- pkgParams . SetProperty ( paramName , pkgInstallInfo [ paramName ] as string , out ErrorRecord IncorrectVersionFormat ) ;
471
+ foreach ( string paramName in pkgParamNames )
472
+ {
473
+ if ( string . Equals ( paramName , CredentialKey , StringComparison . InvariantCultureIgnoreCase ) )
474
+ {
475
+ WriteVerbose ( "Credential specified for required resource" ) ;
476
+ pkgCredential = pkgInstallInfo [ paramName ] as PSCredential ;
477
+ }
454
478
455
- if ( IncorrectVersionFormat != null )
479
+ // for all parameters found, we try to add it to the InstallPkgParams object if it is valid.
480
+ pkgParams . SetProperty ( paramName , pkgInstallInfo [ paramName ] as string , out ErrorRecord ParameterParsingError ) ;
481
+ if ( ParameterParsingError != null )
482
+ {
483
+ ThrowTerminatingError ( ParameterParsingError ) ;
484
+ }
485
+ }
486
+
487
+ if ( pkgParams . Scope == ScopeType . AllUsers )
456
488
{
457
- ThrowTerminatingError ( IncorrectVersionFormat ) ;
489
+ _pathsToInstallPkg = Utils . GetAllInstallationPaths ( this , pkgParams . Scope ) ;
458
490
}
459
- }
460
-
461
- if ( pkgParams . Scope == ScopeType . AllUsers )
462
- {
463
- _pathsToInstallPkg = Utils . GetAllInstallationPaths ( this , pkgParams . Scope ) ;
464
- }
465
491
466
- string pkgVersion = pkgInstallInfo [ "version" ] == null ? String . Empty : pkgInstallInfo [ "version" ] . ToString ( ) ;
492
+ pkgVersion = pkgInstallInfo [ "version" ] == null ? String . Empty : pkgInstallInfo [ "version" ] . ToString ( ) ;
493
+ }
467
494
468
495
ProcessInstallHelper (
469
496
pkgNames : new string [ ] { pkgName } ,
470
497
pkgVersion : pkgVersion ,
471
498
pkgPrerelease : pkgParams . Prerelease ,
472
- pkgRepository : new string [ ] { pkgParams . Repository } ,
499
+ pkgRepository : pkgParams . Repository != null ? new string [ ] { pkgParams . Repository } : new string [ ] { } ,
473
500
pkgCredential : pkgCredential ,
474
501
reqResourceParams : pkgParams ) ;
475
502
}
0 commit comments