2023-06-06 22:56:29 +00:00
|
|
|
class Context {
|
2023-06-21 20:04:18 +00:00
|
|
|
[string]$EntryPoint;
|
2023-06-16 18:24:37 +00:00
|
|
|
[string]$RootDir;
|
2023-06-06 22:56:29 +00:00
|
|
|
[string]$BackupName;
|
2023-06-22 20:56:43 +00:00
|
|
|
[string]$UserName;
|
2023-06-22 21:04:13 +00:00
|
|
|
[string]$AdminName = "Admin";
|
2023-06-23 18:08:31 +00:00
|
|
|
[string]$RunOnceName = "PortValhalla";
|
2023-06-06 22:56:29 +00:00
|
|
|
[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;
|
|
|
|
}
|
2023-06-06 22:56:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[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);
|
|
|
|
}
|
2023-06-06 23:32:14 +00:00
|
|
|
|
|
|
|
[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",
|
2023-06-06 23:32:14 +00:00
|
|
|
$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;
|
|
|
|
}
|
2023-06-16 18:25:43 +00:00
|
|
|
|
|
|
|
[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
|
|
|
|
2023-06-23 18:08:31 +00:00
|
|
|
[void] ProcessDefaultUserKey([System.Action[Microsoft.Win32.RegistryKey]] $action) {
|
2023-06-28 11:40:56 +00:00
|
|
|
$rootPath = "HKLM:\DefaultUser";
|
|
|
|
$regRootPath = $rootPath.Replace(":", "");
|
2023-06-25 15:57:23 +00:00
|
|
|
$hivePath = "$env:SystemDrive\Users\Default\NTUSER.dat"
|
2023-06-28 11:40:56 +00:00
|
|
|
$null = & reg load $regRootPath $hivePath;
|
|
|
|
$root = Get-Item $rootPath;
|
|
|
|
$action.Invoke($root);
|
|
|
|
$root.Handle.Close();
|
|
|
|
[System.GC]::Collect();
|
|
|
|
& reg unload $regRootPath;
|
2023-06-23 18:08:31 +00:00
|
|
|
}
|
|
|
|
|
2023-06-25 16:25:52 +00:00
|
|
|
[void] ProcessLogonKey([System.Action[Microsoft.Win32.RegistryKey]] $action) {
|
|
|
|
$key = Get-Item "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Winlogon";
|
|
|
|
$action.Invoke($key);
|
|
|
|
}
|
|
|
|
|
2023-06-25 13:36:58 +00:00
|
|
|
[Microsoft.Win32.RegistryKey] GetRunOnceKey() {
|
|
|
|
return $this.GetRunOnceKey($null);
|
|
|
|
}
|
|
|
|
|
2023-06-23 18:08:31 +00:00
|
|
|
[Microsoft.Win32.RegistryKey] GetRunOnceKey([Microsoft.Win32.RegistryKey] $userKey) {
|
|
|
|
if (-not $userKey) {
|
|
|
|
$userKey = Get-Item "HKCU:\";
|
|
|
|
}
|
|
|
|
|
2023-06-25 14:33:04 +00:00
|
|
|
Push-Location $userKey.PSPath;
|
2023-06-25 16:05:55 +00:00
|
|
|
$runOncePath = "Software\Microsoft\Windows\CurrentVersion\RunOnce";
|
|
|
|
|
|
|
|
if (-not (Test-Path $runOncePath)) {
|
|
|
|
New-Item $runOncePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = Get-Item $runOncePath;
|
2023-06-25 14:28:12 +00:00
|
|
|
Pop-Location;
|
2023-06-25 14:54:10 +00:00
|
|
|
return $result;
|
2023-06-23 18:08:31 +00:00
|
|
|
}
|
|
|
|
|
2023-06-25 13:36:58 +00:00
|
|
|
[void] RegisterReboot() {
|
|
|
|
$this.RegisterReboot($null);
|
|
|
|
}
|
|
|
|
|
|
|
|
[void] RegisterReboot([Microsoft.Win32.RegistryKey] $userKey) {
|
2023-06-28 11:40:56 +00:00
|
|
|
$runOnceKey = $this.GetRunOnceKey($userKey);
|
2023-06-28 11:43:20 +00:00
|
|
|
Set-ItemProperty -Path $runOnceKey.PSPath -Name $this.RunOnceName -Value "pwsh `"$($this.EntryPoint)`"" -Type "ExpandString";
|
2023-06-28 11:40:56 +00:00
|
|
|
$runOnceKey.Handle.Close();
|
2023-06-23 18:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[void] RegisterNewUserReboot() {
|
|
|
|
$this.ProcessDefaultUserKey({ param ($root) $this.RegisterReboot($root); });
|
|
|
|
}
|
|
|
|
|
|
|
|
[void] DeregisterNewUserReboot() {
|
2023-06-25 16:10:23 +00:00
|
|
|
$this.ProcessDefaultUserKey({ param ($root) Remove-Item -Path $this.GetRunOnceKey($root).PSPath });
|
2023-06-23 18:08:31 +00:00
|
|
|
}
|
|
|
|
|
2023-06-25 16:25:52 +00:00
|
|
|
[void] SetAutologin($user, $pw) {
|
|
|
|
$this.ProcessLogonKey(
|
|
|
|
{
|
|
|
|
param ($logon)
|
|
|
|
$path = $logon.PSPath;
|
|
|
|
Set-ItemProperty $path -Name "AutoAdminLogon" 1;
|
|
|
|
Set-ItemProperty $path -Name "DefaultUserName" $user;
|
|
|
|
|
|
|
|
if ($password) {
|
|
|
|
Set-ItemProperty $path -Name "DefaultPassword" $password;
|
|
|
|
} else {
|
|
|
|
Remove-ItemProperty $path -Name "DefaultPassword";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
[void] RemoveAutologin() {
|
|
|
|
$this.ProcessLogonKey(
|
|
|
|
{
|
|
|
|
param ($logon)
|
|
|
|
$path = $logon.PSPath;
|
2023-06-25 16:48:13 +00:00
|
|
|
Set-ItemProperty $path -Name "AutoAdminLogon" 0;
|
2023-06-25 16:25:52 +00:00
|
|
|
Remove-ItemProperty $path -Name "DefaultDomainName";
|
|
|
|
Remove-ItemProperty $path -Name "DefaultUserName";
|
|
|
|
Remove-ItemProperty $path -Name "DefaultPassword";
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-06-22 20:58:54 +00:00
|
|
|
[void] Reboot() {
|
|
|
|
Write-Host "Restarting Computer...";
|
2023-06-23 18:08:31 +00:00
|
|
|
$this.RegisterReboot();
|
2023-06-22 20:58:54 +00:00
|
|
|
Restart-Computer;
|
2023-06-25 16:11:56 +00:00
|
|
|
exit;
|
2023-06-22 20:58:54 +00:00
|
|
|
}
|
2023-06-06 22:56:29 +00:00
|
|
|
}
|