Esempio n. 1
0
        /// <summary>
        /// ProcessRecord.
        /// </summary>
        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));
            }

            string graphURI      = Authenticate.GetGraphURI(modulePrivateData);
            string schemaVersion = Authenticate.GetSchemaVersion(modulePrivateData);
            string userId        = GetUserId.GetUserIdFromUpn(UPN, graphURI, schemaVersion, AuthenticationResult);

            this.WriteObject(userId);
        }
Esempio n. 2
0
        /// <summary>
        /// ProcessRecord.
        /// </summary>
        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));
            }

            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 = GetUserId.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 = GetUserId.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));
            }
        }
Esempio n. 3
0
        /// <summary>
        /// ProcessRecord.
        /// </summary>
        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));
            }
        }