Misc/xExchangeDiskPart.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
#Adds the array of commands to a single temp file, and has disk part execute the temp file
function StartDiskpart
{
    [CmdletBinding()]
    [OutputType([System.String])]
    Param ([Array]$Commands, [Boolean]$ShowOutput = $true, $VerbosePreference)

    $Tempfile = [System.IO.Path]::GetTempFileName()

    foreach ($Com in $Commands)
    {
        $CMDLine = $CMDLine + $Com + ", "
        Add-Content $Tempfile $Com
    }

    $Output = DiskPart /s $Tempfile

    if ($ShowOutput)
    {
        Write-Verbose "Executed Diskpart commands: $(StringArrayToCommaSeparatedString -Array $Commands). Result:"
        Write-Verbose "$($Output)"
    }

    Remove-Item $Tempfile

    return $Output
}

#Uses diskpart to obtain information on the disks and volumes that already exist on the system
function GetDiskInfo
{
    [Hashtable]$global:DiskToVolumeMap = @{}
    [Hashtable]$global:VolumeToMountPointMap = @{}
    [Hashtable]$global:DiskSizeMap = @{}
    [int[]]$diskNums = @()

    $diskList = StartDiskpart -Commands "List Disk" -ShowOutput $false

    $foundDisks = $false

    #First parse out the list of disks
    foreach ($line in $diskList)
    {
        if ($foundDisks -eq $true)
        {
            if ($line.Contains("Disk "))
            {
                #First find the disk number
                $startIndex = " Disk ".Length
                $endIndex = " -------- ".Length
                $diskNumStr = $line.Substring($startIndex, $endIndex - $startIndex).Trim()

                if ($diskNumStr.Length -gt 0)
                {
                    $diskNum = [int]::Parse($diskNumStr)
                    $diskNums += $diskNum
                }

                #Now find the disk size
                $startIndex = " -------- ------------- ".Length
                $endIndex = " -------- ------------- ------- ".Length
                $diskSize = $line.Substring($startIndex, $endIndex - $startIndex).Trim()

                if ($diskSize.Length -gt 0 -and $diskNum -ne $null)
                {
                    $DiskSizeMap.Add($diskNum, $diskSize)
                }
            }
        }
        elseif ($line.Contains("-------- ------------- ------- ------- --- ---")) #Scroll forward until we find the where the list of disks starts
        {
            $foundDisks = $true
        }
    }

    #Now get info on the disks
    foreach ($diskNum in $diskNums)
    {
        $diskDetails = StartDiskpart -Commands "Select Disk $($diskNum)","Detail Disk" -ShowOutput $false

        $foundVolumes = $false

        for ($i = 0; $i -lt $diskDetails.Count; $i++)
        {
            $line = $diskDetails[$i]

            if ($foundVolumes -eq $true)
            {
                if ($line.StartsWith(" Volume "))
                {
                    #First find the volume number
                    $volStart = " Volume ".Length
                    $volEnd = " ---------- ".Length
                    $volStr = $line.Substring($volStart, $volEnd - $volStart).Trim()

                    if ($volStr.Length -gt 0)
                    {
                        $volNum = [int]::Parse($volStr)

                        AddObjectToMapOfObjectArrays -Map $DiskToVolumeMap -Key $diskNum -Value $volNum

                        #Now parse out the drive letter if it's set
                        $letterStart = " ---------- ".Length
                        $letterEnd = $line.IndexOf(" ---------- --- ") + " ---------- --- ".Length
                        $letter = $line.Substring($letterStart, $letterEnd - $letterStart).Trim()

                        if ($letter.Length -eq 1)
                        {
                            AddObjectToMapOfObjectArrays -Map $VolumeToMountPointMap -Key $volNum -Value $letter
                        }

                        #Now find all the mount points
                        do
                        {
                            $line = $diskDetails[++$i]

                            if ($line -eq $null -or $line.StartsWith(" Volume ") -or $line.Trim().Length -eq 0) #We've hit the next volume, or the end of all info
                            {
                                $i-- #Move $i back one as we may have overrun the start of the next volume info
                                break
                            }
                            else
                            {
                                $mountPoint = $line.Trim()

                                AddObjectToMapOfObjectArrays -Map $VolumeToMountPointMap -Key $volNum -Value $mountPoint
                            }

                        } while ($i -lt $diskDetails.Count)

                    }
                }
            }
            elseif ($line.Contains("There are no volumes."))
            {
                [string[]]$emptyArray = @()
                $DiskToVolumeMap[$diskNum] = $emptyArray

                break
            }
            elseif ($line.Contains("---------- --- ----------- ----- ---------- ------- --------- --------"))
            {
                $foundVolumes = $true
            }
        }
    }
}

function StringArrayToCommaSeparatedString
{
    param([string[]]$Array)

    $string = ""

    if ($Array -ne $null -and $Array.Count -gt 0)
    {
        $string = $Array[0]

        for ($i = 1; $i -lt $Array.Count; $i++)
        {
            $string += ",$($Array[$i])"
        }
    }

    return $string
}

#Takes a hashtable, and adds the given key and value.
function AddObjectToMapOfObjectArrays
{
    Param([Hashtable]$Map, $Key, $Value)

    if ($Map.ContainsKey($Key))
    {
        $Map[$Key] += $Value
    }
    else
    {
        [object[]]$Array = $Value
        $Map[$Key] = $Array
    }
}

#Checks whether the mount point specified in the given path already exists as a mount point
#Returns the volume number if it does exist, else -1
function MountPointExists
{
    param([string]$Path)

    foreach ($key in $global:VolumeToMountPointMap.Keys)
    {
        foreach ($value in $global:VolumeToMountPointMap[$key])
        {
            #Make sure both paths end with the same character
            if (($value.EndsWith("\")) -eq $false)
            {
                $value += "\"
            }

            if (($Path.EndsWith("\")) -eq $false)
            {
                $Path += "\"
            }

            #Do the comparison
            if ($value -like $Path)
            {
                return $key
            }
        }
    }

    return -1
}


Export-ModuleMember -Function *