PortValhalla/scripts/Windows/Scripts/Context.ps1

100 lines
3.1 KiB
PowerShell
Raw Normal View History

class Context {
2023-06-21 20:04:18 +00:00
[string]$EntryPoint;
2023-06-16 18:24:37 +00:00
[string]$RootDir;
[string]$BackupName;
2023-06-22 20:56:43 +00:00
[string]$UserName;
2023-06-22 21:04:13 +00:00
[string]$AdminName = "Admin";
[string]$RunOnceName = "PortValhalla";
[string] BackupRoot() {
2023-06-16 18:24:37 +00:00
if (-not $this.RootDir)
{
return Join-Path $PSScriptRoot ".." ".." ".." "backup" $this.BackupName;
}
else
{
return $this.RootDir;
}
}
[string] ArchivePath($name) {
return Join-Path $this.BackupRoot() "$name.7z";
}
[string] FileArchivePath($name) {
return $this.ArchivePath($(Join-Path "Files" $name));
}
[string] SoftwareArchive([string]$softwareName) {
return $this.ArchivePath($softwareName);
}
[void] Backup([string]$sourcePath, [string]$archivePath, [string[]]$arguments) {
if (Test-Path $archivePath) {
Remove-Item -Recurse $archivePath;
}
Start-Process -WorkingDirectory "$sourcePath" `
-FilePath "7z" `
-ArgumentList (
@(
"a",
"-xr!desktop.ini",
2023-06-09 16:21:26 +00:00
"-xr!thumbs.db",
"-xr!Thumbs.db",
$archivePath) + $arguments) `
-Wait `
-NoNewWindow;
}
2023-06-16 18:24:22 +00:00
[void] Restore([string]$archivePath, [string]$destinationPath) {
if (-not (Test-Path -PathType Container $destinationPath)) {
New-Item -ItemType Directory "$destinationPath";
}
Start-Process -WorkingDirectory "$destinationPath" `
-FilePath "7z"
-ArgumentList "x" `
-Wait `
-NoNewWindow;
}
[string] GetTempDirectory() {
$tempDir = Join-Path $([System.IO.Path]::GetTempPath()) $([System.IO.Path]::GetRandomFileName());
$null = New-Item -ItemType Directory $tempDir;
return $tempDir;
}
2023-06-22 20:58:54 +00:00
[void] ProcessDefaultUserKey([System.Action[Microsoft.Win32.RegistryKey]] $action) {
$root = "HKLM:\DefaultUser";
$hivePath = "$env:SystemRoot\Profiles\Default User\NTUSER.dat"
$null = & reg load $root $hivePath;
$action.Invoke((Get-Item $root));
}
[Microsoft.Win32.RegistryKey] GetRunOnceKey([Microsoft.Win32.RegistryKey] $userKey) {
if (-not $userKey) {
$userKey = Get-Item "HKCU:\";
}
return Get-Item $userKey "$userKey\Software\Microsoft\Windows\CurrentVersion\RunOnce";
}
2023-06-25 13:05:59 +00:00
[void] RegisterReboot([Microsoft.Win32.RegistryKey] $userKey = $null) {
$null = New-ItemProperty -Path $this.GetRunOnceKey($userKey) -Name $this.RunOnceName -Value "pwsh `"$($this.EntryPoint)`"" -PropertyType ExpandString;
}
[void] RegisterNewUserReboot() {
$this.ProcessDefaultUserKey({ param ($root) $this.RegisterReboot($root); });
}
[void] DeregisterNewUserReboot() {
$this.ProcessDefaultUserKey({ param ($root) Remove-ItemProperty $this.GetRunOnceKey($root) -Name $this.RunOnceName });
}
2023-06-22 20:58:54 +00:00
[void] Reboot() {
Write-Host "Restarting Computer...";
$this.RegisterReboot();
2023-06-22 20:58:54 +00:00
Restart-Computer;
}
}