-
Notifications
You must be signed in to change notification settings - Fork 52
/
StorageDsc.VirtualHardDisk.Win32Helpers.psm1
678 lines (558 loc) · 21.4 KB
/
StorageDsc.VirtualHardDisk.Win32Helpers.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
$modulePath = Join-Path -Path (Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent) -ChildPath 'Modules'
Import-Module -Name (Join-Path -Path $modulePath -ChildPath 'DscResource.Common')
# Import Localization Strings
$script:localizedData = Get-LocalizedData -DefaultUICulture 'en-US'
<#
.SYNOPSIS
Returns C# code that will be used to call Dev Drive related Win32 apis
#>
function Get-VirtDiskWin32HelperScript
{
[CmdletBinding()]
[OutputType([System.Void])]
param
()
$virtDiskDefinitions = @'
// https://learn.microsoft.com/en-us/windows/win32/api/virtdisk/ns-virtdisk-virtual_storage_type
[StructLayout(LayoutKind.Sequential)]
public struct VIRTUAL_STORAGE_TYPE
{
public UInt32 DeviceId;
public Guid VendorId;
}
// https://learn.microsoft.com/en-us/windows/win32/api/virtdisk/ns-virtdisk-create_virtual_disk_parameters
[StructLayout(LayoutKind.Sequential)]
public struct CREATE_VIRTUAL_DISK_PARAMETERS
{
public UInt32 Version;
public Guid UniqueId;
public UInt64 MaximumSize;
public UInt32 BlockSizeInBytes;
public UInt32 SectorSizeInBytes;
[MarshalAs(UnmanagedType.LPWStr)]
public string ParentPath;
[MarshalAs(UnmanagedType.LPWStr)]
public string SourcePath;
}
// https://learn.microsoft.com/en-us/windows/win32/api/virtdisk/ns-virtdisk-attach_virtual_disk_parameters
[StructLayout(LayoutKind.Sequential)]
public struct ATTACH_VIRTUAL_DISK_PARAMETERS
{
public UInt32 Version;
public UInt32 Reserved;
}
// https://learn.microsoft.com/en-us/windows/win32/api/virtdisk/ns-virtdisk-open_virtual_disk_parameters
[StructLayout(LayoutKind.Sequential)]
public struct OPEN_VIRTUAL_DISK_PARAMETERS
{
public UInt32 Version;
public UInt32 RWDepth;
}
// Define structures and constants for creating a virtual disk.
// https://learn.microsoft.com/en-us/windows/win32/api/virtdisk/ne-virtdisk-create_virtual_disk_version
public static uint CREATE_VIRTUAL_DISK_VERSION_2 = 2;
// https://learn.microsoft.com/en-us/windows/win32/api/virtdisk/ne-virtdisk-virtual_disk_access_mask-r1
public static uint VIRTUAL_DISK_ACCESS_NONE = 0;
public static uint VIRTUAL_DISK_ACCESS_ALL = 0x003f0000;
// https://learn.microsoft.com/en-us/windows/win32/api/virtdisk/ne-virtdisk-create_virtual_disk_flag
public static uint CREATE_VIRTUAL_DISK_FLAG_NONE = 0x0;
public static uint CREATE_VIRTUAL_DISK_FLAG_FULL_PHYSICAL_ALLOCATION = 0x1;
// Define structures and constants for attaching a virtual disk.
// https://learn.microsoft.com/en-us/windows/win32/api/virtdisk/ne-virtdisk-attach_virtual_disk_flag
public static uint ATTACH_VIRTUAL_DISK_FLAG_PERMANENT_LIFETIME = 0x00000004;
public static uint ATTACH_VIRTUAL_DISK_FLAG_AT_BOOT = 0x00000400;
// https://learn.microsoft.com/en-us/windows/win32/api/virtdisk/ne-virtdisk-attach_virtual_disk_version
public static uint ATTACH_VIRTUAL_DISK_VERSION_1 = 1;
// Define structures and constants for opening a virtual disk.
// https://learn.microsoft.com/en-us/windows/win32/api/virtdisk/ne-virtdisk-open_virtual_disk_version
public static uint OPEN_VIRTUAL_DISK_VERSION_1 = 1;
// https://learn.microsoft.com/en-us/windows/win32/api/virtdisk/ne-virtdisk-open_virtual_disk_flag
public static uint OPEN_VIRTUAL_DISK_FLAG_NONE = 0x0;
// Constants found in virtdisk.h
// https://learn.microsoft.com/en-us/windows/win32/api/virtdisk/ns-virtdisk-virtual_storage_type
public static uint VIRTUAL_STORAGE_TYPE_DEVICE_VHD = 2U;
public static uint VIRTUAL_STORAGE_TYPE_DEVICE_VHDX = 3U;
public static Guid VIRTUAL_STORAGE_TYPE_VENDOR_MICROSOFT = new Guid(0xEC984AEC, 0xA0F9, 0x47E9, 0x90, 0x1F, 0x71, 0x41, 0x5A, 0x66, 0x34, 0x5B);
// Declare method to create a virtual disk
// https://learn.microsoft.com/en-us/windows/win32/api/virtdisk/nf-virtdisk-createvirtualdisk
[DllImport("virtdisk.dll", CharSet = CharSet.Unicode)]
public static extern Int32 CreateVirtualDisk(
ref VIRTUAL_STORAGE_TYPE VirtualStorageType,
string Path,
UInt32 VirtualDiskAccessMask,
IntPtr SecurityDescriptor,
UInt32 Flags,
UInt32 ProviderSpecificFlags,
ref CREATE_VIRTUAL_DISK_PARAMETERS Parameters,
IntPtr Overlapped,
out SafeFileHandle Handle
);
// Declare method to attach a virtual disk
// https://learn.microsoft.com/en-us/windows/win32/api/virtdisk/nf-virtdisk-attachvirtualdisk
[DllImport("virtdisk.dll", CharSet = CharSet.Unicode)]
public static extern Int32 AttachVirtualDisk(
SafeFileHandle VirtualDiskHandle,
IntPtr SecurityDescriptor,
UInt32 Flags,
UInt32 ProviderSpecificFlags,
ref ATTACH_VIRTUAL_DISK_PARAMETERS Parameters,
IntPtr Overlapped
);
// Declare function to open a handle to a virtual disk
// https://learn.microsoft.com/en-us/windows/win32/api/virtdisk/nf-virtdisk-openvirtualdisk
[DllImport("virtdisk.dll", CharSet = CharSet.Unicode)]
public static extern Int32 OpenVirtualDisk(
ref VIRTUAL_STORAGE_TYPE VirtualStorageType,
string Path,
UInt32 VirtualDiskAccessMask,
UInt32 Flags,
ref OPEN_VIRTUAL_DISK_PARAMETERS Parameters,
out SafeFileHandle Handle
);
'@
if (([System.Management.Automation.PSTypeName]'VirtDisk.Helper').Type)
{
$script:VirtDiskHelper = ([System.Management.Automation.PSTypeName]'VirtDisk.Helper').Type
}
else
{
$script:VirtDiskHelper = Add-Type `
-Namespace 'VirtDisk' `
-Name 'Helper' `
-MemberDefinition $virtDiskDefinitions `
-UsingNamespace `
'System.ComponentModel',
'Microsoft.Win32.SafeHandles'
}
return $script:VirtDiskHelper
} # end function Get-VirtDiskWin32HelperScript
<#
.SYNOPSIS
Calls Win32 CreateVirtualDisk api. This is used so we can mock this call
easier.
.PARAMETER VirtualStorageType
Specifies the type and provider (vendor) of the virtual storage device.
.PARAMETER VirtualDiskPath
Specifies the whole path to the virtual disk file.
.PARAMETER AccessMask
Specifies the bitmask for specifying access rights to a virtual hard disk.
.PARAMETER SecurityDescriptor
Specifies the security information associated with the virtual disk.
.PARAMETER Flags
Specifies creation flags for the virtual disk.
.PARAMETER ProviderSpecificFlags
Specifies flags specific to the type of virtual disk being created.
.PARAMETER CreateVirtualDiskParameters
Specifies the virtual hard disk creation parameters, providing control over,
and information about, the newly created virtual disk.
.PARAMETER Overlapped
Specifies the reference to an overlapped structure for asynchronous calls.
.PARAMETER Handle
Specifies the reference to handle object that represents the newly created virtual disk.
#>
function New-VirtualDiskUsingWin32
{
[CmdletBinding()]
[OutputType([System.Int32])]
param
(
[Parameter(Mandatory = $true)]
[ref]
$VirtualStorageType,
[Parameter(Mandatory = $true)]
[System.String]
$VirtualDiskPath,
[Parameter(Mandatory = $true)]
[UInt32]
$AccessMask,
[Parameter(Mandatory = $true)]
[System.IntPtr]
$SecurityDescriptor,
[Parameter(Mandatory = $true)]
[UInt32]
$Flags,
[Parameter(Mandatory = $true)]
[System.UInt32]
$ProviderSpecificFlags,
[Parameter(Mandatory = $true)]
[ref]
$CreateVirtualDiskParameters,
[Parameter(Mandatory = $true)]
[System.IntPtr]
$Overlapped,
[Parameter(Mandatory = $true)]
[ref]
$Handle
)
$helper = Get-VirtDiskWin32HelperScript
return $helper::CreateVirtualDisk(
$virtualStorageType,
$VirtualDiskPath,
$AccessMask,
$SecurityDescriptor,
$Flags,
$ProviderSpecificFlags,
$CreateVirtualDiskParameters,
$Overlapped,
$Handle)
} # end function New-VirtualDiskUsingWin32
<#
.SYNOPSIS
Calls Win32 AttachVirtualDisk api. This is used so we can mock this call
easier.
.PARAMETER Handle
Specifies the reference to a handle to a virtual disk file.
.PARAMETER SecurityDescriptor
Specifies the security information associated with the virtual disk.
.PARAMETER Flags
Specifies attachment flags for the virtual disk.
.PARAMETER ProviderSpecificFlags
Specifies flags specific to the type of virtual disk being created.
.PARAMETER AttachVirtualDiskParameters
Specifies the virtual hard disk attach request parameters.
.PARAMETER Overlapped
Specifies the reference to an overlapped structure for asynchronous calls.
#>
function Add-VirtualDiskUsingWin32
{
[CmdletBinding()]
[OutputType([System.Int32])]
param
(
[Parameter(Mandatory = $true)]
[ref]
$Handle,
[Parameter(Mandatory = $true)]
[System.IntPtr]
$SecurityDescriptor,
[Parameter(Mandatory = $true)]
[System.UInt32]
$Flags,
[Parameter(Mandatory = $true)]
[System.Int32]
$ProviderSpecificFlags,
[Parameter(Mandatory = $true)]
[ref]
$AttachVirtualDiskParameters,
[Parameter(Mandatory = $true)]
[System.IntPtr]
$Overlapped
)
$helper = Get-VirtDiskWin32HelperScript
return $helper::AttachVirtualDisk(
$Handle.Value,
$SecurityDescriptor,
$Flags,
$ProviderSpecificFlags,
$AttachVirtualDiskParameters,
$Overlapped)
} # end function Add-VirtualDiskUsingWin32
<#
.SYNOPSIS
Calls Win32 OpenVirtualDisk api. This is used so we can mock this call
easier.
.PARAMETER VirtualStorageType
Specifies the type and provider (vendor) of the virtual storage device.
.PARAMETER VirtualDiskPath
Specifies the whole path to the virtual disk file.
.PARAMETER AccessMask
Specifies the bitmask for specifying access rights to a virtual hard disk.
.PARAMETER Flags
Specifies Open virtual disk flags for the virtual disk.
.PARAMETER CreateVirtualDiskParameters
Specifies the virtual hard disk open request parameters.
.PARAMETER Handle
Specifies the reference to handle object that represents the a virtual disk file.
#>
function Get-VirtualDiskUsingWin32
{
[CmdletBinding()]
[OutputType([System.Int32])]
param
(
[Parameter(Mandatory = $true)]
[ref]
$VirtualStorageType,
[Parameter(Mandatory = $true)]
[System.String]
$VirtualDiskPath,
[Parameter(Mandatory = $true)]
[System.UInt32]
$AccessMask,
[Parameter(Mandatory = $true)]
[System.UInt32]
$Flags,
[Parameter(Mandatory = $true)]
[ref]
$OpenVirtualDiskParameters,
[Parameter(Mandatory = $true)]
[ref]
$Handle
)
$helper = Get-VirtDiskWin32HelperScript
return $helper::OpenVirtualDisk(
$VirtualStorageType,
$VirtualDiskPath,
$AccessMask,
$Flags,
$OpenVirtualDiskParameters,
$Handle)
} # end function Get-VirtualDiskUsingWin32
<#
.SYNOPSIS
Creates and attaches a virtual disk to the system.
.PARAMETER VirtualDiskPath
Specifies the whole path to the virtual disk file.
.PARAMETER DiskSizeInBytes
Specifies the size of new virtual disk in bytes.
.PARAMETER DiskFormat
Specifies the supported virtual disk format.
.PARAMETER DiskType
Specifies the supported virtual disk type.
#>
function New-SimpleVirtualDisk
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$VirtualDiskPath,
[Parameter(Mandatory = $true)]
[System.UInt64]
$DiskSizeInBytes,
[Parameter(Mandatory = $true)]
[ValidateSet('Vhd', 'Vhdx')]
[System.String]
$DiskFormat,
[Parameter(Mandatory = $true)]
[ValidateSet('Fixed', 'Dynamic')]
[System.String]
$DiskType
)
Write-Verbose -Message ($script:localizedData.CreatingVirtualDiskMessage -f $VirtualDiskPath)
$vDiskHelper = Get-VirtDiskWin32HelperScript
# Get parameters for CreateVirtualDisk function
[ref]$virtualStorageType = Get-VirtualStorageType -DiskFormat $DiskFormat
[ref]$createVirtualDiskParameters = New-Object VirtDisk.Helper+CREATE_VIRTUAL_DISK_PARAMETERS
$createVirtualDiskParameters.Value.Version = [VirtDisk.Helper]::CREATE_VIRTUAL_DISK_VERSION_2
$createVirtualDiskParameters.Value.MaximumSize = $DiskSizeInBytes
$securityDescriptor = [System.IntPtr]::Zero
$accessMask = [VirtDisk.Helper]::VIRTUAL_DISK_ACCESS_NONE
$providerSpecificFlags = 0
# Handle to the new virtual disk
[ref]$handle = [Microsoft.Win32.SafeHandles.SafeFileHandle]::Zero
# Virtual disk will be dynamically expanding, up to the size of $DiskSizeInBytes on the parent disk
$flags = [VirtDisk.Helper]::CREATE_VIRTUAL_DISK_FLAG_NONE
if ($DiskType -eq 'Fixed')
{
# Virtual disk will be fixed, and will take up the up the full size of $DiskSizeInBytes on the parent disk after creation
$flags = [VirtDisk.Helper]::CREATE_VIRTUAL_DISK_FLAG_FULL_PHYSICAL_ALLOCATION
}
try
{
$result = New-VirtualDiskUsingWin32 `
-VirtualStorageType $virtualStorageType `
-VirtualDiskPath $VirtualDiskPath `
-AccessMask $accessMask `
-SecurityDescriptor $securityDescriptor `
-Flags $flags `
-ProviderSpecificFlags $providerSpecificFlags `
-CreateVirtualDiskParameters $createVirtualDiskParameters `
-Overlapped ([System.IntPtr]::Zero) `
-Handle $handle
if ($result -ne 0)
{
$win32Error = [System.ComponentModel.Win32Exception]::new($result)
throw [System.Exception]::new( `
($script:localizedData.CreateVirtualDiskError -f $VirtualDiskPath, $win32Error.Message), `
$win32Error)
}
Write-Verbose -Message ($script:localizedData.VirtualDiskCreatedSuccessfully -f $VirtualDiskPath)
Add-SimpleVirtualDisk `
-VirtualDiskPath $VirtualDiskPath `
-DiskFormat $DiskFormat `
-Handle $handle
}
finally
{
# Close handle
if ($handle.Value)
{
$handle.Value.Close()
}
}
} # function New-SimpleVirtualDisk
<#
.SYNOPSIS
Attaches a virtual disk to the system.
.PARAMETER VirtualDiskPath
Specifies the whole path to the virtual disk file.
.PARAMETER DiskFormat
Specifies the supported virtual disk format.
.PARAMETER Handle
Specifies a reference to a win32 handle that points to a virtual disk
#>
function Add-SimpleVirtualDisk
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$VirtualDiskPath,
[Parameter(Mandatory = $true)]
[ValidateSet('Vhd', 'Vhdx')]
[System.String]
$DiskFormat,
[Parameter()]
[ref]
$Handle
)
try
{
Write-Verbose -Message ($script:localizedData.AttachingVirtualDiskMessage -f $VirtualDiskPath)
$vDiskHelper = Get-VirtDiskWin32HelperScript
# No handle passed in so we need to open the virtual disk first using $virtualDiskPath to get the handle.
if ($null -eq $Handle)
{
$Handle = Get-VirtualDiskHandle -VirtualDiskPath $VirtualDiskPath -DiskFormat $DiskFormat
}
# Build parameters for AttachVirtualDisk function.
[ref]$attachVirtualDiskParameters = New-Object VirtDisk.Helper+ATTACH_VIRTUAL_DISK_PARAMETERS
$attachVirtualDiskParameters.Value.Version = [VirtDisk.Helper]::ATTACH_VIRTUAL_DISK_VERSION_1
$securityDescriptor = [System.IntPtr]::Zero
$providerSpecificFlags = 0
$result = 0
<#
Some builds of Windows may not have the ATTACH_VIRTUAL_DISK_FLAG_AT_BOOT flag. So we attempt to attach the virtual
disk with the flag first. If this fails we attach the virtual disk without the flag. The flag allows the
virtual disk to be attached by the system at boot time.
#>
for ($attempts = 0; $attempts -lt 2; $attempts++)
{
if ($attempts -eq 0)
{
$flags = [VirtDisk.Helper]::ATTACH_VIRTUAL_DISK_FLAG_PERMANENT_LIFETIME -bor
[VirtDisk.Helper]::ATTACH_VIRTUAL_DISK_FLAG_AT_BOOT
}
else
{
$flags = [VirtDisk.Helper]::ATTACH_VIRTUAL_DISK_FLAG_PERMANENT_LIFETIME
}
$result = Add-VirtualDiskUsingWin32 `
-Handle $Handle `
-SecurityDescriptor $securityDescriptor `
-Flags $flags `
-ProviderSpecificFlags $providerSpecificFlags `
-AttachVirtualDiskParameters $attachVirtualDiskParameters `
-Overlapped ([System.IntPtr]::Zero)
if ($result -eq 0)
{
break
}
}
if ($result -ne 0)
{
$win32Error = [System.ComponentModel.Win32Exception]::new($result)
throw [System.Exception]::new( `
($script:localizedData.AttachVirtualDiskError -f $VirtualDiskPath, $win32Error.Message), `
$win32Error)
}
Write-Verbose -Message ($script:localizedData.VirtualDiskAttachedSuccessfully -f $VirtualDiskPath)
}
finally
{
# Close handle
if ($handle.Value)
{
$handle.Value.Close()
}
}
} # function Add-SimpleVirtualDisk
<#
.SYNOPSIS
Opens a handle to a virtual disk on the system.
.PARAMETER VirtualDiskPath
Specifies the whole path to the virtual disk file.
.PARAMETER DiskFormat
Specifies the supported virtual disk format.
#>
function Get-VirtualDiskHandle
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$VirtualDiskPath,
[Parameter(Mandatory = $true)]
[ValidateSet('Vhd', 'Vhdx')]
[System.String]
$DiskFormat
)
Write-Verbose -Message ($script:localizedData.OpeningVirtualBeforeAttachingMessage)
$vDiskHelper = Get-VirtDiskWin32HelperScript
# Get parameters for OpenVirtualDisk function.
[ref]$virtualStorageType = Get-VirtualStorageType -DiskFormat $DiskFormat
[ref]$openVirtualDiskParameters = New-Object VirtDisk.Helper+OPEN_VIRTUAL_DISK_PARAMETERS
$openVirtualDiskParameters.Value.Version = [VirtDisk.Helper]::OPEN_VIRTUAL_DISK_VERSION_1
$accessMask = [VirtDisk.Helper]::VIRTUAL_DISK_ACCESS_ALL
$flags = [VirtDisk.Helper]::OPEN_VIRTUAL_DISK_FLAG_NONE
# Handle to the virtual disk.
[ref]$handle = [Microsoft.Win32.SafeHandles.SafeFileHandle]::Zero
$result = Get-VirtualDiskUsingWin32 `
-VirtualStorageType $virtualStorageType `
-VirtualDiskPath $VirtualDiskPath `
-AccessMask $accessMask `
-Flags $flags `
-OpenVirtualDiskParameters $openVirtualDiskParameters `
-Handle $handle
if ($result -ne 0)
{
$win32Error = [System.ComponentModel.Win32Exception]::new($result)
throw [System.Exception]::new( `
($script:localizedData.OpenVirtualDiskError -f $VirtualDiskPath, $win32Error.Message), `
$win32Error)
}
Write-Verbose -Message ($script:localizedData.VirtualDiskOpenedSuccessfully -f $VirtualDiskPath)
return $handle
} # function Get-VirtualDiskHandle
<#
.SYNOPSIS
Gets the storage type based on the disk format.
.PARAMETER DiskFormat
Specifies the supported virtual disk format.
#>
function Get-VirtualStorageType
{
[CmdletBinding()]
[OutputType([VirtDisk.Helper+VIRTUAL_STORAGE_TYPE])]
param
(
[Parameter(Mandatory = $true)]
[ValidateSet('Vhd', 'Vhdx')]
[System.String]
$DiskFormat
)
# Create VIRTUAL_STORAGE_TYPE structure.
$virtualStorageType = New-Object -TypeName VirtDisk.Helper+VIRTUAL_STORAGE_TYPE
# Default to the vhdx file format.
$virtualStorageType.VendorId = [VirtDisk.Helper]::VIRTUAL_STORAGE_TYPE_VENDOR_MICROSOFT
$virtualStorageType.DeviceId = [VirtDisk.Helper]::VIRTUAL_STORAGE_TYPE_DEVICE_VHDX
if ($DiskFormat -eq 'Vhd')
{
$virtualStorageType.DeviceId = [VirtDisk.Helper]::VIRTUAL_STORAGE_TYPE_DEVICE_VHD
}
return $virtualStorageType
} # function Get-VirtualStorageType
Export-ModuleMember -Function @(
'New-SimpleVirtualDisk',
'Add-SimpleVirtualDisk',
'Get-VirtualDiskHandle',
'Get-VirtualStorageType',
'Get-VirtDiskWin32HelperScript',
'New-VirtualDiskUsingWin32',
'Add-VirtualDiskUsingWin32',
'Get-VirtualDiskUsingWin32'
)