// Create a viewmodel from the WMI query results public AppRequestViewModel GetRequestData(ManagementObject m) { // Initialize and populate the ViewModel with the easy fields AppRequestViewModel newRequest = new AppRequestViewModel { Application = m.GetPropertyValue("Application").ToString(), Comments = m.GetPropertyValue("Comments").ToString(), LastModifiedBy = m.GetPropertyValue("LastModifiedBy").ToString(), RequestGuid = m.GetPropertyValue("RequestGuid").ToString(), UserName = m.GetPropertyValue("User").ToString(), //ModifyingUser = System.Security.Principal.WindowsIdentity.GetCurrent() ModifyingUser = System.Web.HttpContext.Current.User.Identity.Name }; // Get the two that require some extra processing /* * We get CurrentState as an integer from the SCCM WMI * database, so we need to use the StateList enum to * convert that to a string that makes sense to the user */ StateList stateList = (StateList)Convert.ToInt16(m.GetPropertyValue("CurrentState")); newRequest.CurrentState = stateList.ToString(); /* * Convert LastModifiedTime to a DateTime object using ParseExact, * then convert to user local time for times that make sense */ Char delim = '.'; string[] modTime = m.GetPropertyValue("LastModifiedDate").ToString().Split(delim); CultureInfo provider = CultureInfo.InvariantCulture; DateTime iKnowThisIsUtc = DateTime.ParseExact(modTime[0], "yyyyMMddHHmmss", provider); DateTime runTimeKnowsThisIsUtc = DateTime.SpecifyKind( iKnowThisIsUtc, DateTimeKind.Utc); newRequest.LastModifiedTime = runTimeKnowsThisIsUtc.ToLocalTime(); return(newRequest); }
public ActionResult Deny(string requestGuid, string newComments, string modUser) { // Create our WMI connector for this session WmiConnector wmiConnector = new WmiConnector(); ManagementObjectCollection results = wmiConnector.PerformWmiQuery("SELECT * FROM SMS_UserApplicationRequest WHERE RequestGUID=\"" + requestGuid + "\""); if (results.Count.Equals(1)) { foreach (ManagementObject m in results) { // Set up our method parameters ManagementBaseObject inParams = m.GetMethodParameters("Deny"); inParams["Comments"] = newComments; // Approve the request ManagementBaseObject approval = m.InvokeMethod("Deny", inParams, null); AppRequestViewModel result = wmiConnector.GetRequestData(m); // This doesn't currently appear to be functioning if (approval.GetPropertyValue("StatusCode").ToString() == 0.ToString()) { result.ActionResult = result.Application + " was successfully denied for user " + result.UserName + " at " + result.LastModifiedTime; } else { result.ActionResult = result.Application + " was not denied for user " + result.UserName + " at " + result.LastModifiedTime; } return(RedirectToAction("ViewRequest", "CmApp", result)); } } // Just redirecting back to root if the query goes wonky // TODO: Add some error catching/handling return(Redirect("/CmApp")); }