-
April 18th, 2016, 01:26 PM
#1
New to C# - working with dialog boxes and loops
Hello,
I am literally brand new to C# (I generally use PowerShell), and I am working on some web development in Visual Studio. I have a PS script that when a user runs it, it opens a file dialog box prompting for a text file, then takes that text file (which is a list of machine names) and iterates through each machine name in the text file, doing some "stuff" to it. So, everything I am finding on this shows that I would be defining a class for OpenFileDialog, etc. - where I am running into confusion is that I am doing this within a controller (.cs) where I already have classes defined, and I am not sure how to just "inject" this file dialog portion into it. It seems no matter where I add it, it is causing confusion - or it could just be that I have no idea what I am doing, and I am not afraid to admit that. I am not looking for "here is your answer", I am really just looking for some information and possibly a nudge in the right direction, etc.
Here is my code. with a nice comment showing where and what I am trying to do. Any guidance (not straight out answer, I am trying to learn) would be appreciated.
Thank you.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SupportToolkit.Models;
using SupportToolkit.WindowsAutomationServices;
using NLog;
namespace SupportToolkit.Controllers
{
public class RemoveTPOSRecordsController : Controller
{
private static Logger recordDeletionLogger = LogManager.GetCurrentClassLogger();
[Authorize(Roles = @"no\NST_RIT_Users,no\NST_STM_Users,no\NST_Admin_Users,no\NST_CORP_Users")]
// GET: RemoveTPOSRecords
public ActionResult Index(RemoveTPOSRecordsModel model)
{
if (model.TPOSMachineName != null)
{
}
if (ModelState.IsValid)
{
if (!(string.IsNullOrEmpty(model.btnDeleteRecord)))
{
InvokeRecordDeletion(model);
}
}
if (ModelState.IsValid)
{
if (!(string.IsNullOrEmpty(model.btnUploadFile)))
{
InvokeBulkDeletion(model);
}
}
return View(model);
}
[Authorize(Roles = @"no\NST_RIT_Users,no\NST_STM_Users,no\NST_Admin_Users,no\NST_CORP_Users")]
public string InvokeRecordDeletion(RemoveTPOSRecordsModel model)
{
model.Status = "Running Service";
var windowsAutomationService = new WindowsAutomationServicesClient();
string shortServiceOutput;
var serviceAction = "Remove-TPOSRecords";
var userName = User.Identity.Name;
string[] recordDeletionArguments = new string[1];
recordDeletionArguments[0] = model.TPOSMachineName;
var serviceOutput = windowsAutomationService.RunAutomationService(serviceAction, userName, recordDeletionArguments);
recordDeletionLogger.Info(userName + " is attempting to remove the record " + model.TPOSMachineName);
if (serviceOutput.Length >= 7)
{
shortServiceOutput = serviceOutput.Substring(0, 7);
shortServiceOutput = shortServiceOutput.ToLower();
}
else
{
shortServiceOutput = serviceOutput;
shortServiceOutput = shortServiceOutput.ToLower();
}
if (shortServiceOutput == "success")
{
model.Status = "Successfully removed " + model.TPOSMachineName + " from SCCM and AD";
recordDeletionLogger.Info(userName + " successfully removed " + model.TPOSMachineName + " from SCCM and AD");
return "Success";
}
else if (serviceOutput.Contains("=FailedMACFind"))
{
model.Status = "Failure removing " + model.TPOSMachineName + " from SCCM and AD. Machine name " + model.TPOSMachineName + " not found";
recordDeletionLogger.Info(userName + " failed to remove " + model.TPOSMachineName + " from SCCM and AD. Machine name " + model.TPOSMachineName + " not found");
return "Failure";
}
else if (serviceOutput.Contains("=MultipleEntries"))
{
model.Status = "Failure removing " + model.TPOSMachineName + " from SCCM and AD. Multiple entries for " + model.TPOSMachineName + " were found";
recordDeletionLogger.Info(userName + " failed to remove " + model.TPOSMachineName + " from SCCM and AD. Multiple entries for " + model.TPOSMachineName + " were found");
return "Failure";
}
else
{
model.Status = "Failure removing " + model.TPOSMachineName + " from SCCM and AD. Unknown Error";
recordDeletionLogger.Info(userName + " failed to remove " + model.TPOSMachineName + " from SCCM and AD");
return "Failure";
}
}
[Authorize(Roles = @"no\NST_RIT_Users,no\NST_STM_Users,no\NST_Admin_Users")]
public string InvokeBulkDeletion(RemoveTPOSRecordsModel model)
{
//somewhere in here is where I would be adding the file dialog/iteration through machine names, I would assume - then the code below is the "stuff" that happens to each machine
model.Status = "Running Service";
var windowsAutomationService = new WindowsAutomationServicesClient();
string shortServiceOutput;
var registerNames = model.TextMachineName;
var serviceAction = "Remove-TPOSRecords";
var userName = User.Identity.Name;
string[] recordDeletionArguments = new string[1];
recordDeletionArguments[0] = model.TextMachineName;
var serviceOutput = windowsAutomationService.RunAutomationService(serviceAction, userName, recordDeletionArguments);
recordDeletionLogger.Info(userName + " is attempting to remove the record " + model.TextMachineName);
if (serviceOutput.Length >= 7)
{
shortServiceOutput = serviceOutput.Substring(0, 7);
shortServiceOutput = shortServiceOutput.ToLower();
}
else
{
shortServiceOutput = serviceOutput;
shortServiceOutput = shortServiceOutput.ToLower();
}
if (shortServiceOutput == "success")
{
model.Status = "Successfully removed " + model.TextMachineName + " from SCCM and AD";
recordDeletionLogger.Info(userName + " successfully removed " + model.TextMachineName + " from SCCM and AD");
return "Success";
}
else if (serviceOutput.Contains("=FailedMACFind"))
{
model.Status = "Failure removing " + model.TextMachineName + " from SCCM and AD. Machine name " + model.TextMachineName + " not found";
recordDeletionLogger.Info(userName + " failed to remove " + model.TextMachineName + " from SCCM and AD. Machine name " + model.TextMachineName + " not found");
return "Failure";
}
else if (serviceOutput.Contains("=MultipleEntries"))
{
model.Status = "Failure removing " + model.TextMachineName + " from SCCM and AD. Multiple entries for " + model.TextMachineName + " were found";
recordDeletionLogger.Info(userName + " failed to remove " + model.TextMachineName + " from SCCM and AD. Multiple entries for " + model.TextMachineName + " were found");
return "Failure";
}
else
{
model.Status = "Failure removing " + model.TextMachineName + " from SCCM and AD. Unknown Error";
recordDeletionLogger.Info(userName + " failed to remove " + model.TextMachineName + " from SCCM and AD");
return "Failure";
}
}
}
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|