-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSetupRollback.ps1
More file actions
87 lines (73 loc) · 3.54 KB
/
SetupRollback.ps1
File metadata and controls
87 lines (73 loc) · 3.54 KB
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
<#
.SYNOPSIS
Main script that's executed in case a feature update is rolled back.
.DESCRIPTION
Main script that's executed in case a feature update is rolled back.
.EXAMPLE
.\SetupRollback.ps1
.NOTES
FileName: SetupRollback.ps1
Author: Nickolaj Andersen
Contact: @NickolajA
Created: 2024-08-26
Updated: 2024-08-26
Version history:
1.0.0 - (2024-08-26) Script created
#>
Begin {
# Declare variables for logging and script name
$ScriptName = "SetupRollback"
$ScriptLogFileName = "SetupRollback.log"
}
Process {
# Functions
function Write-LogEntry {
param (
[parameter(Mandatory = $true, HelpMessage = "Value added to the log file.")]
[ValidateNotNullOrEmpty()]
[string]$Value,
[parameter(Mandatory = $true, HelpMessage = "Severity for the log entry. 1 for Informational, 2 for Warning and 3 for Error.")]
[ValidateNotNullOrEmpty()]
[ValidateSet("1", "2", "3")]
[string]$Severity,
[parameter(Mandatory = $false, HelpMessage = "Name of the log file that the entry will written to.")]
[ValidateNotNullOrEmpty()]
[string]$FileName = $ScriptLogFileName
)
# Check if the script is running as SYSTEM, else use the user's temp folder for the log file location
if ([Security.Principal.WindowsIdentity]::GetCurrent().IsSystem -eq $true) {
$LogFilePath = Join-Path -Path (Join-Path -Path $env:ProgramData -ChildPath "Microsoft\IntuneManagementExtension\Logs") -ChildPath $FileName
}
else {
$LogFilePath = Join-Path -Path (Join-Path -Path $env:TEMP -ChildPath "RemediationScript\Logs") -ChildPath $FileName
}
# Create log folder path if it does not exist
try {
$LogFolderPath = Split-Path -Path $LogFilePath -Parent
if (-not(Test-Path -Path $LogFolderPath)) {
New-Item -ItemType "Directory" -Path $LogFolderPath -Force -ErrorAction "Stop" | Out-Null
}
}
catch [System.Exception] {
Write-Warning -Message "An error occurred while attempting to create the log folder path. Error message at line $($_.InvocationInfo.ScriptLineNumber): $($_.Exception.Message)"
}
# Construct time stamp for log entry
$Time = -join @((Get-Date -Format "HH:mm:ss.fff"), "+", (Get-WmiObject -Class Win32_TimeZone | Select-Object -ExpandProperty Bias))
# Construct date for log entry
$Date = (Get-Date -Format "MM-dd-yyyy")
# Construct context for log entry
$Context = $([System.Security.Principal.WindowsIdentity]::GetCurrent().Name)
# Construct final log entry
$LogText = "<![LOG[$($Value)]LOG]!><time=""$($Time)"" date=""$($Date)"" component=""$($ScriptName)"" context=""$($Context)"" type=""$($Severity)"" thread=""$($PID)"" file="""">"
# Add value to log file
try {
Out-File -InputObject $LogText -Append -NoClobber -Encoding Default -FilePath $LogFilePath -ErrorAction Stop
}
catch [System.Exception] {
Write-Warning -Message "Unable to append log entry $($ScriptName).log file. Error message at line $($_.InvocationInfo.ScriptLineNumber): $($_.Exception.Message)"
}
}
# Log that the feature update rollback has been initiated
Write-LogEntry -Value "Feature update rollback initiated" -Severity 1
# Script logic to be added here for when a feature update is rolled back
}