Skip to content

Commit

Permalink
Implemented upload of existing drive-mapping-configurations as PowerS…
Browse files Browse the repository at this point in the history
…hell scripts and some smaller optimizations
  • Loading branch information
nicolonsky committed Aug 24, 2019
1 parent 72f09e6 commit 4fc73a5
Showing 1 changed file with 75 additions and 86 deletions.
161 changes: 75 additions & 86 deletions IntuneDriveMapping/Controllers/DriveMappingController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,21 @@ public class DriveMappingController : Controller
//Http session configs
const string sessionName = "driveMappingList";
const string errosSession = "lastError";
const string aadAppRegSession = "appReg";

//Configuration params for the generated PowerShell script
const string poshInsertString = "!INTUNEDRIVEMAPPINGJSON!";
const string poshTemplateName = "IntuneDriveMappingTemplate.ps1";
const string poshExportName = "DriveMapping.ps1";
const string poshConfigVariable = "$driveMappingJson=";
const string poshConfigVariableEnd = "$driveMappingConfig";

//default view where everything comes together
const string indexView = "Index";


public ActionResult Index()
{
//don't display any table data if no content is available
ViewBag.ShowList = false;


//get version
try
{
Expand All @@ -51,7 +47,6 @@ public ActionResult Index()
//SunFunNothingTodo
}


//check if error message is stored in session & forward to view
if (HttpContext.Session.GetString(errosSession) != null)
{
Expand All @@ -62,7 +57,6 @@ public ActionResult Index()

}


//check if a drivemapping list exists and display it
if (HttpContext.Session.GetString(sessionName)==null)
{
Expand All @@ -71,34 +65,26 @@ public ActionResult Index()

else
{

List<DriveMappingModel> driveMappings = JsonConvert.DeserializeObject<List<DriveMappingModel>>(HttpContext.Session.GetString(sessionName));

ViewBag.ShowList = true;

return View(driveMappings);

}

}

public ActionResult Create()
public ActionResult Create()
{

return View();

}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(DriveMappingModel driveMapping)
{

try
{

if (ModelState.IsValid)
{

//check if first ever item is addedd or list with entries already exists
if (HttpContext.Session.GetString(sessionName) != null)
{
Expand All @@ -122,11 +108,9 @@ public ActionResult Create(DriveMappingModel driveMapping)
else
{
return View();

}

return RedirectToAction(indexView);

}

catch (Exception ex)
Expand All @@ -136,107 +120,112 @@ public ActionResult Create(DriveMappingModel driveMapping)
return RedirectToAction(indexView);

}

}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Upload()
{
try
{
var file = Request.Form.Files[0];

if (file != null && file.Length > 0)
{
if (file.FileName.Contains(".ps1"))
{

string powerShellContent;

string driveMappingJson;

int driveMappingJsonLocation;

int driveMappingJsonLocationEnd;

StreamReader inputStreamReader = new StreamReader(file.OpenReadStream());
if (file.FileName.Contains(".ps1"))
{

powerShellContent = inputStreamReader.ReadToEnd();
string driveMappingConfig=null;
string line;


driveMappingJsonLocation = powerShellContent.IndexOf(poshConfigVariable);
System.IO.StreamReader powerShellContent = new System.IO.StreamReader(file.OpenReadStream());

driveMappingJsonLocationEnd = powerShellContent.IndexOf(poshConfigVariableEnd);
while ((line = powerShellContent.ReadLine()) != null)
{
if(line.StartsWith(poshConfigVariable))
{
driveMappingConfig = line;

driveMappingJson = powerShellContent.Substring(driveMappingJsonLocation, driveMappingJsonLocationEnd);
driveMappingConfig = driveMappingConfig.Replace(poshConfigVariable, "");
driveMappingConfig = driveMappingConfig.TrimStart('\'');
driveMappingConfig = driveMappingConfig.TrimEnd('\'');

throw new Exception(driveMappingJson);
}
}

List <DriveMappingModel> driveMappings = JsonConvert.DeserializeObject<List<DriveMappingModel>>(driveMappingJson);
if (driveMappingConfig != null)
{
List<DriveMappingModel> driveMappings = JsonConvert.DeserializeObject<List<DriveMappingModel>>(driveMappingConfig);

HttpContext.Session.SetString(sessionName, JsonConvert.SerializeObject(driveMappings));

return RedirectToAction(indexView);

}
else if (file.FileName.Contains(".xml"))
else
{
// create xmldoc
XmlDocument xmldoc = new XmlDocument();
throw new SystemException("Could not find configuration in uploaded PowerShell script.");
}
}
else if (file.FileName.Contains(".xml"))
{
// create xmldoc
XmlDocument xmldoc = new XmlDocument();

xmldoc.Load(file.OpenReadStream());
xmldoc.Load(file.OpenReadStream());

//namespace manager & URI's needed in order to read GPP nodes
XmlNamespaceManager nsmanager = new XmlNamespaceManager(xmldoc.NameTable);
//namespace manager & URI's needed in order to read GPP nodes
XmlNamespaceManager nsmanager = new XmlNamespaceManager(xmldoc.NameTable);

nsmanager.AddNamespace("q1", "http://www.microsoft.com/GroupPolicy/Settings");
nsmanager.AddNamespace("q2", "http://www.microsoft.com/GroupPolicy/Settings/DriveMaps");
nsmanager.AddNamespace("q1", "http://www.microsoft.com/GroupPolicy/Settings");
nsmanager.AddNamespace("q2", "http://www.microsoft.com/GroupPolicy/Settings/DriveMaps");

XmlNodeList driveProperties = xmldoc.SelectNodes("q1:GPO/q1:User/q1:ExtensionData/q1:Extension/q2:DriveMapSettings/q2:Drive", nsmanager);
XmlNodeList driveProperties = xmldoc.SelectNodes("q1:GPO/q1:User/q1:ExtensionData/q1:Extension/q2:DriveMapSettings/q2:Drive", nsmanager);

//create list to store all entries
List<DriveMappingModel> driveMappings = new List<DriveMappingModel>();
//create list to store all entries
List<DriveMappingModel> driveMappings = new List<DriveMappingModel>();

DriveMappingModel driveMapping;
DriveMappingModel driveMapping;

//helper index to assign id to our entries
int i = 0;
//helper index to assign id to our entries
int i = 0;

foreach (XmlNode property in driveProperties)
foreach (XmlNode property in driveProperties)
{
//the real drive mapping configuration is stored in the 2nd XML child-node --> index 1
driveMapping = new DriveMappingModel
{
Path = property.ChildNodes[1].Attributes["path"].InnerXml,
DriveLetter = property.ChildNodes[1].Attributes["letter"].InnerXml,
Label = property.ChildNodes[1].Attributes["label"].InnerXml,
Id = (i + 1)
};

//check if we have a filter applied as child node --> index 2
try
{
//the real drive mapping configuration is stored in the 2nd XML child-node --> index 1
driveMapping = new DriveMappingModel
{
Path = property.ChildNodes[1].Attributes["path"].InnerXml,
DriveLetter = property.ChildNodes[1].Attributes["letter"].InnerXml,
Label = property.ChildNodes[1].Attributes["label"].InnerXml,
Id = (i + 1)
};

//check if we have a filter applied as child node --> index 2
try
{
string groupFilter = property.ChildNodes[2].ChildNodes[0].Attributes["name"].InnerXml;

String[] streamlinedGroupFilter = groupFilter.Split('\\');

driveMapping.GroupFilter = streamlinedGroupFilter[1];
}
catch
{
//nothing we can do
}

driveMappings.Add(driveMapping);

i++;
string groupFilter = property.ChildNodes[2].ChildNodes[0].Attributes["name"].InnerXml;

String[] streamlinedGroupFilter = groupFilter.Split('\\');

driveMapping.GroupFilter = streamlinedGroupFilter[1];
}
catch
{
//nothing we can do
}

HttpContext.Session.SetString(sessionName, JsonConvert.SerializeObject(driveMappings));
}else
{
throw new NullReferenceException();
driveMappings.Add(driveMapping);

i++;
}

HttpContext.Session.SetString(sessionName, JsonConvert.SerializeObject(driveMappings));
}
else
{
throw new NullReferenceException();
}

return RedirectToAction(indexView);
}
catch (Exception ex)
Expand Down Expand Up @@ -276,14 +265,14 @@ public ActionResult Edit(int? Id)
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(DriveMappingModel driveMapping)
{
try
{
if (ModelState.IsValid)
{
//haven't found better solution --> improvement needed!
//so i just remove the existing entry and add the new one and do a resort of the list
//haven't found better solution: so i just remove the existing entry and add the new one and do a resort of the list

List<DriveMappingModel> driveMappings = JsonConvert.DeserializeObject<List<DriveMappingModel>>(HttpContext.Session.GetString(sessionName));

Expand Down

0 comments on commit 4fc73a5

Please sign in to comment.