Get-DiskPartVDisk.ps1

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
Function Get-DiskPartVDisk
{
    <#
    .SYNOPSIS
        Run a LIST VDISK and parse the output into objects, from one or more remote systems
 
    .FUNCTIONALITY
        Computers
 
    .DESCRIPTION
        Run a LIST VDISK and parse the output into objects, from one or more remote systems.
 
        Get-Help Invoke-DiskPartScript -Full for details on implementation of remote calls
 
    .PARAMETER ComputerName
        Computer(s) to run command on.
 
    .EXAMPLE
        Get-DiskPartVDisk -computername c-is-hyperv-1
 
        # Run 'list vdisk' on c-is-hyperv-1.
 
    .LINK
        https://github.com/RamblingCookieMonster/PSDiskPart
 
    .LINK
        Invoke-DiskPartScript
 
    .LINK
        Get-DiskPartDisk
 
    .LINK
        Get-DiskPartVolume
 
    .LINK
        Get-DiskPartVDisk
 
    .NOTES
        Thanks to Adam Conkle https://gallery.technet.microsoft.com/DiskPartexe-Powershell-0f7a1bab
 
    #>

    [OutputType('System.Management.Automation.PSObject')]
    [CmdletBinding()]
    param (
        [Parameter(
            ValueFromPipeline=$true,
            ValueFromPipelineByPropertyName=$true)]
        [string[]]$ComputerName = $env:COMPUTERNAME
    )

    Process
    {

        
        foreach($Computer in $ComputerName)
        {
            $dpscript = "list vdisk`n"
            Try
            {
                $Output = $Null
                if($Computer -eq $env:COMPUTERNAME)
                {
                    $Output = $dpscript | diskpart
                }
                else
                {
                    $Output = ( Invoke-DiskPartScript -ComputerName $Computer -DiskPartText $dpscript -Raw -ErrorAction stop ) -split "`n"
                }
            }
            Catch
            {
                Write-Error $_
                Continue
            }
        
            $VDisks = ForEach ($Line in $Output)
            {
                If ($Line.StartsWith(" VDisk"))
                {
                    $Line
                }
            }

            $VDiskCount = $VDisks.Count

            For ($i=1; $i -le ($VDiskCount - 1); $i++)
            {
                $currLine = $VDisks[$i]
                $currLine -Match " VDisk (?<vdisknum>...) +(?<phydisknum>........) +(?<state>....................) +(?<type>.........) +(?<file>.+)" | Out-Null
                $VDiskObj = @{
                    "ComputerName" = $Computer
                    "VDiskNumber" = $Matches['vdisknum'].Trim()
                    "PhysicalDiskNumber" = $Matches['phydisknum'].Trim()
                    "State" = $Matches['state'].Trim()
                    "Type" = $Matches['type'].Trim()
                    "File" = $Matches['file'].Trim()
                }

                $dpscript = "select vdisk file=$($VDiskObj.File)`ndetail vdisk`n"

                Try
                {
                    $Output = $Null
                    if($Computer -eq $env:COMPUTERNAME)
                    {
                        $Output = $dpscript | diskpart
                    }
                    else
                    {
                        $Output = ( Invoke-DiskPartScript -ComputerName $Computer -DiskPartText $dpscript -Raw -ErrorAction stop ) -split "`n"
                    }
                }
                Catch
                {
                    Write-Error $_
                    Continue
                }

                ForEach ($Line in $Output)
                {
                    If ($Line -cmatch "Device type ID" -and $Line -match ":")
                    {
                        $VDiskObj.Add( "DeviceTypeId", $Line.Split(":")[1].Trim())
                    }
                    ElseIf ($Line.StartsWith("Vendor ID") -and $Line -match ":")
                    {
                        $VDiskObj.Add( "VendorId", $Line.Split(":")[1].Trim())
                    }
                    ElseIf ($Line.StartsWith("State") -and $Line -match ":")
                    {
                        $VDiskObj.Add("DetailState", $Line.Split(":")[1].Trim())
                    }
                    ElseIf ($Line.StartsWith("Virtual size") -and $Line -match ":")
                    {
                        $VDiskObj.Add("VirtualSize", $Line.Split(":")[1].Trim())
                    }
                    ElseIf ($Line.StartsWith("Physical size") -and $Line -match ":")
                    {
                        $VDiskObj.Add("PhysicalSize", $Line.Split(":")[1].Trim())
                    }
                    ElseIf ($Line.StartsWith("Is Child") -and $Line -match ":")
                    {
                        $VDiskObj.Add( "IsChild", $Line.Split(":")[1].Trim())
                    }
                    ElseIf ($Line.StartsWith("Parent Filename") -and $Line -match ":")
                    {
                        $VDiskObj.Add("ParentFileName", $Line.Split(":")[1].Trim())
                    }
                    ElseIf ($Line.StartsWith("Associated disk#") -and $Line -match ":")
                    {
                        $VDiskObj.Add("AssociatedDiskNum", $Line.Split(":")[1].Trim())
                    }
                }
    
                New-Object -TypeName PSObject -Property $VDiskObj |
                    Select-Object -Property ComputerName,
                          VDiskNumber,
                        PhysicalDiskNumber,
                        State,
                        Type,
                        File,
                        DeviceTypeId,
                        VendorId,
                        DetailState,
                        VirtualSize,
                        PhysicalSize,
                        IsChild,
                        ParentFileName,
                        AssociatedDiskNum
            }
        }
    }
}