-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResolve-ReAgentC.ps1
More file actions
138 lines (113 loc) · 4.94 KB
/
Copy pathResolve-ReAgentC.ps1
File metadata and controls
138 lines (113 loc) · 4.94 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
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
function Resolve-ReAgentC {
<#
.SYNOPSIS
Enables Windows recovery if not already.
.DESCRIPTION
Sets a couple of session variables and parses "ReagentC.exe /info" output. If the system is not already enabled this
will start to set the system up so it can be enabled and removes any extra recovery partitions that are not
in use so it can find the correct one.
.PARAMETER LogPath
Path of the logfile you want it to log to. Default is C:\Temp.
.INPUTS
Description of objects that can be piped to the script.
.OUTPUTS
WindowsRecoveryStatus object, either enabled or disabled.
.EXAMPLE
Resolve-ReAgentC
.LINK
https://docs.microsoft.com/en-us/windows-hardware/manufacture/desktop/reagentc-command-line-options?view=windows-11
.NOTES
Detail on what the script does, if this is needed.
#>
[CmdletBinding(ConfirmImpact='High')]
Param (
[Parameter(
ValueFromPipeline,
ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[String]$LogPath = "C:\Temp"
)
begin {
# Add Logging block
try {
if (!("PSLogger" -as [type])) {
$callingScript = ($MyInvocation.MyCommand.Name) -split ('.ps1')
."\\server\path\here\Logging.ps1"
$logger = [PSLogger]::new($LogPath, $callingScript)
}
}
catch {
$PSCmdlet.ThrowTerminatingError($PSitem)
}
$logger.Notice("Starting $($MyInvocation.MyCommand) script")
# First block to add/change stuff in
try {
Write-Verbose -Message "Setting Error Action to Silently Continue"
$ErrorActionPreference = "SilentlyContinue"
Write-Verbose -Message "Creating and setting a environment variable for $([Environment]::SystemDirectory)"
$env:SystemDirectory = [Environment]::SystemDirectory
$xml = "$env:SystemDirectory\Recovery\ReAgent.xml"
$logger.Informational("Checking Windows Recovery Environment info...")
Write-Verbose -Message "Checking Windows Recovery Environment info..."
$analyzeReagentc = Invoke-Expression "$env:SystemDirectory\ReagentC.exe /info"
$analyzeReagentcEnabled = "$AnalyzeReagentC" -Match [regex]::new("Enabled")
$analyzeReagentcDisabled = "$AnalyzeReagentC" -Match [regex]::new("Disabled")
}
catch {
$logger.Error("$PSitem")
$PSCmdlet.ThrowTerminatingError($PSitem)
}
}
process {
try {
if ($analyzeReagentcEnabled) {
$logger.informational("Windows RE Status: Enabled")
$Status = "Enabled"
}
elseif ($analyzeReagentcDisabled) {
$Status = "Disabled"
Write-Verbose -Message "Enabling Windows Recovery Environment"
if (Test-Path -Path $xml) {
$logger.warning("Removing $xml")
Remove-Item -Path $xml
}
$enableWinRE = Invoke-Expression "$env:SystemDirectory\ReagentC.exe /enable"
$Status = "Enabled"
}
else {
$logger.warning("Unknown Windows RE Status")
$Status = "Unknown"
}
}
catch {
$logger.Error("$PSitem")
$PSCmdlet.ThrowTerminatingError($PSitem)
}
try {
$Partition = (Get-Partition -DiskNumber 0 | Where-Object {$_.type -match "Recovery"})
if ($Partition.count -gt 1) {
[string]$recoveryPartition = $analyzeReagentc | select-string -pattern "partition"
if(!([string]::IsNullOrWhiteSpace($recoveryPartition))){
if($recoveryPartition -match '(partition+\d)') {
$logger.informational("$($matches[0]) is the current recovery partition, removing non-used recovery partition")
Write-output "$($matches[0]) is the current recovery partition, removing non-used recovery partition"
if($matches[0] -match'(\d)') {
$Partition | Where-Object {$_.PartitionNumber -notcontains "$($matches[0])"} | Remove-Partition
$logger.informational("Removed non-used recovery partition")
}
}
}
}
}
catch {
$logger.Error("$PSitem")
$PSCmdlet.ThrowTerminatingError($PSitem)
}
[PSCustomObject]@{
WindowsRecoveryStatus = $Status
}
}
end {
$logger.Notice("Finished $($MyInvocation.MyCommand) script")
}
}