-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathMain.ps1
185 lines (162 loc) · 7.79 KB
/
Main.ps1
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<#
.SYNOPSIS
Main script to load and display the Intune Toolkit window.
.DESCRIPTION
This script loads a XAML file to define the UI, locates UI elements,
imports required external scripts, and displays the window. Error handling
and logging are implemented to catch and log errors during these processes.
.NOTES
Author: Maxime Guillemin | CloudFlow
Date: 09/07/2024
.EXAMPLE
Show-Window
Displays the main window of the application.
#>
$currentVersion = "v0.2.7.1-alpha"
#region log file
# Define the log file path
$global:logFile = "$env:TEMP\IntuneToolkit.log"
# Create a backup of the existing log file with the current date-time
if (Test-Path -Path $global:logFile -ErrorAction SilentlyContinue) {
$timestamp = (Get-Date).ToString("yyyyMMdd_HHmmss")
$backupFilePath = Join-Path -Path $env:TEMP -ChildPath "IntuneToolkit-$timestamp.log"
Copy-Item -Path $global:logFile -Destination $backupFilePath -ErrorAction SilentlyContinue
#Clear Existing $global:logFile content
Clear-Content -Path $global:logFile -ErrorAction SilentlyContinue
$logEntry = "Log entry created at $($timestamp)"
Add-Content -Path $global:logFile -Value $logEntry
} else {
# Create new log file if doesn't exist of after it was backed up
New-Item -Path $global:logFile -ItemType File -Force -ErrorAction SilentlyContinue
$logEntry = "Log entry created at $($timestamp)"
Add-Content -Path $global:logFile -Value $logEntry
}
#endregion
# Function to log actions and errors to the IntuneToolkit log file
function Write-IntuneToolkitLog {
param (
[string]$message,
[string]$component = "Main-IntuneToolkit",
[string]$context = "",
[string]$type = "1",
[string]$thread = [System.Threading.Thread]::CurrentThread.ManagedThreadId,
[string]$file = "Main.ps1"
)
$timestamp = Get-Date -Format "HH:mm:ss.fffzzz"
$date = Get-Date -Format "MM-dd-yyyy"
$logMessage = "<![LOG[$message]LOG]!><time=\$($timestamp)\ date=\$($date)\ component=\$($component)\ context=\$($context)\ type=\$($type)\ thread=\$($thread)\ file=\$($file)\>"
Add-Content -Path $logFile -Value $logMessage
}
# Initialize the debug log file
if (-Not (Test-Path $logFile)) {
New-Item -Path $logFile -ItemType File -Force | Out-Null
} else {
Add-Content -Path $logFile -Value "`n`n--- Script started at $(Get-Date -Format "yyyy-MM-dd HH:mm:ss") ---`n"
}
# Load required assemblies with error handling
try {
Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName System.Windows.Forms
Write-IntuneToolkitLog "Successfully loaded required assemblies"
} catch {
$errorMessage = "Failed to load required assemblies: $($_.Exception.Message)"
Write-Error $errorMessage
Write-IntuneToolkitLog $errorMessage
exit 1
}
# Check if PowerShell versoin is 7.0.0 based on requirements from https://github.com/MG-Cloudflow/Intune-Toolkit by Thiago Beier https://x.com/thiagobeier https://github.com/thiagogbeier
$PScurrentVersion = $PSVersionTable.PSVersion
$PSrequiredVersion = [Version]"7.0.0"
# Check if the current version is less than the required version
if ($PScurrentVersion -lt $PSrequiredVersion) {
$errorMessage = "You are running PowerShell version $PScurrentVersion. Please upgrade to PowerShell 7 or higher."
[System.Windows.Forms.MessageBox]::Show($errorMessage, "PowerShell Version outdated", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error) | Out-Null
Write-IntuneToolkitLog $errorMessage
exit 1
} else {
#$errorMessage = "You are running PowerShell version $currentVersion. All good!"
Write-IntuneToolkitLog $errorMessage
}
# Function to display the main window
function Show-Window {
Write-IntuneToolkitLog "Starting Show-Window"
try {
$xamlPath = ".\XML\Main.xaml"
if (-Not (Test-Path $xamlPath)) {
throw "XAML file not found: $xamlPath"
}
Write-IntuneToolkitLog "Loading XAML file from $xamlPath"
[xml]$xaml = Get-Content $xamlPath -ErrorAction Stop
$reader = (New-Object System.Xml.XmlNodeReader $xaml)
$Window = [Windows.Markup.XamlReader]::Load($reader)
Write-IntuneToolkitLog "Successfully loaded XAML file"
# Load UI elements
$TenantInfo = $Window.FindName("TenantInfo")
$StatusText = $Window.FindName("StatusText")
$ConnectButton = $Window.FindName("ConnectButton")
$ConnectEnterpriseAppButton = $Window.FindName("ConnectEnterpriseAppButton")
$LogoutButton = $Window.FindName("LogoutButton")
$RefreshButton = $Window.FindName("RefreshButton")
$StatusText = $Window.FindName("StatusText")
$PolicyDataGrid = $Window.FindName("PolicyDataGrid")
$RenameButton = $Window.FindName("RenameButton")
$DeleteAssignmentButton = $Window.FindName("DeleteAssignmentButton")
$AddAssignmentButton = $Window.FindName("AddAssignmentButton")
$BackupButton = $Window.FindName("BackupButton")
$RestoreButton = $Window.FindName("RestoreButton")
$ExportToCSVButton = $Window.FindName("ExportToCSVButton")
$ExportToMDButton = $Window.FindName("ExportToMDButton")
$ConfigurationPoliciesButton = $Window.FindName("ConfigurationPoliciesButton")
$DeviceConfigurationButton = $Window.FindName("DeviceConfigurationButton")
$ComplianceButton = $Window.FindName("ComplianceButton")
$AdminTemplatesButton = $Window.FindName("AdminTemplatesButton")
$ApplicationsButton = $Window.FindName("ApplicationsButton")
$AppConfigButton = $Window.FindName("AppConfigButton")
$RemediationScriptsButton = $Window.FindName("RemediationScriptsButton")
$PlatformScriptsButton = $Window.FindName("PlatformScriptsButton")
$MacosScriptsButton = $Window.FindName("MacosScriptsButton")
$SearchBox = $Window.FindName("SearchBox")
$SearchButton = $Window.FindName("SearchButton")
$SearchFieldComboBox = $Window.FindName("SearchFieldComboBox")
$global:CurrentPolicyType = ""
Get-ChildItem -Path ".\Scripts" -Recurse | Unblock-File
# Import external script files
. .\Scripts\Functions.ps1
. .\Scripts\AssignmentSettingsFunctions.ps1
. .\Scripts\Connect-ToMgGraph.ps1
. .\Scripts\ConnectButton.ps1
. .\Scripts\ConnectEnterpriseAppButton.ps1
. .\Scripts\LogoutButton.ps1
. .\Scripts\RefreshButton.ps1
. .\Scripts\ConfigurationPoliciesButton.ps1
. .\Scripts\DeviceConfigurationButton.ps1
. .\Scripts\ComplianceButton.ps1
. .\Scripts\AdminTemplatesButton.ps1
. .\Scripts\ApplicationsButton.ps1
. .\Scripts\DeleteAssignmentButton.ps1
. .\Scripts\AddAssignmentButton.ps1
. .\Scripts\BackupButton.ps1
. .\Scripts\RestoreButton.ps1
. .\Scripts\ExportToCSVButton.ps1
. .\Scripts\ExportToMDButton.ps1
. .\Scripts\Show-SelectionDialog.ps1
. .\Scripts\SearchButton.ps1
. .\Scripts\RemediationScriptsButton.ps1
. .\Scripts\RenameButton.ps1
. .\Scripts\PlatformScriptsButton.ps1
. .\Scripts\AppConfigButton.ps1
. .\Scripts\MacosScriptsButton.ps1
# Check for the latest version
. .\Scripts\CheckVersion.ps1
Check-LatestVersion -currentVersion $currentVersion
Write-IntuneToolkitLog "Successfully imported external scripts"
$Window.ShowDialog() | Out-Null
Write-IntuneToolkitLog "Displayed the window successfully"
} catch {
$errorMessage = "Failed to load and display the window: $($_.Exception.Message)"
Write-Error $errorMessage
Write-IntuneToolkitLog $errorMessage
}
}
# Show the window
Show-Window