-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDriveMount.psm1
142 lines (113 loc) · 4.17 KB
/
DriveMount.psm1
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
Set-Variable -Option Constant -Scope Script -Name SCRIPT_SEND_SIGNAL -Value {
param (
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$SignalName,
[int]$WaitSeconds
)
Start-Sleep -Seconds $WaitSeconds
waitfor /si "${SignalName}"
}
<#
.SYNOPSIS
Mock implementation of drive mounting/dismounting.
.DESCRIPTION
Asynchronously sends the given signal after the given time has passed.
No other work is performed.
.NOTES
This function returns immediately as the signal is sent asynchronously.
#>
function Mock-DriveMountDismount {
param(
[Parameter(Mandatory)]
[ValidateLength(1,225)]
[string]$SignalName,
[int]$WaitSeconds = 5
)
Start-Job -ScriptBlock $SCRIPT_SEND_SIGNAL -ArgumentList "$SignalName", $WaitSeconds | Out-Null
}
<#
.SYNOPSIS
Mounts a given drive.
.DESCRIPTION
Asynchronously mounts a drive designated by an ID, and assign it a given label once mounted.
.NOTES
The function starts the mount procedure asynchronously and returns immediately.
The caller is notified of the completion of the mount procedure through a signal mechanism.
The signal can be listened to with `waitfor` ; see `waitfor` documentation for details.
.PARAMETER DriveId
The ID of the drive to mount.
.PARAMETER MountedDriveLabel
An arbitrary label to assign to the drive once opened/connected and mounted.
It shouldn't be longer than 32 or 11 characters for a drive hosting respectively an NTFS file system or an (ex)FAT file system.
.PARAMETER DriveMountedSignalName
The name of the signal sent to `waitfor` once the drive is mounted.
Must be a valid signal name as defined by `waitfor`, i.e. be <= 225 character long and only include [a-z][A-Z][0-9] or characters between EASCII codes 128 and 255 (if not, the behavior is undefined).
.EXAMPLE
PS> Mount-Drive -DriveId "ID000" -MountedDriveLabel "custom_label" -DriveMountedSignalName "customsignal0"
PS> waitfor /t 30 "customsignal0"
SUCCESS: Signal received.
PS> $?
True
.EXAMPLE
PS> Mount-Drive -DriveId "ID001" -MountedDriveLabel "custom_label" -DriveMountedSignalName "customsignal1"
PS> waitfor /t 30 "customsignal1"
ERROR: Timed out waiting for 'customsignal1'.
PS> $?
False
#>
function Mount-Drive {
param (
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$DriveId,
[Parameter(Mandatory)]
[ValidateLength(1,32)]
[string]$MountedDriveLabel,
[Parameter(Mandatory)]
[ValidateLength(1,225)]
[string]$DriveMountedSignalName
)
#TODO Implement the actual drive mounting.
Mock-DriveMountDismount "$DriveMountedSignalName"
}
<#
.SYNOPSIS
Dismounts a drive.
.DESCRIPTION
Asynchronously dismounts a mounted drive.
.NOTES
The function starts the dismount procedure asynchronously and returns immediately.
The caller is notified of the completion of the dismount procedure through a signal mechanism.
The signal can be listened to with `waitfor` ; see `waitfor` documentation for details.
.PARAMETER DriveLetter
The letter assigned to the drive to dismount.
It can optionally include the ':' or ':\' suffixes (i.e. "C", "C:" and "C:\" are all accepted).
.PARAMETER DriveDismountedSignalName
The name of the signal sent to `waitfor` once the drive is dismounted.
Must be a valid signal name as defined by `waitfor`, i.e. be <= 225 character long and only include [a-z][A-Z][0-9] or characters between EASCII codes 128 and 255 (if not, the behavior is undefined).
.EXAMPLE
PS> Dismount-Drive -DriveLetter "C" -DriveDismountedSignalName "customsignal0"
PS> waitfor /t 30 "customsignal0"
SUCCESS: Signal received.
PS> $?
True
.EXAMPLE
PS> Dismount-Drive -DriveLetter "Z" -DriveDismountedSignalName "customsignal1"
PS> waitfor /t 30 "customsignal1"
ERROR: Timed out waiting for 'customsignal1'.
PS> $?
False
#>
function Dismount-Drive {
param (
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$DriveLetter,
[Parameter(Mandatory)]
[ValidateLength(1,225)]
[string]$DriveDismountedSignalName
)
#TODO Implement the actual drive dismounting.
Mock-DriveMountDismount "$DriveDismountedSignalName"
}