Skip to content

Commit

Permalink
Enhanced update procedure
Browse files Browse the repository at this point in the history
Increased the update speed by restarting the app gracefully, when an update is applied.
  • Loading branch information
Weatherlights committed Feb 10, 2022
1 parent 69b771b commit 9583db2
Show file tree
Hide file tree
Showing 16 changed files with 154 additions and 61 deletions.
Binary file modified Code/Program/.vs/NetworkShareMapper/v17/.suo
Binary file not shown.
101 changes: 65 additions & 36 deletions Code/Program/NetworkShareMapper/NetworkDriveMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,68 +21,97 @@ public NetworkDriveMapping(PolicyRetrival policyRetrival)
myLogWriter = new LogWriter("NetworkDriveMapping");
}

private void MapDrives()
{
if (myPolicyRetrival.isEnabled())
try
{
bool isMapPersistent = myPolicyRetrival.isMapPersistent();
foreach (string policyName in myPolicyRetrival.retrivePolicyNames())
{
NetworkDriveMappingPolicy policy = myPolicyRetrival.GetPolicyByName(policyName);
if (policy.driveLetter != null && policy.uncPath != null)
try
{
DriveSettings.MapNetworkDrive(policy.driveLetter, policy.uncPath, isMapPersistent, policy.Username, policy.Password);
//myLogWriter.LogWrite("Mapped networkdrive " + policy.driveLetter + " to " + policy.uncPath);
}
catch (Exception e)
{
myLogWriter.LogWrite("Failed to map networkdrive " + policy.driveLetter + " to " + policy.uncPath + "\nException: " + e.ToString(), 2);
// do nothing
}
}
}
catch (Exception e)
{
myLogWriter.LogWrite("An unknown error occured.\nException: " + e.ToString(), 3);
}
}

public void Execute()
{

NetworkChangeDetector myNetworkChangeDetector = new NetworkChangeDetector();
myLogWriter.LogWrite("Initialized NetworkChangeDetector");
Task updateTask = new Task(myUpdateHandler.InstallUpdate);

Task<bool> searchUpdateTask = null;
Task<bool> installUpdateTask = null;

myLogWriter.LogWrite("Initialized UpdateTask");
int retryCount = 0;
while (true)
bool shouldRun = true;
while (shouldRun)
{
int sleepTime = myPolicyRetrival.getRefreshInterval();;
bool isMapPersistent = myPolicyRetrival.isMapPersistent();
int sleepTime = myPolicyRetrival.getRefreshInterval(); ;

if (myPolicyRetrival.isNetworkTestEnabled())
{
if (myNetworkChangeDetector.CheckNetworkChange())
{
myLogWriter.LogWrite("CheckNetworkChange() indicated a network change.");
retryCount = myPolicyRetrival.getRetryCount();
}
} else
}
else
{
retryCount = 1;
}

if (retryCount > 0)
{
//myLogWriter.LogWrite("retryCount is " + retryCount);
if (myPolicyRetrival.isEnabled())
try
{
foreach (string policyName in myPolicyRetrival.retrivePolicyNames())
{
NetworkDriveMappingPolicy policy = myPolicyRetrival.GetPolicyByName(policyName);
if (policy.driveLetter != null && policy.uncPath != null)
try
{
DriveSettings.MapNetworkDrive(policy.driveLetter, policy.uncPath, isMapPersistent, policy.Username, policy.Password);
//myLogWriter.LogWrite("Mapped networkdrive " + policy.driveLetter + " to " + policy.uncPath);
}
catch (Exception e)
{
myLogWriter.LogWrite("Failed to map networkdrive " + policy.driveLetter + " to " + policy.uncPath + "\nException: " + e.ToString(), 2);
// do nothing
}
}
}
catch (Exception e)
{
myLogWriter.LogWrite("An unknown error occured.\nException: " + e.ToString(), 3);
}
} else
MapDrives();
Thread.Sleep(sleepTime);
}
else
{
if (retryCount == 0)
myLogWriter.LogWrite("Will now to to sleep");
if (updateTask.Status == TaskStatus.Created)
myLogWriter.LogWrite("Will now go to sleep.");

Thread.Sleep(sleepTime);
bool updatesReadyToInstall = false; ;
if (searchUpdateTask == null)
searchUpdateTask = myUpdateHandler.SearchAndDownloadUpdates();
if (searchUpdateTask.IsCompleted)
updatesReadyToInstall = searchUpdateTask.Result;

if (updatesReadyToInstall)
{
updateTask.Start();
if (installUpdateTask == null)
installUpdateTask = myUpdateHandler.InstallUpdate();
if (installUpdateTask.IsCompleted)
if (installUpdateTask.Result)
{
shouldRun = false;
myLogWriter.LogWrite("Will now restart Intune Network Drive Mapping to install updates.", 1);
}

}

}

