protected override void ProcessRecord()

in src/PFXImportPowershell/PFXImportPS/cmdlets/ImportUserPFXCertificate.cs [115:181]


        protected override void ProcessRecord()
        {
            Hashtable modulePrivateData = this.MyInvocation.MyCommand.Module.PrivateData as Hashtable;
            if (AuthenticationResult == null)
            {
                AuthenticationResult = Authenticate.GetAuthToken(modulePrivateData);
            }
            if (!Authenticate.AuthTokenIsValid(AuthenticationResult))
            {
                this.ThrowTerminatingError(
                    new ErrorRecord(
                        new AuthenticationException("Cannot get Authentication Token"),
                        "Authentication Failure",
                        ErrorCategory.AuthenticationError,
                        AuthenticationResult));
            }

            successCnt = 0;
            failureCnt = 0;

            string graphURI = Authenticate.GetGraphURI(modulePrivateData);
            string schemaVersion = Authenticate.GetSchemaVersion(modulePrivateData);

            if (CertificateList == null || CertificateList.Count == 0)
            {
                this.ThrowTerminatingError(
                    new ErrorRecord(
                        new ArgumentException("No Certificates specified"),
                        "Date Input Failure",
                        ErrorCategory.InvalidArgument,
                        AuthenticationResult));
            }

            foreach (UserPFXCertificate cert in CertificateList)
            {
                string url;
                if (IsUpdate.IsPresent)
                {
                    string userId = GetUserId.GetUserIdFromUpn(cert.UserPrincipalName, graphURI, schemaVersion, AuthenticationResult);
                    url = string.Format(CultureInfo.InvariantCulture, "{0}/{1}/deviceManagement/userPfxCertificates({2}-{3})", graphURI, schemaVersion, userId, cert.Thumbprint);
                }
                else
                {
                    url = string.Format(CultureInfo.InvariantCulture, "{0}/{1}/deviceManagement/userPfxCertificates", graphURI, schemaVersion);
                }

                HttpWebRequest request = CreateWebRequest(url, AuthenticationResult);

                string certJson = SerializationHelpers.SerializeUserPFXCertificate(cert);
                byte[] contentBytes = Encoding.UTF8.GetBytes(certJson);

                request.ContentLength = contentBytes.Length;

                using (Stream reqStream = request.GetRequestStream())
                {
                    reqStream.Write(contentBytes, 0, contentBytes.Length);
                }

                ProcessResponse(request, cert);
            }

            this.WriteCommandDetail(string.Format(LogMessages.ImportCertificatesSuccess, successCnt));
            if (failureCnt > 0)
            {
                this.WriteWarning(string.Format(LogMessages.ImportCertificatesFailure, failureCnt));
            }
        }