Get-DiskPartDisk.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
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
Function Get-DiskPartDisk
{
    <#
    .SYNOPSIS
        Run a LIST DISK and parse the output into objects, from one or more remote systems
 
    .FUNCTIONALITY
        Computers
 
    .DESCRIPTION
        Run a LIST DISK 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-DiskPartDisk -computername c-is-hyperv-1
 
        # Run 'list disk' 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 disk`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
            }
        
            $Disks = ForEach ($Line in $Output)
            {
                If ($Line.StartsWith(" Disk"))
                {
                    $Line
                }
            }

            $DiskCount = $Disks.Count

            For ($i=1;$i -le ($DiskCount - 1);$i++)
            {
                $currLine = $Disks[$i]
                $currLine -Match " Disk (?<disknum>...) +(?<sts>.............) +(?<sz>.......) +(?<fr>.......) +(?<dyn>...) +(?<gpt>...)" | Out-Null
                $DiskObj = @{
                    "ComputerName" = $Computer
                    "DiskNumber" = $Matches['disknum'].Trim()
                    "Status" = $Matches['sts'].Trim()
                    "Size" = $Matches['sz'].Trim()
                    "Free" = $Matches['fr'].Trim()
                    "Dyn" = $Matches['dyn'].Trim()
                    "Gpt" = $Matches['gpt'].Trim()
                }

                $dpscript = "select disk $($DiskObj.DiskNumber)`ndetail disk`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 "Disk ID" -and $Line -match ":")
                    {
                         $DiskObj.Add( "DiskID", $Line.Split(":")[1].Trim())
                    }
                    ElseIf ($Line.StartsWith("Type") -and $Line -match ":")
                    {
                        $DiskObj.Add( "DetailType", $Line.Split(":")[1].Trim())
                    }
                    ElseIf ($Line.StartsWith("Status") -and $Line -match ":")
                    {
                        $DiskObj.Add( "DetailStatus", $Line.Split(":")[1].Trim())
                    }
                    ElseIf ($Line.StartsWith("Path") -and $Line -match ":")
                    {
                        $DiskObj.Add( "Path", $Line.Split(":")[1].Trim())
                    }
                    ElseIf ($Line.StartsWith("Target") -and $Line -match ":")
                    {
                       $DiskObj.Add( "Target", $Line.Split(":")[1].Trim())
                    }
                    ElseIf ($Line.StartsWith("LUN ID") -and $Line -match ":")
                    {
                        $DiskObj.Add( "LUNID", $Line.Split(":")[1].Trim())
                    }
                    ElseIf ($Line.StartsWith("Location Path") -and $Line -match ":")
                    {
                        $DiskObj.Add( "LocationPath", $Line.Split(":")[1].Trim())
                    }
                    ElseIf ($Line.StartsWith("Current Read-only State") -and $Line -match ":")
                    {
                        $DiskObj.Add( "CurrentReadOnlyState", $Line.Split(":")[1].Trim())
                    }
                    ElseIf ($Line.StartsWith("Read-only") -and $Line -match ":")
                    {
                        $DiskObj.Add( "ReadOnly", $Line.Split(":")[1].Trim())
                    }
                    ElseIf ($Line.StartsWith("Boot Disk") -and $Line -match ":")
                    {
                        $DiskObj.Add( "BootDisk", $Line.Split(":")[1].Trim())
                    }
                    ElseIf ($Line.StartsWith("Pagefile Disk") -and $Line -match ":")
                    {
                        $DiskObj.Add( "PagefileDisk", $Line.Split(":")[1].Trim())
                    }
                    ElseIf ($Line.StartsWith("Hibernation File Disk") -and $Line -match ":")
                    {
                        $DiskObj.Add(  "HibernationFileDisk", $Line.Split(":")[1].Trim())
                    }
                    ElseIf ($Line.StartsWith("Crashdump Disk") -and $Line -match ":")
                    {
                        $DiskObj.Add( "CrashdumpDisk", $Line.Split(":")[1].Trim())
                    }
                    ElseIf ($Line.StartsWith("Clustered Disk") -and $Line -match ":")
                    {
                        $DiskObj.Add( "ClusteredDisk", $Line.Split(":")[1].Trim())
                    }
                }
    
                New-Object -TypeName PSObject -Property $DiskObj |
                    Select-Object -Property ComputerName,
                    DiskNumber,
                    Status,
                    Size,
                    Free,
                    Dyn,
                    Gpt,
                    DiskID,
                    DetailType,
                    DetailStatus,
                    Path,
                    Target,
                    LUNID,
                    LocationPath,
                    CurrentReadOnlyState,
                    ReadOnly,
                    BootDisk,
                    PagefileDisk,
                    HibernationFileDisk,
                    CrashdumpDisk,
                    ClusteredDisk
            }
        }
    }
}