retryCount--;
Thread.Sleep(sleepTime);

}
}
}
Expand Down
5 changes: 4 additions & 1 deletion Code/Program/NetworkShareMapper/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@ class Program
[STAThread]
static void Main(string[] args)
{
LogWriter myLogWriter = new LogWriter("Program");
myLogWriter.LogWrite("Intune Network Drive Mapping has started.");
PolicyRetrival myPolicyRetrival = new PolicyRetrival();
if (!myPolicyRetrival.isEnabled() && args.Contains("Startmenu"))
{
myLogWriter.LogWrite("Has been started from start menu but configuration is missing. Will display user hint.",2);
UnconfiguredNoticeWindow myUnconfiguredNoticeWindow = new UnconfiguredNoticeWindow();
myUnconfiguredNoticeWindow.ShowDialog();
}

NetworkDriveMapping myNetworkDriveMapping = new NetworkDriveMapping(myPolicyRetrival);
myNetworkDriveMapping.Execute();

myLogWriter.LogWrite("Exiting Intune Network Drive Mapping", 2);
}
}
}
107 changes: 84 additions & 23 deletions Code/Program/NetworkShareMapper/UpdateHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ internal class UpdateHandler
{
private StoreContext context = null;
private LogWriter myLogWriter = null;
IReadOnlyList<StorePackageUpdate> storePackageUpdates = null;

public UpdateHandler()
{
Expand All @@ -34,63 +35,123 @@ private void SetRestartOnUpdate()

}

public async void InstallUpdate()
public async Task<bool> SearchAndDownloadUpdates()
{
myLogWriter.LogWrite("Begin InstallUpdate");
myLogWriter.LogWrite("Begin SearchAndDownloadUpdates");
bool updateReadyToInstall = false;
try
{
if (context == null)
{
context = StoreContext.GetDefault();
}

IReadOnlyList<StorePackageUpdate> storePackageUpdates =
await context.GetAppAndOptionalStorePackageUpdatesAsync();
storePackageUpdates =
await context.GetAppAndOptionalStorePackageUpdatesAsync();
myLogWriter.LogWrite("Search for Updates finished. Found " + storePackageUpdates.Count + " Updates.");
// Start the silent installation of the packages. Because the packages have already
// been downloaded in the previous method, the following line of code just installs
// the downloaded packages.

if (storePackageUpdates.Count > 0)
{
if (!context.CanSilentlyDownloadStorePackageUpdates)
{
myLogWriter.LogWrite("CanSilentlyDownloadStorePackageUpdates returned false. Will abort.", 2);
return;
updateReadyToInstall = false;
}
else
{
myLogWriter.LogWrite("Will now download updates.");

StorePackageUpdateResult downloadResult =
await context.TrySilentDownloadStorePackageUpdatesAsync(storePackageUpdates);

switch (downloadResult.OverallState)
{
case StorePackageUpdateState.Completed:
myLogWriter.LogWrite("Download has been finished successfully.");
updateReadyToInstall = true;
break;
case StorePackageUpdateState.Canceled:
myLogWriter.LogWrite("Update canceled: Canceled by the user.", 2);
updateReadyToInstall = false;
break;
case StorePackageUpdateState.ErrorLowBattery:
myLogWriter.LogWrite("Update canceled: Battery level to low to download update.", 2);
updateReadyToInstall = false;
break;
case StorePackageUpdateState.ErrorWiFiRecommended:
myLogWriter.LogWrite("Update canceled: The user is recommended to use a wifi to update.", 2);
updateReadyToInstall = false;
break;
case StorePackageUpdateState.ErrorWiFiRequired:
myLogWriter.LogWrite("Update canceled: A wifi connection is required to perform the update.", 2);
updateReadyToInstall = false;
break;
case StorePackageUpdateState.OtherError:
myLogWriter.LogWrite("Update canceled: Unknown error.", 3);
updateReadyToInstall = false;
break;
default:
updateReadyToInstall = false;
break;
}
}
}
} catch (Exception e)
{
myLogWriter.LogWrite("Update search could not be completed. Exception: " + e.ToString(), 2);
}
return updateReadyToInstall;
}


public async Task<bool> InstallUpdate()
{
bool updatesInstalledSuccessfully = false;
myLogWriter.LogWrite("Begin InstallUpdate");
try
{
SetRestartOnUpdate();
myLogWriter.LogWrite("Update is now installing.");
StorePackageUpdateResult downloadResult =
await context.TrySilentDownloadAndInstallStorePackageUpdatesAsync(storePackageUpdates);
myLogWriter.LogWrite("Installation of updates completed.");
switch (downloadResult.OverallState)
{
// If the user cancelled the installation or you can't perform the installation
// for some other reason, try again later. The RetryInstallLater method is not
// implemented in this example, you should implement it as needed for your own app.
case StorePackageUpdateState.Completed:
myLogWriter.LogWrite("Update installed successfully.", 1);
updatesInstalledSuccessfully = true;
break;
case StorePackageUpdateState.Canceled:
myLogWriter.LogWrite("Update was canceled by the user.", 2);
return;
updatesInstalledSuccessfully = false;
break;
case StorePackageUpdateState.ErrorWiFiRequired:
myLogWriter.LogWrite("No wifi connection available.", 2);
return;
case StorePackageUpdateState.ErrorWiFiRecommended:
updatesInstalledSuccessfully = false;
break;
case StorePackageUpdateState.ErrorWiFiRecommended:
myLogWriter.LogWrite("A wifi connection is recommended.", 2);
return;
case StorePackageUpdateState.ErrorLowBattery:
myLogWriter.LogWrite("Update failed because of low battery.", 2);
return;
case StorePackageUpdateState.OtherError:
updatesInstalledSuccessfully = false;
break;
case StorePackageUpdateState.ErrorLowBattery:
updatesInstalledSuccessfully = false;
break;
case StorePackageUpdateState.OtherError:
myLogWriter.LogWrite("Update failed because of unknown error.", 3);
return;
default:
updatesInstalledSuccessfully = false;
break;
default:
myLogWriter.LogWrite("Update installed successfully.", 1);
break;
}
updatesInstalledSuccessfully = false;
break;

}

} catch (Exception e)
{
myLogWriter.LogWrite("Update search could not be completed. Exception: " + e.ToString(), 2);
}
return updatesInstalledSuccessfully;

}
}
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ C:\Users\hauke\GitHub\Intune-Network-Drive-Mapping-Tool\Code\Program\NetworkShar
C:\Users\HaukeGötze\GitHub\Intune-Network-Drive-Mapping-Tool\Code\Program\NetworkShareMapper\bin\Debug\NetworkShareMapper.exe.config
C:\Users\HaukeGötze\GitHub\Intune-Network-Drive-Mapping-Tool\Code\Program\NetworkShareMapper\bin\Debug\NetworkShareMapper.exe
C:\Users\HaukeGötze\GitHub\Intune-Network-Drive-Mapping-Tool\Code\Program\NetworkShareMapper\bin\Debug\NetworkShareMapper.pdb
C:\Users\HaukeGötze\GitHub\Intune-Network-Drive-Mapping-Tool\Code\Program\NetworkShareMapper\obj\Debug\NetworkShareMapper.csproj.AssemblyReference.cache
C:\Users\HaukeGötze\GitHub\Intune-Network-Drive-Mapping-Tool\Code\Program\NetworkShareMapper\obj\Debug\NetworkShareMapper.csproj.SuggestedBindingRedirects.cache
C:\Users\HaukeGötze\GitHub\Intune-Network-Drive-Mapping-Tool\Code\Program\NetworkShareMapper\obj\Debug\UnconfiguredNoticeWindow.g.cs
C:\Users\HaukeGötze\GitHub\Intune-Network-Drive-Mapping-Tool\Code\Program\NetworkShareMapper\obj\Debug\NetworkShareMapper_MarkupCompile.cache
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ C:\Users\HaukeGötze\GitHub\Intune-Network-Drive-Mapping-Tool\Code\Program\Netwo
C:\Users\HaukeGötze\GitHub\Intune-Network-Drive-Mapping-Tool\Code\Program\NetworkShareMapper\obj\Release\NetworkShareMapper.csproj.CoreCompileInputs.cache
C:\Users\HaukeGötze\GitHub\Intune-Network-Drive-Mapping-Tool\Code\Program\NetworkShareMapper\obj\Release\NetworkShareMapper.exe
C:\Users\HaukeGötze\GitHub\Intune-Network-Drive-Mapping-Tool\Code\Program\NetworkShareMapper\obj\Release\NetworkShareMapper.pdb
C:\Users\HaukeGötze\GitHub\Intune-Network-Drive-Mapping-Tool\Code\Program\NetworkShareMapper\obj\Release\NetworkShareMapper.csproj.AssemblyReference.cache
Binary file not shown.
Binary file not shown.

0 comments on commit 9583db2

Please sign in to comment.