protected override void ProcessRecord()
        {
            if (ParameterSetName != ParameterSet1Name &&
                ParameterSetName != ParameterSet2Name)
            {
                throw new ValidationMetadataException("Please input web app and certificate.");
            }

            if (ParameterSetName == ParameterSet2Name)
            {
                CmdletHelpers.ExtractWebAppPropertiesFromWebApp(WebApp, out resourceGroupName, out webAppName, out slot);
            }
            else
            {
                resourceGroupName = ResourceGroupName;
                webAppName        = WebAppName;
                slot = Slot;
            }
        }
Beispiel #2
0
        protected override void ProcessRecord()
        {
            if (ParameterSetName != ParameterSet1Name &&
                ParameterSetName != ParameterSet2Name &&
                ParameterSetName != ParameterSet3Name &&
                ParameterSetName != ParameterSet4Name)
            {
                throw new ValidationMetadataException("Please input web app and certificate.");
            }

            if (ParameterSetName == ParameterSet3Name ||
                ParameterSetName == ParameterSet4Name)
            {
                CmdletHelpers.ExtractWebAppPropertiesFromWebApp(WebApp, out resourceGroupName, out webAppName, out slot);
            }
            else
            {
                resourceGroupName = ResourceGroupName;
                webAppName        = WebAppName;
                slot = Slot;
            }

            string thumbPrint = null;
            var    webapp     = WebsitesClient.GetWebApp(resourceGroupName, webAppName, slot);

            switch (ParameterSetName)
            {
            case ParameterSet1Name:
            case ParameterSet3Name:
                var certificateBytes   = File.ReadAllBytes(CertificateFilePath);
                var certificateDetails = new X509Certificate2(certificateBytes, CertificatePassword);

                var certificateName = GenerateCertName(certificateDetails.Thumbprint, webapp.HostingEnvironmentProfile != null ? webapp.HostingEnvironmentProfile.Name : null, webapp.Location, resourceGroupName);
                var certificate     = new Certificate
                {
                    PfxBlob  = Convert.ToBase64String(certificateBytes),
                    Password = CertificatePassword,
                    Location = webapp.Location
                };

                if (webapp.HostingEnvironmentProfile != null)
                {
                    certificate.HostingEnvironmentProfile = webapp.HostingEnvironmentProfile;
                }

                var certificateResourceGroup = CmdletHelpers.GetResourceGroupFromResourceId(webapp.ServerFarmId);
                try
                {
                    WebsitesClient.CreateCertificate(certificateResourceGroup, certificateName, certificate);
                }
                catch (CloudException e)
                {
                    // This exception is thrown when certificate already exists. Let's swallow it and continue.
                    if (e.Response.StatusCode != HttpStatusCode.Conflict)
                    {
                        throw;
                    }
                }

                thumbPrint = certificateDetails.Thumbprint;
                break;

            case ParameterSet2Name:
            case ParameterSet4Name:
                thumbPrint = Thumbprint;
                break;
            }

            WriteObject(CmdletHelpers.GetHostNameSslStatesFromSiteResponse(
                            WebsitesClient.UpdateHostNameSslState(
                                resourceGroupName,
                                webAppName,
                                slot,
                                webapp.Location,
                                Name,
                                SslState.HasValue ? SslState.Value : Management.WebSites.Models.SslState.SniEnabled,
                                thumbPrint),
                            Name));
        }