PortValhalla/scripts/Windows/Scripts/Context.ps1

71 lines
2.1 KiB
PowerShell

class Context {
[string]$EntryPoint;
[string]$RootDir;
[string]$BackupName;
[string]$UserName;
[string]$AdminName = "Admin";
[string] BackupRoot() {
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",
"-xr!thumbs.db",
"-xr!Thumbs.db",
$archivePath) + $arguments) `
-Wait `
-NoNewWindow;
}
[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;
}
[void] Reboot() {
Write-Host "Restarting Computer...";
$null = New-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\RunOnce" -Name "PortValhalla" -Value "pwsh `"$($this.EntryPoint)`"" -PropertyType ExpandString;
Restart-Computer;
}
}