File size: 2,028 Bytes
8c763fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

# Precondition: start from fresh PS session, do not have the media mounted
param([switch]$useModule, [string]$VHDPath)

function CreateVHD ($VHDPath, $Size)
{
  $drive = (New-VHD -Path $vhdpath -SizeBytes $size -Dynamic   | `
              Mount-VHD -Passthru |  `
              Get-Disk -Number {$_.DiskNumber} | `
              Initialize-Disk -PartitionStyle MBR -PassThru | `
              New-Partition -UseMaximumSize -AssignDriveLetter:$false -MbrType IFS | `
              Format-Volume -Confirm:$false -FileSystem NTFS -Force | `
              Get-Partition | `
              Add-PartitionAccessPath -AssignDriveLetter -PassThru | `
              Get-Volume).DriveLetter

    $drive
}

if ($useModule)
{
    $m = New-Module {
        function Test-DrivePresenceFromModule
        {
            param ([String]$Path)

            if (Test-Path $Path)
            {
                "Drive found"
                if (-not (Get-PSDrive -Name $Path[0] -Scope Global -ErrorAction SilentlyContinue))
                {
                    Write-Error "Drive is NOT in Global scope"
                }
            }
            else { Write-Error "$Path not found" }
        }

        Export-ModuleMember -Function Test-DrivePresenceFromModule
    }
}

try
{
    if ($useModule)
    {
        Import-Module $m -Force
    }

    $drive = CreateVHD -VHDPath $VHDPath -Size 5mb
    $pathToCheck = "${drive}:"

    if ($useModule)
    {
        Test-DrivePresenceFromModule -Path $pathToCheck
    }
    else
    {
        if (Test-Path $pathToCheck)
        {
            "Drive found"
            if (-not (Get-PSDrive -Name $drive -Scope Global -ErrorAction SilentlyContinue))
            {
                Write-Error "Drive is NOT in Global scope"
            }
        }
        else { Write-Error "$pathToCheck not found" }
    }
}
finally
{
    if ($useModule)
    {
        Remove-Module $m
    }
    Dismount-VHD $VHDPath
    Remove-Item $VHDPath
}