|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using System.Windows; |
| 8 | +using System.Windows.Controls; |
| 9 | +using System.Windows.Input; |
| 10 | +using System.Windows.Interop; |
| 11 | +using Microsoft.Identity.Client; |
| 12 | +using Newtonsoft.Json; |
| 13 | + |
| 14 | +namespace Endpoint_Manager_Scripts_Editor |
| 15 | +{ |
| 16 | + public partial class MainWindow |
| 17 | + { |
| 18 | + string[] scopes = new string[] { "DeviceManagementConfiguration.ReadWrite.All" }; |
| 19 | + private RootValue scripts; |
| 20 | + private string authToken; |
| 21 | + |
| 22 | + public MainWindow() |
| 23 | + { |
| 24 | + InitializeComponent(); |
| 25 | + } |
| 26 | + |
| 27 | + private async void Btn_ConnectIntune(object sender, RoutedEventArgs e) |
| 28 | + { |
| 29 | + AuthenticationResult authResult = null; |
| 30 | + var app = App.PublicClientApp; |
| 31 | + |
| 32 | + var accounts = await app.GetAccountsAsync(); |
| 33 | + var firstAccount = accounts.FirstOrDefault(); |
| 34 | + |
| 35 | + try |
| 36 | + { |
| 37 | + authResult = await app.AcquireTokenSilent(scopes, firstAccount) |
| 38 | + .ExecuteAsync(); |
| 39 | + } |
| 40 | + catch (MsalUiRequiredException ex) |
| 41 | + { |
| 42 | + // A MsalUiRequiredException happened on AcquireTokenSilent. |
| 43 | + // This indicates you need to call AcquireTokenInteractive to acquire a token |
| 44 | + System.Diagnostics.Debug.WriteLine($"MsalUiRequiredException: {ex.Message}"); |
| 45 | + |
| 46 | + try |
| 47 | + { |
| 48 | + authResult = await app.AcquireTokenInteractive(scopes) |
| 49 | + .WithAccount(accounts.FirstOrDefault()) |
| 50 | + .WithParentActivityOrWindow(new WindowInteropHelper(this).Handle) // optional, used to center the browser on the window |
| 51 | + .WithPrompt(Microsoft.Identity.Client.Prompt.SelectAccount) |
| 52 | + .ExecuteAsync(); |
| 53 | + } |
| 54 | + catch (MsalException msalex) |
| 55 | + { |
| 56 | + Status.Text = $"Error Acquiring Token:{System.Environment.NewLine}{msalex}"; |
| 57 | + } |
| 58 | + } |
| 59 | + catch (Exception ex) |
| 60 | + { |
| 61 | + Status.Text = $"Error Acquiring Token Silently:{System.Environment.NewLine}{ex}"; |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + if (authResult != null) |
| 66 | + { |
| 67 | + Tenant.Text = $"TenantId: {authResult.TenantId}"; |
| 68 | + Account.Text = $"Account: {authResult.Account.Username}"; |
| 69 | + Status.Text = "Retrieving list of scripts"; |
| 70 | + authToken = authResult.AccessToken; |
| 71 | + string graphAPIEndpoint = "https://graph.microsoft.com/beta/deviceManagement/deviceManagementScripts"; |
| 72 | + var result = await GetHttpContentWithToken(graphAPIEndpoint, authResult.AccessToken); |
| 73 | + |
| 74 | + scripts = JsonConvert.DeserializeObject<RootValue>(result); |
| 75 | + ArrayList scriptList = new ArrayList(); |
| 76 | + foreach (Script script in scripts.value) |
| 77 | + { |
| 78 | + scriptList.Add(script.displayName); |
| 79 | + }; |
| 80 | + scriptList.Sort(); |
| 81 | + ComboBox.ItemsSource = scriptList; |
| 82 | + Status.Text = "Select a script"; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + public async Task<string> GetHttpContentWithToken(string url, string token) |
| 87 | + { |
| 88 | + var httpClient = new System.Net.Http.HttpClient(); |
| 89 | + System.Net.Http.HttpResponseMessage response; |
| 90 | + |
| 91 | + try |
| 92 | + { |
| 93 | + var request = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, url); |
| 94 | + //Add the token in Authorization header |
| 95 | + request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token); |
| 96 | + response = await httpClient.SendAsync(request); |
| 97 | + var content = await response.Content.ReadAsStringAsync(); |
| 98 | + return content; |
| 99 | + } |
| 100 | + catch (Exception ex) |
| 101 | + { |
| 102 | + return ex.ToString(); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private async void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) |
| 107 | + { |
| 108 | + int index = scripts.value.FindIndex(x => x.displayName == ComboBox.SelectedItem); |
| 109 | + string id = scripts.value[index].id; |
| 110 | + string graphAPIEndpoint = "https://graph.microsoft.com/beta/deviceManagement/deviceManagementScripts" + "/" + id; |
| 111 | + var result = await GetHttpContentWithToken(graphAPIEndpoint, authToken); |
| 112 | + |
| 113 | + Script script = JsonConvert.DeserializeObject<Script>(result); |
| 114 | + var base64 = Convert.FromBase64String(script.scriptContent); |
| 115 | + var scriptText = Encoding.UTF8.GetString(base64); |
| 116 | + ScriptWindow.Text = scriptText; |
| 117 | + FileName.IsEnabled = true; |
| 118 | + FileName.Foreground = System.Windows.Media.Brushes.AntiqueWhite; |
| 119 | + FileName.Text = $"Filename: {script.fileName} |"; |
| 120 | + RunasThirtyTwo.IsEnabled = true; |
| 121 | + RunasThirtyTwo.Text = $"Run as 32-bit: {script.runAs32Bit} |"; |
| 122 | + RunasThirtyTwo.Foreground = System.Windows.Media.Brushes.AntiqueWhite; |
| 123 | + SignatureCheck.IsEnabled = true; |
| 124 | + SignatureCheck.Text = $"Enforce signature check: {script.enforceSignatureCheck} |"; |
| 125 | + SignatureCheck.Foreground = System.Windows.Media.Brushes.AntiqueWhite; |
| 126 | + RunasAccount.IsEnabled = true; |
| 127 | + RunasAccount.Text = $"Run as account: {script.runAsAccount} |"; |
| 128 | + RunasAccount.Foreground = System.Windows.Media.Brushes.AntiqueWhite; |
| 129 | + Created.IsEnabled = true; |
| 130 | + Created.Text = $"Created: {script.createdDateTime} |"; |
| 131 | + Created.Foreground = System.Windows.Media.Brushes.AntiqueWhite; |
| 132 | + Modified.IsEnabled = true; |
| 133 | + Modified.Text = $"Modified: {script.lastModifiedDateTime} |"; |
| 134 | + Modified.Foreground = System.Windows.Media.Brushes.AntiqueWhite; |
| 135 | + Description.IsEnabled = true; |
| 136 | + Description.Text = $"Description: {script.description}"; |
| 137 | + Description.Foreground = System.Windows.Media.Brushes.AntiqueWhite; |
| 138 | + } |
| 139 | + |
| 140 | + private void MetroWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) |
| 141 | + { |
| 142 | + this.DragMove(); |
| 143 | + } |
| 144 | + } |
| 145 | + public class RootValue |
| 146 | + { |
| 147 | + public string odatametadata { get; set; } |
| 148 | + public List<Script> value; |
| 149 | + } |
| 150 | + public class Script |
| 151 | + { |
| 152 | + public bool enforceSignatureCheck { get; set; } |
| 153 | + public bool runAs32Bit { get; set; } |
| 154 | + public string id { get; set; } |
| 155 | + public string displayName { get; set; } |
| 156 | + public string scriptContent { get; set; } |
| 157 | + public string description { get; set; } |
| 158 | + public DateTime createdDateTime { get; set; } |
| 159 | + public DateTime lastModifiedDateTime { get; set; } |
| 160 | + public string runAsAccount { get; set; } |
| 161 | + public string fileName { get; set; } |
| 162 | + } |
| 163 | + |
| 164 | +} |
0 commit comments