Files
syno-scripts/Powershell/VEEAM/Modules/part2.ps1
T

346 lines
5.4 KiB
PowerShell
Executable File

############################################################
# PART 2
#
# Resume Manager
# Statusverwaltung
# Pending Reboot
# Registry Funktionen
############################################################
############################################################
# REGISTRY
############################################################
function Test-RegistryValue {
param(
[string]$Path,
[string]$Name
)
try{
Get-ItemProperty `
-Path $Path `
-Name $Name `
-ErrorAction Stop | Out-Null
return $true
}
catch{
return $false
}
}
function Get-RegistryValue {
param(
[string]$Path,
[string]$Name
)
try{
(Get-ItemProperty `
-Path $Path `
-Name $Name `
-ErrorAction Stop).$Name
}
catch{
return $null
}
}
############################################################
# STATUSDATEI
############################################################
function Save-State {
param(
[string]$Step
)
$State = [PSCustomObject]@{
Version = $Version
Computer = $env:COMPUTERNAME
Date = Get-Date
Step = $Step
}
$State |
ConvertTo-Json |
Set-Content `
-Path $StateFile `
-Encoding UTF8
Write-Log "Status gespeichert ($Step)"
}
############################################################
function Load-State {
if(!(Test-Path $StateFile))
{
return $null
}
try{
Get-Content `
$StateFile `
-Raw |
ConvertFrom-Json
}
catch{
return $null
}
}
############################################################
function Remove-State {
if(Test-Path $StateFile)
{
Remove-Item `
$StateFile `
-Force
Write-Log "Statusdatei entfernt"
}
}
############################################################
# RESUME FLAG
############################################################
function Set-ResumeFlag {
New-Item `
-ItemType File `
-Path $ResumeFlag `
-Force |
Out-Null
Write-Log "Resume Flag gesetzt"
}
############################################################
function Clear-ResumeFlag {
if(Test-Path $ResumeFlag)
{
Remove-Item `
$ResumeFlag `
-Force
Write-Log "Resume Flag entfernt"
}
}
############################################################
# PENDING REBOOT
############################################################
function Test-PendingReboot {
Write-Log "Prüfe Pending Reboot"
$Pending = $false
if(Test-Path `
"HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending")
{
$Pending = $true
}
if(Test-Path `
"HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired")
{
$Pending = $true
}
if(Test-RegistryValue `
"HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager" `
"PendingFileRenameOperations")
{
$Pending = $true
}
if($Pending)
{
Write-Log "Neustart erforderlich"
}
else
{
Write-Log "Kein Neustart erforderlich"
}
return $Pending
}
############################################################
# SCHEDULED TASK
############################################################
function Register-ResumeTask {
Write-Log "Erstelle Resume Task"
$Action = New-ScheduledTaskAction `
-Execute "powershell.exe" `
-Argument "-ExecutionPolicy Bypass -File `"$PSCommandPath`" -Resume"
$Trigger = New-ScheduledTaskTrigger `
-AtStartup
Register-ScheduledTask `
-TaskName "VeeamInstallerResume" `
-Action $Action `
-Trigger $Trigger `
-RunLevel Highest `
-Force |
Out-Null
}
############################################################
function Remove-ResumeTask {
if(Get-ScheduledTask `
-TaskName "VeeamInstallerResume" `
-ErrorAction SilentlyContinue)
{
Unregister-ScheduledTask `
-TaskName "VeeamInstallerResume" `
-Confirm:$false
Write-Log "Resume Task entfernt"
}
}
############################################################
# RESTART
############################################################
function Restart-AndResume {
Write-Log "Automatischer Neustart"
Save-State `
-Step "Resume"
Set-ResumeFlag
Register-ResumeTask
Restart-Computer `
-Force
exit
}
############################################################
# RESUME
############################################################
function Resume-Installation {
if(!(Test-Path $ResumeFlag))
{
return
}
Write-Log "Installation wird fortgesetzt"
$State = Load-State
if($null -eq $State)
{
Stop-Installation "Statusdatei beschädigt."
}
Remove-ResumeTask
Clear-ResumeFlag
Remove-State
Write-Log "Fortsetzung abgeschlossen"
}
############################################################
# STARTPARAMETER
############################################################
param(
[switch]$Resume
)
if($Resume)
{
Resume-Installation
}