protected override void ProcessRecord() { Hashtable modulePrivateData = this.MyInvocation.MyCommand.Module.PrivateData as Hashtable; AuthenticationResult authToken = Authenticate.GetAuthToken(AdminUserName, AdminPassword, modulePrivateData); if (!Authenticate.AuthTokenIsValid(authToken)) { this.ThrowTerminatingError( new ErrorRecord( new InvalidOperationException("Cannot get Authentication Token"), "Authentication Failure", ErrorCategory.AuthenticationError, authToken)); } }
/// <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); }
/// <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); string urlbase = string.Format(CultureInfo.InvariantCulture, "{0}/{1}/deviceManagement/userPfxCertificates", graphURI, schemaVersion); if (UserThumbprintList != null && UserThumbprintList.Count > 0) { foreach (UserThumbprint userthumbprint in UserThumbprintList) { string url = string.Format("{0}?$filter=tolower(userPrincipalName) eq '{1}' and tolower(thumbprint) eq '{2}'", urlbase, userthumbprint.User.ToLowerInvariant(), userthumbprint.Thumbprint.ToLowerInvariant()); HttpWebRequest request; try { request = CreateWebRequest(url, AuthenticationResult); // Returns a single record and comes back in a different format than the other requests. ProcessSingleResponse(request, "User:"******" Thumbprint:" + userthumbprint.Thumbprint); } catch (WebException we) { this.WriteError(new ErrorRecord(we, we.Message + " request-id:" + we.Response.Headers["request-id"], ErrorCategory.InvalidResult, userthumbprint)); } } } else if (UserList != null && UserList.Count > 0) { foreach (string user in UserList) { string url = string.Format("{0}?$filter=tolower(userPrincipalName) eq '{1}'", urlbase, user.ToLowerInvariant()); HttpWebRequest request; try { request = CreateWebRequest(url, AuthenticationResult); ProcessCollectionResponse(request, user); } catch (WebException we) { this.WriteError(new ErrorRecord(we, we.Message + " request-id:" + we.Response.Headers["request-id"], ErrorCategory.InvalidResult, user)); } } } else { HttpWebRequest request; try { request = CreateWebRequest(urlbase, AuthenticationResult); ProcessCollectionResponse(request, null); } catch (WebException we) { this.WriteError(new ErrorRecord(we, we.Message + " request-id:" + we.Response.Headers["request-id"], ErrorCategory.InvalidResult, null)); } } }
/// <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)); } }
/// <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)); } }