Add code for cleaning up after installations
This commit is contained in:
parent
e806e0cb2a
commit
9c872432a8
3 changed files with 70 additions and 16 deletions
|
@ -5,6 +5,7 @@ using namespace System.Security.Principal;
|
||||||
enum WindowsInstallerStage {
|
enum WindowsInstallerStage {
|
||||||
Initialize
|
Initialize
|
||||||
Run
|
Run
|
||||||
|
Cleanup
|
||||||
Completed
|
Completed
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -27,6 +27,35 @@ $null = New-Module {
|
||||||
Set-SetupOption $taskOption ([string]$Task);
|
Set-SetupOption $taskOption ([string]$Task);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
<#
|
||||||
|
.SYNOPSIS
|
||||||
|
Gets the PowerShell modules required for operating.
|
||||||
|
#>
|
||||||
|
function Get-RequiredModules {
|
||||||
|
$modules = @(
|
||||||
|
@("PSScriptAnalyzer")
|
||||||
|
) + (& {
|
||||||
|
if (-not $IsWindows) {
|
||||||
|
@()
|
||||||
|
} else {
|
||||||
|
@(
|
||||||
|
@("KnownFolders"),
|
||||||
|
@("PSWindowsUpdate"),
|
||||||
|
@("LocalAccounts", $true),
|
||||||
|
@("NuGet")
|
||||||
|
)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
for ($i = 0; $i -lt $modules.Count; $i++) {
|
||||||
|
if ($modules[$i] -is [string]) {
|
||||||
|
$modules[$i] = @($modules[$i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $modules;
|
||||||
|
}
|
||||||
|
|
||||||
function Start-Operation {
|
function Start-Operation {
|
||||||
param(
|
param(
|
||||||
[switch] $NonInteractive,
|
[switch] $NonInteractive,
|
||||||
|
@ -215,22 +244,7 @@ $null = New-Module {
|
||||||
}
|
}
|
||||||
|
|
||||||
Invoke-Hook "Install-PSModules" -Fallback {
|
Invoke-Hook "Install-PSModules" -Fallback {
|
||||||
$modules = @(
|
foreach ($module in (Get-RequiredModules)) {
|
||||||
@("PSScriptAnalyzer")
|
|
||||||
) + (& {
|
|
||||||
if (-not $IsWindows) {
|
|
||||||
@()
|
|
||||||
} else {
|
|
||||||
@(
|
|
||||||
@("KnownFolders"),
|
|
||||||
@("PSWindowsUpdate"),
|
|
||||||
@("LocalAccounts", $true),
|
|
||||||
@("NuGet")
|
|
||||||
)
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
foreach ($module in $modules) {
|
|
||||||
$parameters = @{ };
|
$parameters = @{ };
|
||||||
|
|
||||||
if ($module -is [string]) {
|
if ($module -is [string]) {
|
||||||
|
@ -403,6 +417,14 @@ $null = New-Module {
|
||||||
function Clear-OperationResources {
|
function Clear-OperationResources {
|
||||||
if ($IsWindows) {
|
if ($IsWindows) {
|
||||||
Uninstall-WslDistribution;
|
Uninstall-WslDistribution;
|
||||||
|
$null = Uninstall-Package Selenium.WebDriver -ErrorAction Continue;
|
||||||
|
Uninstall-ChocoPackage 7zip.portable gsudo selenium-gecko-driver yq;
|
||||||
|
Uninstall-WingetPackage AutoHotkey.AutoHotkey;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($module in (Get-RequiredModules)) {
|
||||||
|
Remove-Module -Force $module[0] -ErrorAction SilentlyContinue;
|
||||||
|
Uninstall-Module -Force -Name $module[0] -ErrorAction SilentlyContinue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -21,6 +21,7 @@ $null = New-Module {
|
||||||
. "$PSScriptRoot/../Types/WindowsInstallerAction.ps1";
|
. "$PSScriptRoot/../Types/WindowsInstallerAction.ps1";
|
||||||
. "$PSScriptRoot/../../Common/Scripts/Config.ps1";
|
. "$PSScriptRoot/../../Common/Scripts/Config.ps1";
|
||||||
. "$PSScriptRoot/../../Common/Scripts/Operations.ps1";
|
. "$PSScriptRoot/../../Common/Scripts/Operations.ps1";
|
||||||
|
. "$PSScriptRoot/../../Common/Scripts/Scripting.ps1";
|
||||||
. "$PSScriptRoot/../../Common/Scripts/Software.ps1";
|
. "$PSScriptRoot/../../Common/Scripts/Software.ps1";
|
||||||
. "$PSScriptRoot/../../Common/Scripts/SoftwareManagement.ps1";
|
. "$PSScriptRoot/../../Common/Scripts/SoftwareManagement.ps1";
|
||||||
. "$PSScriptRoot/../../Common/Types/InstallerAction.ps1";
|
. "$PSScriptRoot/../../Common/Types/InstallerAction.ps1";
|
||||||
|
@ -130,7 +131,37 @@ $null = New-Module {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Set-Stage ([WindowsInstallerStage]::Cleanup);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
([WindowsInstallerStage]::Cleanup) {
|
||||||
|
$taskName = "PortValhalla Cleaner";
|
||||||
|
$setupUser = Get-SetupUser;
|
||||||
|
Clear-OperationResources;
|
||||||
|
Remove-Item -Recurse -Force "C:\ProgramData\PortValhalla";
|
||||||
|
Get-SetupConfigKey | Remove-Item -Recurse -Force;
|
||||||
|
Disable-Autologin;
|
||||||
|
Disable-LocalUser $setupUser;
|
||||||
|
|
||||||
|
$script = {
|
||||||
|
param(
|
||||||
|
$TaskName,
|
||||||
|
$UserName
|
||||||
|
)
|
||||||
|
|
||||||
|
$user = Get-LocalUser $UserName;
|
||||||
|
[string] $sid = $user.SID;
|
||||||
|
Remove-LocalUser $user;
|
||||||
|
Get-CimInstance Win32_UserProfile | Where-Object { $_.SID -eq $sid } | Remove-CimInstance;
|
||||||
|
Unregister-ScheduledTask -Confirm:$false $TaskName;
|
||||||
|
};
|
||||||
|
|
||||||
|
$trigger = New-ScheduledTaskTrigger -AtStartup;
|
||||||
|
$task = New-ScheduledTaskAction -Execute "pwsh" -Argument "-Command & { $script } $(ConvertTo-Injection $taskName) $(ConvertTo-Injection $setupUser)";
|
||||||
|
$null = Register-ScheduledTask -Force $taskName -Action $task -Trigger $trigger -RunLevel Highest -User "SYSTEM";
|
||||||
Set-Stage ([WindowsInstallerStage]::Completed);
|
Set-Stage ([WindowsInstallerStage]::Completed);
|
||||||
|
Enable-UAC;
|
||||||
|
Restart-Intermediate -NoRegister;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue