Beispiel #1
0
        /// <summary>
        /// Uses a graph call to return the UserId for a specified UPN
        /// </summary>
        /// <param name="user">The User Principal Name.</param>
        /// <returns>The Azuer UserId.</returns>
        public static string GetUserIdFromUpn(string user, string graphURI, string schemaVersion, AuthenticationResult authenticationResult)
        {
            string         url = string.Format(CultureInfo.InvariantCulture, "{0}/{1}/users?$filter=userPrincipalName eq '{2}'", graphURI, schemaVersion, user);
            HttpWebRequest request;

            request = GetUserPFXCertificate.CreateWebRequest(url, authenticationResult);

            using (var response = (HttpWebResponse)request.GetResponse())
            {
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    string responseMessage = string.Empty;
                    using (StreamReader rs = new StreamReader(response.GetResponseStream()))
                    {
                        responseMessage = rs.ReadToEnd();
                    }

                    User userObj = SerializationHelpers.DeserializeUser(responseMessage);
                    return(userObj.Id.Replace("-", string.Empty));
                }
                else
                {
                    throw new InvalidOperationException(response.StatusDescription);
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// ProcessRecord.
        /// </summary>
        protected override void ProcessRecord()
        {
            if (!Authenticate.AuthTokenIsValid(AuthenticationResult))
            {
                this.ThrowTerminatingError(
                    new ErrorRecord(
                        new AuthenticationException("Cannot get Authentication Token"),
                        "Authentication Failure",
                        ErrorCategory.AuthenticationError,
                        AuthenticationResult));
            }

            Hashtable modulePrivateData = this.MyInvocation.MyCommand.Module.PrivateData as Hashtable;
            string    graphURI          = Authenticate.GetGraphURI(modulePrivateData);
            string    schemaVersion     = Authenticate.GetSchemaVersion(modulePrivateData);

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

            if (UserThumbprintList == null)
            {
                UserThumbprintList = new List <UserThumbprint>();
            }

            if (UserList != null)
            {
                PowerShell ps = PowerShell.Create();
                ps.AddCommand("Import-Module").AddParameter("ModuleInfo", this.MyInvocation.MyCommand.Module);
                ps.Invoke();
                ps.Commands.Clear();

                ps.AddCommand("Get-IntuneUserPfxCertificate");
                ps.AddParameter("AuthenticationResult", AuthenticationResult);
                ps.AddParameter("UserList", UserList);

                foreach (PSObject result in ps.Invoke())
                {
                    UserPFXCertificate cert   = result.BaseObject as UserPFXCertificate;
                    string             userId = GetUserPFXCertificate.GetUserIdFromUpn(cert.UserPrincipalName, graphURI, schemaVersion, AuthenticationResult);
                    UserThumbprintList.Add(new UserThumbprint()
                    {
                        User = userId, Thumbprint = cert.Thumbprint
                    });
                }
            }

            if (CertificateList != null && CertificateList.Count > 0)
            {
                foreach (UserPFXCertificate cert in CertificateList)
                {
                    string userId = GetUserPFXCertificate.GetUserIdFromUpn(cert.UserPrincipalName, graphURI, schemaVersion, AuthenticationResult);
                    UserThumbprintList.Add(new UserThumbprint()
                    {
                        User = userId, Thumbprint = cert.Thumbprint
                    });
                }
            }

            successCnt = 0;
            failureCnt = 0;

            foreach (UserThumbprint userThumbprint in UserThumbprintList)
            {
                string         url = string.Format(CultureInfo.InvariantCulture, "{0}/{1}/deviceManagement/userPfxCertificates/{2}-{3}", graphURI, schemaVersion, userThumbprint.User, userThumbprint.Thumbprint);
                HttpWebRequest request;
                request = CreateWebRequest(url, AuthenticationResult);
                ProcessResponse(request, userThumbprint.User + "-" + userThumbprint.Thumbprint);
            }

            this.WriteCommandDetail(string.Format(LogMessages.RemoveCertificateSuccess, successCnt));
            if (failureCnt > 0)
            {
                this.WriteWarning(string.Format(LogMessages.RemoveCertificateFailure, successCnt));
            }
        }
Beispiel #3
0
        /// <summary>
        /// ProcessRecord.
        /// </summary>
        protected override void ProcessRecord()
        {
            if (!Authenticate.AuthTokenIsValid(AuthenticationResult))
            {
                this.ThrowTerminatingError(
                    new ErrorRecord(
                        new AuthenticationException("Cannot get Authentication Token"),
                        "Authentication Failure",
                        ErrorCategory.AuthenticationError,
                        AuthenticationResult));
            }

            successCnt = 0;
            failureCnt = 0;

            Hashtable modulePrivateData = this.MyInvocation.MyCommand.Module.PrivateData as Hashtable;
            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 = GetUserPFXCertificate.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));
            }
        }