-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRAMDisk.psm1
More file actions
77 lines (69 loc) · 2.46 KB
/
Copy pathRAMDisk.psm1
File metadata and controls
77 lines (69 loc) · 2.46 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
Set-StrictMode -Version Latest
function Test-IMDiskInstalled {
$command = Get-Command "imdisk.exe"
if ($command.CommandType -eq [Management.Automation.CommandTypes]::Application) {
Write-Output $true
} else {
Write-Output $false
}
}
function Test-ExitSuccess {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true,
HelpMessage="The name of the command executed.")]
[String]$Name
)
if ($LASTEXITCODE -ne 0) {
Write-Error "$Name failed with code: $LASTEXITCODE"
return $false
}
return $true
}
function Remove-RAMDisk {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true,
HelpMessage="The drive letter to which to mount the RAMDisk.")]
[String]$DriveLetter
)
if (!(Test-IMDiskInstalled)) {
throw "IMDisk is not installed. With chocolatey, install via: choco install imdisk"
}
& imdisk.exe -D -m "${DriveLetter}:"
return (Test-ExitSuccess -Name "imdisk.exe")
}
function New-RAMDisk {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true,
Position=0,
HelpMessage="The drive letter to which to mount the RAMDisk.")]
[String]$DriveLetter,
[Parameter(Mandatory=$true,
Position=1,
HelpMessage="The size of the RAMDisk, suffixed with units: b (512-byte blocks), k, m, g, t, K, M, G, or T")]
[String]$Size,
[Parameter(Mandatory=$false,
Position=2,
HelpMessage="The filesystem type to pass to the format command: ntfs, fat, fat32, ...")]
[String]$FSType = "ntfs",
[Parameter(Mandatory=$false,
Position=3,
HelpMessage="The label to use for the filesystem.")]
[String]$FSLabel = "RAMDisk"
)
if (!(Test-IMDiskInstalled)) {
throw "IMDisk is not installed. With chocolatey, install via: choco install imdisk"
}
# Could pass format args using -p, but then string parsing prevents us from having spaces in the disk label.
& imdisk.exe -a -s $Size -m "${DriveLetter}:"
if (Test-ExitSuccess -Name "imdisk.exe") {
& format.com "${DriveLetter}:" "/fs:$FSType" "/v:$FSLabel" "/q" "/y"
Write-Output (Test-ExitSuccess -Name "format.com")
} else {
Write-Output $false
}
}
Export-ModuleMember -Function Remove-RAMDisk
Export-ModuleMember -Function New-RAMDisk