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