diff --git a/Code/Program/.vs/NetworkShareMapper/v17/.suo b/Code/Program/.vs/NetworkShareMapper/v17/.suo index b0aeb93..4083b8f 100644 Binary files a/Code/Program/.vs/NetworkShareMapper/v17/.suo and b/Code/Program/.vs/NetworkShareMapper/v17/.suo differ diff --git a/Code/Program/NetworkShareMapper/NetworkDriveMapping.cs b/Code/Program/NetworkShareMapper/NetworkDriveMapping.cs index b0e9b5a..6480474 100644 --- a/Code/Program/NetworkShareMapper/NetworkDriveMapping.cs +++ b/Code/Program/NetworkShareMapper/NetworkDriveMapping.cs @@ -21,18 +21,50 @@ 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 searchUpdateTask = null; + Task 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()) @@ -40,49 +72,46 @@ public void Execute() 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); + } } } diff --git a/Code/Program/NetworkShareMapper/Program.cs b/Code/Program/NetworkShareMapper/Program.cs index 2a0dc3d..9daa6a0 100644 --- a/Code/Program/NetworkShareMapper/Program.cs +++ b/Code/Program/NetworkShareMapper/Program.cs @@ -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); } } } diff --git a/Code/Program/NetworkShareMapper/UpdateHandler.cs b/Code/Program/NetworkShareMapper/UpdateHandler.cs index ce4e329..90ea6be 100644 --- a/Code/Program/NetworkShareMapper/UpdateHandler.cs +++ b/Code/Program/NetworkShareMapper/UpdateHandler.cs @@ -12,6 +12,7 @@ internal class UpdateHandler { private StoreContext context = null; private LogWriter myLogWriter = null; + IReadOnlyList storePackageUpdates = null; public UpdateHandler() { @@ -34,9 +35,10 @@ private void SetRestartOnUpdate() } - public async void InstallUpdate() + public async Task SearchAndDownloadUpdates() { - myLogWriter.LogWrite("Begin InstallUpdate"); + myLogWriter.LogWrite("Begin SearchAndDownloadUpdates"); + bool updateReadyToInstall = false; try { if (context == null) @@ -44,19 +46,70 @@ public async void InstallUpdate() context = StoreContext.GetDefault(); } - IReadOnlyList 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 InstallUpdate() + { + bool updatesInstalledSuccessfully = false; + myLogWriter.LogWrite("Begin InstallUpdate"); + try + { SetRestartOnUpdate(); myLogWriter.LogWrite("Update is now installing."); StorePackageUpdateResult downloadResult = @@ -64,33 +117,41 @@ public async void InstallUpdate() 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; } } diff --git a/Code/Program/NetworkShareMapper/bin/Debug/NetworkShareMapper.exe b/Code/Program/NetworkShareMapper/bin/Debug/NetworkShareMapper.exe index 6ed0a9c..b0ba4ef 100644 Binary files a/Code/Program/NetworkShareMapper/bin/Debug/NetworkShareMapper.exe and b/Code/Program/NetworkShareMapper/bin/Debug/NetworkShareMapper.exe differ diff --git a/Code/Program/NetworkShareMapper/bin/Debug/NetworkShareMapper.pdb b/Code/Program/NetworkShareMapper/bin/Debug/NetworkShareMapper.pdb index 2d4c9c4..a737575 100644 Binary files a/Code/Program/NetworkShareMapper/bin/Debug/NetworkShareMapper.pdb and b/Code/Program/NetworkShareMapper/bin/Debug/NetworkShareMapper.pdb differ diff --git a/Code/Program/NetworkShareMapper/bin/Release/NetworkShareMapper.exe b/Code/Program/NetworkShareMapper/bin/Release/NetworkShareMapper.exe index dcff548..23d1ea4 100644 Binary files a/Code/Program/NetworkShareMapper/bin/Release/NetworkShareMapper.exe and b/Code/Program/NetworkShareMapper/bin/Release/NetworkShareMapper.exe differ diff --git a/Code/Program/NetworkShareMapper/bin/Release/NetworkShareMapper.pdb b/Code/Program/NetworkShareMapper/bin/Release/NetworkShareMapper.pdb index e37f7e1..b0bc586 100644 Binary files a/Code/Program/NetworkShareMapper/bin/Release/NetworkShareMapper.pdb and b/Code/Program/NetworkShareMapper/bin/Release/NetworkShareMapper.pdb differ diff --git a/Code/Program/NetworkShareMapper/obj/Debug/NetworkShareMapper.csproj.AssemblyReference.cache b/Code/Program/NetworkShareMapper/obj/Debug/NetworkShareMapper.csproj.AssemblyReference.cache deleted file mode 100644 index 204fdef..0000000 Binary files a/Code/Program/NetworkShareMapper/obj/Debug/NetworkShareMapper.csproj.AssemblyReference.cache and /dev/null differ diff --git a/Code/Program/NetworkShareMapper/obj/Debug/NetworkShareMapper.csproj.FileListAbsolute.txt b/Code/Program/NetworkShareMapper/obj/Debug/NetworkShareMapper.csproj.FileListAbsolute.txt index 888baa2..0e811b0 100644 --- a/Code/Program/NetworkShareMapper/obj/Debug/NetworkShareMapper.csproj.FileListAbsolute.txt +++ b/Code/Program/NetworkShareMapper/obj/Debug/NetworkShareMapper.csproj.FileListAbsolute.txt @@ -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 diff --git a/Code/Program/NetworkShareMapper/obj/Debug/NetworkShareMapper.exe b/Code/Program/NetworkShareMapper/obj/Debug/NetworkShareMapper.exe index 6ed0a9c..b0ba4ef 100644 Binary files a/Code/Program/NetworkShareMapper/obj/Debug/NetworkShareMapper.exe and b/Code/Program/NetworkShareMapper/obj/Debug/NetworkShareMapper.exe differ diff --git a/Code/Program/NetworkShareMapper/obj/Debug/NetworkShareMapper.pdb b/Code/Program/NetworkShareMapper/obj/Debug/NetworkShareMapper.pdb index 2d4c9c4..a737575 100644 Binary files a/Code/Program/NetworkShareMapper/obj/Debug/NetworkShareMapper.pdb and b/Code/Program/NetworkShareMapper/obj/Debug/NetworkShareMapper.pdb differ diff --git a/Code/Program/NetworkShareMapper/obj/Release/NetworkShareMapper.csproj.AssemblyReference.cache b/Code/Program/NetworkShareMapper/obj/Release/NetworkShareMapper.csproj.AssemblyReference.cache new file mode 100644 index 0000000..028c3af Binary files /dev/null and b/Code/Program/NetworkShareMapper/obj/Release/NetworkShareMapper.csproj.AssemblyReference.cache differ diff --git a/Code/Program/NetworkShareMapper/obj/Release/NetworkShareMapper.csproj.FileListAbsolute.txt b/Code/Program/NetworkShareMapper/obj/Release/NetworkShareMapper.csproj.FileListAbsolute.txt index ad258ed..6041bc7 100644 --- a/Code/Program/NetworkShareMapper/obj/Release/NetworkShareMapper.csproj.FileListAbsolute.txt +++ b/Code/Program/NetworkShareMapper/obj/Release/NetworkShareMapper.csproj.FileListAbsolute.txt @@ -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 diff --git a/Code/Program/NetworkShareMapper/obj/Release/NetworkShareMapper.exe b/Code/Program/NetworkShareMapper/obj/Release/NetworkShareMapper.exe index dcff548..23d1ea4 100644 Binary files a/Code/Program/NetworkShareMapper/obj/Release/NetworkShareMapper.exe and b/Code/Program/NetworkShareMapper/obj/Release/NetworkShareMapper.exe differ diff --git a/Code/Program/NetworkShareMapper/obj/Release/NetworkShareMapper.pdb b/Code/Program/NetworkShareMapper/obj/Release/NetworkShareMapper.pdb index e37f7e1..b0bc586 100644 Binary files a/Code/Program/NetworkShareMapper/obj/Release/NetworkShareMapper.pdb and b/Code/Program/NetworkShareMapper/obj/Release/NetworkShareMapper.pdb differ