diff --git a/src/Commands/Admin/NewSite.cs b/src/Commands/Admin/NewSite.cs index ac4627ad8..3b9e852b1 100644 --- a/src/Commands/Admin/NewSite.cs +++ b/src/Commands/Admin/NewSite.cs @@ -1,13 +1,13 @@ -using System; -using System.Linq; -using System.Management.Automation; -using Microsoft.SharePoint.Client; -using PnP.PowerShell.Commands.Enums; -using PnP.PowerShell.Commands.Attributes; +using Microsoft.SharePoint.Client; +using PnP.Core.Admin.Model.Microsoft365; using PnP.Core.Admin.Model.SharePoint; using PnP.Core.Services; +using PnP.PowerShell.Commands.Attributes; +using PnP.PowerShell.Commands.Enums; using PnP.PowerShell.Commands.Utilities; -using PnP.Core.Admin.Model.Microsoft365; +using System; +using System.Linq; +using System.Management.Automation; using System.Text.Json; namespace PnP.PowerShell.Commands @@ -33,7 +33,7 @@ public class NewSite : PnPSharePointCmdlet, IDynamicParameters [Parameter(Mandatory = false)] public SwitchParameter Wait; - + [Parameter(Mandatory = false)] public Framework.Enums.TimeZone TimeZone; @@ -167,30 +167,23 @@ protected override void ExecuteCmdlet() if (ClientContext.GetContextSettings()?.Type != Framework.Utilities.Context.ClientContextType.SharePointACSAppOnly) { - try - { - var rc = Connection.PnPContext.GetSiteCollectionManager().CreateSiteCollection(teamSiteOptions, siteCreationOptions, vanityUrlOptions); + var rc = Connection.PnPContext.GetSiteCollectionManager().CreateSiteCollection(teamSiteOptions, siteCreationOptions, vanityUrlOptions); - if (ParameterSpecified(nameof(TimeZone))) - { - using (var newSiteContext = ClientContext.Clone(rc.Uri.AbsoluteUri)) - { - newSiteContext.Web.EnsureProperties(w => w.RegionalSettings, w => w.RegionalSettings.TimeZones); - newSiteContext.Web.RegionalSettings.TimeZone = newSiteContext.Web.RegionalSettings.TimeZones.Where(t => t.Id == ((int)TimeZone)).First(); - newSiteContext.Web.RegionalSettings.Update(); - newSiteContext.ExecuteQueryRetry(); - newSiteContext.Site.EnsureProperty(s => s.Url); - WriteObject(rc.Uri.AbsoluteUri); - } - } - else + if (ParameterSpecified(nameof(TimeZone))) + { + using (var newSiteContext = ClientContext.Clone(rc.Uri.AbsoluteUri)) { + newSiteContext.Web.EnsureProperties(w => w.RegionalSettings, w => w.RegionalSettings.TimeZones); + newSiteContext.Web.RegionalSettings.TimeZone = newSiteContext.Web.RegionalSettings.TimeZones.Where(t => t.Id == ((int)TimeZone)).First(); + newSiteContext.Web.RegionalSettings.Update(); + newSiteContext.ExecuteQueryRetry(); + newSiteContext.Site.EnsureProperty(s => s.Url); WriteObject(rc.Uri.AbsoluteUri); } } - catch (Exception ex) + else { - LogError(ex); + WriteObject(rc.Uri.AbsoluteUri); } } else diff --git a/src/Commands/Base/PnPConnectedCmdlet.cs b/src/Commands/Base/PnPConnectedCmdlet.cs index 6e32f1b53..9ba1a9053 100644 --- a/src/Commands/Base/PnPConnectedCmdlet.cs +++ b/src/Commands/Base/PnPConnectedCmdlet.cs @@ -90,7 +90,8 @@ protected override void ProcessRecord() } break; case Core.MicrosoftGraphServiceException pgex: - errorMessage = (pgex.Error as Core.MicrosoftGraphError).Message; + var pgexError = pgex.Error as Core.MicrosoftGraphError; + errorMessage = $"{pgex.Message} - {pgexError.Code} {pgexError.HttpResponseCode} {pgexError.Message}"; break; default: errorMessage = ex.Message; @@ -116,7 +117,7 @@ protected override void ProcessRecord() LogError(errorMessage); } - + } } }