diff --git a/src/Commands/Admin/AddTenantRestrictedSearchAllowedList.cs b/src/Commands/Admin/AddTenantRestrictedSearchAllowedList.cs index 036dacc89..7b68a8a56 100644 --- a/src/Commands/Admin/AddTenantRestrictedSearchAllowedList.cs +++ b/src/Commands/Admin/AddTenantRestrictedSearchAllowedList.cs @@ -36,7 +36,7 @@ protected override void ExecuteCmdlet() } else { - throw new ArgumentException("Parameter set cannot be resolved using the specified named parameters."); + throw new InvalidOperationException("Parameter set cannot be resolved using the specified named parameters."); } if (_sitelist == null) diff --git a/src/Commands/Admin/GetContainerTypeConfiguration.cs b/src/Commands/Admin/GetContainerTypeConfiguration.cs index 27c091954..991d49640 100644 --- a/src/Commands/Admin/GetContainerTypeConfiguration.cs +++ b/src/Commands/Admin/GetContainerTypeConfiguration.cs @@ -16,7 +16,7 @@ protected override void ExecuteCmdlet() { if (Identity == Guid.Empty) { - throw new ArgumentException($"Identity {Identity} value is invalid"); + throw new PSArgumentException($"Identity cannot be an empty GUID. Please provide a valid non-empty GUID."); } ClientResult sPOContainerTypeConfigurationByContainerTypeId = Tenant.GetSPOContainerTypeConfigurationByContainerTypeId(Identity); AdminContext.ExecuteQueryRetry(); diff --git a/src/Commands/Admin/GetStorageEntity.cs b/src/Commands/Admin/GetStorageEntity.cs index dad59c44d..464aaa301 100644 --- a/src/Commands/Admin/GetStorageEntity.cs +++ b/src/Commands/Admin/GetStorageEntity.cs @@ -90,7 +90,7 @@ protected override void ExecuteCmdlet() { var storageEntitiesDict = JsonSerializer.Deserialize>>(storageEntitiesIndex); - var storageEntities = new List(); + var storageEntities = new List(storageEntitiesDict.Count); foreach (var key in storageEntitiesDict.Keys) { var storageEntity = new StorageEntity diff --git a/src/Commands/Admin/GetTenantDeletedSite.cs b/src/Commands/Admin/GetTenantDeletedSite.cs index 987d97a5d..b8dc0e72e 100644 --- a/src/Commands/Admin/GetTenantDeletedSite.cs +++ b/src/Commands/Admin/GetTenantDeletedSite.cs @@ -37,7 +37,7 @@ protected override void ExecuteCmdlet() bool flag = Identity != null && !string.IsNullOrEmpty(Identity.Url) && UrlUtilities.IsPersonalSiteUrl(Identity.Url); if (Identity == null || string.IsNullOrEmpty(Identity.Url) || flag) { - List list = new List(); + List list = []; uint siteRowLimit = Limit; bool flag2 = siteRowLimit == 0; bool flag3 = false; @@ -95,7 +95,7 @@ private bool RequestDeletedSitePropertiesFromSharePoint(Func sp.NextStartIndexFromSharePoint); @@ -146,7 +146,7 @@ private bool RequestDeletedSiteProperties(Func timeoutFunction = TimeoutFunction; diff --git a/src/Commands/Admin/RemoveTenantSyncClientRestriction.cs b/src/Commands/Admin/RemoveTenantSyncClientRestriction.cs index 778155b48..0965ac4af 100644 --- a/src/Commands/Admin/RemoveTenantSyncClientRestriction.cs +++ b/src/Commands/Admin/RemoveTenantSyncClientRestriction.cs @@ -13,7 +13,7 @@ protected override void ExecuteCmdlet() { Tenant.IsUnmanagedSyncClientForTenantRestricted = false; Tenant.BlockMacSync = false; - Tenant.ExcludedFileExtensionsForSyncClient = new List(); + Tenant.ExcludedFileExtensionsForSyncClient = []; Tenant.OptOutOfGrooveBlock = false; Tenant.OptOutOfGrooveSoftBlock = false; Tenant.DisableReportProblemDialog = false; diff --git a/src/Commands/Admin/SetTenant.cs b/src/Commands/Admin/SetTenant.cs index 3d30ec924..3f76c4ea3 100644 --- a/src/Commands/Admin/SetTenant.cs +++ b/src/Commands/Admin/SetTenant.cs @@ -1690,7 +1690,7 @@ protected override void ExecuteCmdlet() else { // Build a GUID array by resolving each provided site URL to its SiteProperties and extracting the Id - var siteIdList = new List(); + var siteIdList = new List(KnowledgeAgentSelectedSitesList.Length); var tenantForLookup = new Tenant(AdminContext); foreach (var siteUrl in KnowledgeAgentSelectedSitesList) diff --git a/src/Commands/Admin/SetTenantSyncClientRestriction.cs b/src/Commands/Admin/SetTenantSyncClientRestriction.cs index cd01ae22e..897662a69 100644 --- a/src/Commands/Admin/SetTenantSyncClientRestriction.cs +++ b/src/Commands/Admin/SetTenantSyncClientRestriction.cs @@ -68,7 +68,7 @@ protected override void ExecuteCmdlet() break; default: - throw new PSArgumentException(string.Format(Resources.GrooveBlockOptionNotSupported, nameof(GrooveBlockOption), GrooveBlockOption), nameof(GrooveBlockOption)); + throw new PSArgumentException($"Parameter '{nameof(GrooveBlockOption)}' does not support value '{GrooveBlockOption}'. Supported values are: OptOut, HardOptin, SoftOptin", nameof(GrooveBlockOption)); } } AdminContext.ExecuteQueryRetry(); diff --git a/src/Commands/Apps/GetApplicationCustomizer.cs b/src/Commands/Apps/GetApplicationCustomizer.cs index 14a893eb1..d361b0a7d 100644 --- a/src/Commands/Apps/GetApplicationCustomizer.cs +++ b/src/Commands/Apps/GetApplicationCustomizer.cs @@ -27,7 +27,7 @@ public class GetApplicationCustomizer : PnPWebRetrievalsCmdlet protected override void ExecuteCmdlet() { - List actions = new List(); + List actions = []; if (Scope == CustomActionScope.All || Scope == CustomActionScope.Web) { diff --git a/src/Commands/Apps/GetAzureADAppSitePermission.cs b/src/Commands/Apps/GetAzureADAppSitePermission.cs index 1a982ee36..38c47c092 100644 --- a/src/Commands/Apps/GetAzureADAppSitePermission.cs +++ b/src/Commands/Apps/GetAzureADAppSitePermission.cs @@ -54,7 +54,7 @@ protected override void ExecuteCmdlet() var permissions = this.GraphRequestHelper.GetResultCollection($"v1.0/sites/{siteId}/permissions?$select=Id"); if (permissions.Any()) { - var results = new List(); + var results = new List(permissions.Count()); foreach (var permission in permissions) { // Request the permission individually so it will include the roles diff --git a/src/Commands/AzureAD/NewAzureADGroup.cs b/src/Commands/AzureAD/NewAzureADGroup.cs index 660f46400..51737bf19 100644 --- a/src/Commands/AzureAD/NewAzureADGroup.cs +++ b/src/Commands/AzureAD/NewAzureADGroup.cs @@ -52,7 +52,7 @@ protected override void ExecuteCmdlet() { var existingGroup = AzureADGroupsUtility.GetGroup(GraphRequestHelper, MailNickname); - forceCreation = existingGroup == null || ShouldContinue(string.Format(Resources.ForceCreationOfExistingGroup0, MailNickname), Resources.Confirm); + forceCreation = existingGroup == null || ShouldContinue($"The Unified Group with MailNickname of {MailNickname} already exists. Do you want to create another one?", Resources.Confirm); } else { diff --git a/src/Commands/AzureAD/RegisterAzureADApp.cs b/src/Commands/AzureAD/RegisterAzureADApp.cs index a33d2cf40..5660acb62 100644 --- a/src/Commands/AzureAD/RegisterAzureADApp.cs +++ b/src/Commands/AzureAD/RegisterAzureADApp.cs @@ -460,7 +460,7 @@ private X509Certificate2 GetCertificate(PSObject record) // Ensure a file exists at the provided CertificatePath if (!File.Exists(CertificatePath)) { - throw new PSArgumentException(string.Format(Resources.CertificateNotFoundAtPath, CertificatePath), nameof(CertificatePath)); + throw new PSArgumentException($"Certificate not found at path: {CertificatePath}", nameof(CertificatePath)); } try @@ -469,7 +469,7 @@ private X509Certificate2 GetCertificate(PSObject record) } catch (CryptographicException e) when (e.Message.Contains("The specified password is not correct")) { - throw new PSArgumentNullException(nameof(CertificatePassword), string.Format(Resources.PrivateKeyCertificateImportFailedPasswordIncorrect, nameof(CertificatePassword))); + throw new PSArgumentNullException(nameof(CertificatePassword), $"Failed to import private key certificate. Ensure the correct password is provided for parameter: {nameof(CertificatePassword)}"); } // Ensure the certificate at the provided CertificatePath holds a private key diff --git a/src/Commands/SiteDesigns/GetBuiltInSiteTemplateSettings.cs b/src/Commands/SiteDesigns/GetBuiltInSiteTemplateSettings.cs index 7562aa0cf..a7f9e5c94 100644 --- a/src/Commands/SiteDesigns/GetBuiltInSiteTemplateSettings.cs +++ b/src/Commands/SiteDesigns/GetBuiltInSiteTemplateSettings.cs @@ -4,6 +4,7 @@ using PnP.PowerShell.Commands.Base.PipeBinds; using PnP.PowerShell.Commands.Enums; using PnP.PowerShell.Commands.Model.SharePoint; +using System; using System.Linq; using System.Management.Automation; @@ -28,7 +29,8 @@ protected override void ExecuteCmdlet() ClientResult templateSetting = null; if (ParameterSpecified(nameof(Identity))) { - if (Identity == null || !Identity.Id.HasValue) throw new PSArgumentException($"Identity contains an invalid {nameof(BuiltInSiteTemplateSettingsPipeBind)} value", nameof(Identity)); + ArgumentNullException.ThrowIfNull(Identity, nameof(Identity)); + if (!Identity.Id.HasValue) throw new PSArgumentException($"Identity contains an invalid {nameof(BuiltInSiteTemplateSettingsPipeBind)} value", nameof(Identity)); templateSetting = Tenant.GetOutOfBoxSiteTemplateSettings(AdminContext, Identity.Id.Value); } diff --git a/src/Commands/SiteDesigns/SetBuiltInSiteTemplateSettings.cs b/src/Commands/SiteDesigns/SetBuiltInSiteTemplateSettings.cs index 8006bee96..78dfa7242 100644 --- a/src/Commands/SiteDesigns/SetBuiltInSiteTemplateSettings.cs +++ b/src/Commands/SiteDesigns/SetBuiltInSiteTemplateSettings.cs @@ -4,6 +4,7 @@ using PnP.PowerShell.Commands.Base.PipeBinds; using PnP.PowerShell.Commands.Enums; using PnP.PowerShell.Commands.Model.SharePoint; +using System; using System.Linq; using System.Management.Automation; @@ -36,7 +37,8 @@ protected override void ExecuteCmdlet() ClientResult templateSetting = null; if (ParameterSpecified(nameof(Identity))) { - if (Identity == null || !Identity.Id.HasValue) throw new PSArgumentException($"Identity contains an invalid {nameof(BuiltInSiteTemplateSettingsPipeBind)} value", nameof(Identity)); + ArgumentNullException.ThrowIfNull(Identity, nameof(Identity)); + if (!Identity.Id.HasValue) throw new PSArgumentException($"Identity contains an invalid {nameof(BuiltInSiteTemplateSettingsPipeBind)} value", nameof(Identity)); if (!ParameterSpecified(nameof(WhatIf))) { diff --git a/src/Commands/Utilities/DynamicLinq.cs b/src/Commands/Utilities/DynamicLinq.cs index 7a2b2a112..d6ae569ba 100644 --- a/src/Commands/Utilities/DynamicLinq.cs +++ b/src/Commands/Utilities/DynamicLinq.cs @@ -20,8 +20,8 @@ public static IQueryable Where(this IQueryable source, string predicate public static IQueryable Where(this IQueryable source, string predicate, params object[] values) { - if (source == null) throw new ArgumentNullException(nameof(source)); - if (predicate == null) throw new ArgumentNullException(nameof(predicate)); + ArgumentNullException.ThrowIfNull(source); + ArgumentNullException.ThrowIfNull(predicate); LambdaExpression lambda = DynamicExpression.ParseLambda(source.ElementType, typeof(bool), predicate, values); return source.Provider.CreateQuery( Expression.Call( @@ -32,8 +32,8 @@ public static IQueryable Where(this IQueryable source, string predicate, params public static IQueryable Select(this IQueryable source, string selector, params object[] values) { - if (source == null) throw new ArgumentNullException(nameof(source)); - if (selector == null) throw new ArgumentNullException(nameof(selector)); + ArgumentNullException.ThrowIfNull(source); + ArgumentNullException.ThrowIfNull(selector); LambdaExpression lambda = DynamicExpression.ParseLambda(source.ElementType, null, selector, values); return source.Provider.CreateQuery( Expression.Call( @@ -49,8 +49,8 @@ public static IQueryable OrderBy(this IQueryable source, string orderin public static IQueryable OrderBy(this IQueryable source, string ordering, params object[] values) { - if (source == null) throw new ArgumentNullException(nameof(source)); - if (ordering == null) throw new ArgumentNullException(nameof(ordering)); + ArgumentNullException.ThrowIfNull(source); + ArgumentNullException.ThrowIfNull(ordering); ParameterExpression[] parameters = new ParameterExpression[] { Expression.Parameter(source.ElementType, "") }; ExpressionParser parser = new ExpressionParser(parameters, ordering, values); @@ -72,7 +72,7 @@ public static IQueryable OrderBy(this IQueryable source, string ordering, params public static IQueryable Take(this IQueryable source, int count) { - if (source == null) throw new ArgumentNullException(nameof(source)); + ArgumentNullException.ThrowIfNull(source); return source.Provider.CreateQuery( Expression.Call( typeof(Queryable), "Take", @@ -82,7 +82,7 @@ public static IQueryable Take(this IQueryable source, int count) public static IQueryable Skip(this IQueryable source, int count) { - if (source == null) throw new ArgumentNullException(nameof(source)); + ArgumentNullException.ThrowIfNull(source); return source.Provider.CreateQuery( Expression.Call( typeof(Queryable), "Skip", @@ -92,9 +92,9 @@ public static IQueryable Skip(this IQueryable source, int count) public static IQueryable GroupBy(this IQueryable source, string keySelector, string elementSelector, params object[] values) { - if (source == null) throw new ArgumentNullException(nameof(source)); - if (keySelector == null) throw new ArgumentNullException(nameof(keySelector)); - if (elementSelector == null) throw new ArgumentNullException(nameof(elementSelector)); + ArgumentNullException.ThrowIfNull(source); + ArgumentNullException.ThrowIfNull(keySelector); + ArgumentNullException.ThrowIfNull(elementSelector); LambdaExpression keyLambda = DynamicExpression.ParseLambda(source.ElementType, null, keySelector, values); LambdaExpression elementLambda = DynamicExpression.ParseLambda(source.ElementType, null, elementSelector, values); return source.Provider.CreateQuery( @@ -106,7 +106,7 @@ public static IQueryable GroupBy(this IQueryable source, string keySelector, str public static bool Any(this IQueryable source) { - if (source == null) throw new ArgumentNullException(nameof(source)); + ArgumentNullException.ThrowIfNull(source); return (bool)source.Provider.Execute( Expression.Call( typeof(Queryable), "Any", @@ -115,7 +115,7 @@ public static bool Any(this IQueryable source) public static int Count(this IQueryable source) { - if (source == null) throw new ArgumentNullException(nameof(source)); + ArgumentNullException.ThrowIfNull(source); return (int)source.Provider.Execute( Expression.Call( typeof(Queryable), "Count", @@ -149,8 +149,8 @@ public class DynamicProperty public DynamicProperty(string name, Type type) { - if (name == null) throw new ArgumentNullException(nameof(name)); - if (type == null) throw new ArgumentNullException(nameof(type)); + ArgumentNullException.ThrowIfNull(name); + ArgumentNullException.ThrowIfNull(type); this.name = name; this.type = type; } @@ -636,7 +636,7 @@ interface IEnumerableSignatures public ExpressionParser(ParameterExpression[] parameters, string expression, object[] values) { - if (expression == null) throw new ArgumentNullException(nameof(expression)); + ArgumentNullException.ThrowIfNull(expression); if (keywords == null) keywords = CreateKeywords(); symbols = new Dictionary(StringComparer.OrdinalIgnoreCase); literals = new Dictionary(); diff --git a/src/Commands/Utilities/REST/RestHelper.cs b/src/Commands/Utilities/REST/RestHelper.cs index 431da791e..5f83a8eb1 100644 --- a/src/Commands/Utilities/REST/RestHelper.cs +++ b/src/Commands/Utilities/REST/RestHelper.cs @@ -62,7 +62,7 @@ public static string ExecuteGetRequest(ClientContext context, string endPointUrl StringBuilder sb = new StringBuilder(); foreach (var cookie in authManager.CookieContainer.GetCookies(new Uri(url))) { - sb.AppendFormat("{0}; ", cookie.ToString()); + sb.Append($"{cookie}; "); } client.DefaultRequestHeaders.Add("Cookie", sb.ToString()); } diff --git a/src/Tests/PnP.PowerShell.Tests.csproj b/src/Tests/PnP.PowerShell.Tests.csproj index a35882600..b77efda6f 100644 --- a/src/Tests/PnP.PowerShell.Tests.csproj +++ b/src/Tests/PnP.PowerShell.Tests.csproj @@ -1,7 +1,7 @@  - net6.0-windows + net8.0-windows false PnP.PowerShell.Tests PnP.PowerShell.Tests @@ -10,7 +10,7 @@ true PnP.PowerShell Debug;Release;DebugLocal - false + false