-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnable-ShadowCopy.ps1
More file actions
103 lines (81 loc) · 3.72 KB
/
Copy pathEnable-ShadowCopy.ps1
File metadata and controls
103 lines (81 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<#
.SYNOPSIS
Richtet Schattenkopien fuer das Laufwerk C: ein, inklusive
Speicherzuweisung und Zeitplan.
.DESCRIPTION
Legt die Schattenkopie-Konfiguration fuer C: an, setzt die maximale
Groesse des Schattenspeichers und protokolliert die Schritte nach
C:\Install\shadowcopy.log.
Schattenkopien geben Benutzern ueber "Vorgaengerversionen" die
Moeglichkeit, geloeschte oder ueberschriebene Dateien selbst
wiederherzustellen - ohne Anfrage an die IT.
.NOTES
Erfordert administrative Rechte. Schattenkopien sind KEIN Ersatz fuer ein
Backup: sie liegen auf demselben Datentraeger und sind bei dessen Ausfall
ebenfalls weg.
Die Datei definiert eine eigene Write-Log-Funktion; dieser Name wird im
Repo von mehreren Dateien unabhaengig voneinander belegt.
#>
function Write-Log {
param([string]$Message)
$logPath = "C:\Install"
$logFile = "$logPath\shadowcopy.log"
if (-not (Test-Path $logPath)) {
New-Item -ItemType Directory -Path $logPath -Force | Out-Null
}
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
"$timestamp - $Message" | Out-File -FilePath $logFile -Encoding UTF8 -Append
}
function Enable-ShadowCopyC {
param(
[int]$MaxPercent = 10
)
# $volume = Get-CimInstance -Query "SELECT * FROM Win32_Volume WHERE DriveLetter = 'C:' AND FileSystem = 'NTFS'"
$volume = Get-CimInstance -Namespace root\cimv2 -ClassName Win32_Volume | Where-Object { $_.Name -eq "C:\" -and $_.FileSystem -eq "NTFS" }
if (-not $volume) {
Write-Log "C:\ nicht gefunden oder kein NTFS-Dateisystem. Abbruch."
return
}
$maxSizeBytes = [math]::Floor($volume.Capacity * ($MaxPercent / 100))
$existing = Get-CimInstance -Namespace root\cimv2 -ClassName Win32_ShadowStorage | Where-Object { $_.Volume.DeviceID -eq $volume.DeviceID }
if (-not $existing) {
Start-Process -Wait -FilePath "vssadmin.exe" -ArgumentList @(
"add shadowstorage",
"/for=C:",
"/on=C:",
"/maxsize=$([math]::Round($maxSizeBytes / 1MB))MB"
) -WindowStyle Hidden
Write-Log "Schattenkopie für C:\ aktiviert mit max. $MaxPercent% = $([math]::Round($maxSizeBytes / 1MB)) MB."
}
else {
Write-Log "Schattenkopien für C:\ waren bereits aktiv. Ändere auf $MaxPercent ..."
Start-Process -Wait -FilePath "vssadmin.exe" -ArgumentList @(
"resize shadowstorage",
"/for=C:",
"/on=C:",
"/maxsize=$([math]::Round($maxSizeBytes / 1MB))MB"
) -WindowStyle Hidden
Write-Log "Schattenkopie für C:\ geändert mit max. $MaxPercent% = $([math]::Round($maxSizeBytes / 1MB)) MB."
}
}
# Legt die maximale Größe der Schattenkopie fest und aktiviert sie
Enable-ShadowCopyC -MaxPercent 10
# Aktiviert den Systemschutz für C:\
Enable-ComputerRestore -Drive 'C:\':
# Erstellt einen initialen Wiederherstellungspunkt
if (-not (Get-ComputerRestorePoint -ErrorAction SilentlyContinue)) {
Write-Log "Erstelle initialen Wiederherstellungspunkt ..."
Checkpoint-Computer -Description 'Initialer Wiederherstellungspunkt' -RestorePointType 'MODIFY_SETTINGS' -ErrorAction Stop
Write-Log "Initialer Wiederherstellungspunkt erstellt."
}
else {
Write-Log "Initialer Wiederherstellungspunkt bereits vorhanden."
}
$myScriptPath = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
$myFilename = "Wiederherstellungspunkt_C_Täglich.xml"
$fullPath = Join-Path -Path $myScriptPath -ChildPath $myFilename
if (Test-Path -Path $fullPath -PathType Leaf) {
schtasks /create /tn "Wiederherstellungspunkt_C_Täglich" /xml $fullPath /f
}
# Erstellt eine Schattenkopie
# $r = ([WmiClass]'root\cimv2:Win32_ShadowCopy').Create('C:\', 'ClientAccessible')