-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNew-ShadowCopyConfig.ps1
111 lines (96 loc) · 3.95 KB
/
New-ShadowCopyConfig.ps1
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
104
105
106
107
108
109
110
111
Function New-ShadowCopyConfig
{
<#
.Synopsis
Creates a shadowstorage object on the target machine and schedules the default
0700 and 1200 Volume Shadow Copies
.DESCRIPTION
Long description
.EXAMPLE
Example of how to use this cmdlet
.EXAMPLE
Another example of how to use this cmdlet
.NOTES
TODO: Find and correct the issue leading to the volume appearing to be disabled for shadowcopies in the GUI
#>
[CmdletBinding()]
Param
(
# Param1 help description
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[string]
$ComputerName = $env:COMPUTERNAME,
# Path of volume to protect with a new shadow copy schedule
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=1)]
[string]
$Path,
# Path of volume to protect with a new shadow copy schedule
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$true,
Position=2)]
[string]
$StoragePath = $Path
)
Begin
{
}
Process
{
# Try - Do I wrap the whole thing in a try based on the success or failure of the
# new CIMSession? or catch right away and do a throw? I think the latter.
$cs = New-CimSession -ComputerName $ComputerName
$Volumes = Get-CimInstance -Class Win32_Volume -CimSession $cs
$DiffVolume = $Volumes | Where Name -EQ $StoragePath
$PathDriveLetter = $Path[0]
$ShareVolumeCount = ($Volumes | Where Name -Like "$PathDriveLetter*").count
$TargetVolume = $Volumes | Where Name -Like "*$Path*"
# Calculate / Choose MaxSpace
if (($TargetVolume.Capacity / 10) -gt ($DiffVolume.Capacity / $ShareVolumeCount))
{
$MaxSpace = ($DiffVolume.Capacity / $ShareVolumeCount)
}
Else
{
$MaxSpace = ($TargetVolume.Capacity / 10)
}
[Uint64]$MaxSpace = [math]::Round($MaxSpace,0)
Write-Verbose "MaxSize will be $MaxSpace" -Verbose
$ShadowStorageParam = @{Volume = $TargetVolume.Name
DiffVolume = $DiffVolume.Name
MaxSpace = $MaxSpace}
# Create the ShadowStorage
Invoke-CimMethod -ClassName Win32_ShadowStorage -MethodName Create -CimSession $cs -Arguments $ShadowStorageParam
Write-Verbose "Shadow Storage created" -Verbose
# Schedule the Shadow Copies
$ActionParams = @{Execute = "C:\Windows\system32\vssadmin.exe"
Argument = "Create Shadow /AutoRetry=15 /For=$($TargetVolume.DeviceID)"
WorkingDirectory = "%systemroot%\system32"
}
# Need to add an author if possible AND RunWithHighestPrivileges
# Uncheck the AC power only settings
$TaskAction = New-ScheduledTaskAction @ActionParams
$TaskTriggerAM = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Monday, Tuesday, Wednesday, Thursday, Friday -At (get-date 07:00:01)
$TaskTriggerPM = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Monday, Tuesday, Wednesday, Thursday, Friday -At (get-date 12:00:01)
$Principal = New-ScheduledTaskPrincipal "System"
$TaskSettings = New-ScheduledTaskSettingsSet
$TaskName = "ShadowCopyVolume$($TargetVolume.DeviceID.TrimStart('\\?\Volume').Trim('\') )"
Register-ScheduledTask -TaskName $TaskName `
-Action $TaskAction `
-Principal $Principal `
-Trigger $TaskTriggerAM, $TaskTriggerPM `
-Settings $TaskSettings `
-CimSession $cs
# IF things go wrong creating the task, check HKLM\Software\Microsoft\Windows NT\CurrentVersion\Schedule\TaskCache\Tasks and \Tree
Write-Verbose "Scheduled Task created" -Verbose
Remove-CimSession $cs | Out-Null
# For Testing:
# vssadmin delete shadowstorage /for=<path> [/on=<path>] [/quiet]
}
End
{
}
}