forked from JohnDuprey/PSWindowsImageRepair
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Optimize-ComponentStore.ps1
69 lines (59 loc) · 2.08 KB
/
Optimize-ComponentStore.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
function Optimize-ComponentStore {
<#
.SYNOPSIS
Analyze and cleanup the Component Store
.DESCRIPTION
Analyze the Component Store and start a cleanup procedure if it needs it.
Windows server 2022 has StartComponentCleanup as a parameter now for Repair-WindowsImage.
.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
Description of objects that are output by the script.
.EXAMPLE
Optimize-ComponentStore
.LINK
Links to further documentation.
.NOTES
Detail on what the script does, if this is needed.
#>
[CmdletBinding()]
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")
}
process {
try {
$logger.Warning("Cleaning Up Windows Component Store")
Write-Warning -Message "Cleaning Up Windows Component Store"
$StartComponentCleanup = Invoke-Expression "C:\Windows\system32\Dism.exe /Online /Cleanup-Image /StartComponentCleanup"
$logger.Informational(" $StartComponentCleanup")
$StartComponentCleanup
}
catch {
$logger.Error("$PSitem")
$PSCmdlet.ThrowTerminatingError($PSitem)
}
}
end {
$logger.Notice("Finished $($MyInvocation.MyCommand) script")
}
}