PublicFunctions/Get-VolumeInfoOnPipeline.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
Function Get-VolumeInfoOnPipeline
{
    <#
        .Synopsis
            Adds Volume Info Columns to an object
        .DESCRIPTION
            This typically takes an imported csv file with a ComputerName Column as an iput object
            but just about any collection of objects that exposes .ComputerName should work
            The output is the same type of object as the input so that it can be piped
            to the next function to add another column

            Columns returned are a subset of Win32_Volume

            Adds rows as needed. One per drive after the first (usually c:)

            By default, each new row gets a copy of all the data in the non Volume columns. This allows
            for filtering with Where-Object.
        .EXAMPLE
            Get-ServerCollection |
                Test-ServerConnectionOnPipeline |
                Get-ProcInfoOnPipeline |
                Get-VolumeInfoOnPipeline |
                Select-Object ComputerName,TotalProcs,ProcName,Cores,Volumes,DriveType,Capacity,PctFree |
                Format-Table -autosize

            A Sampling of Functions and Columns
        .EXAMPLE
            Get-ServerCollection |
                Test-ServerConnectionOnPipeline |
                Get-ProcInfoOnPipeline |
                Get-VolumeInfoOnPipeline |
                Select-Object ComputerName,TotalProcs,Cores,Volumes,DriveType,Capacity,PctFree |
                Where-Object PctFree -gt 95 |
                Format-Table -autosize

            Gets all of the drives either over or under a threshold. IN this case % free greater than 95%
        .EXAMPLE
            Get-ServerCollection |
                Test-ServerConnectionOnPipeline |
                Get-OSCaptionOnPipeline |
                Get-TimeZoneOnPipeline |
                Get-TotalMemoryOnPipeline |
                Get-MachineModelOnPipeline |
                Get-ProcInfoOnPipeline |
                Get-VolumeInfoOnPipeline |
                Select-Object ComputerName,OSVersion,TotalMemory,MachineModel,TotalProcs,ProcName,Cores,Volumes,DriveType,Capacity,PctFree |
                Where-Object DriveType -eq 3 |
                Export-Csv -path .\ServerSpecs.csv -NoTypeInformation

            Gets a nice specs report and outputs it to a CSV file in the working directory
    #>

    [CmdletBinding()]

    Param (
        [parameter(
        Mandatory=$true,
        ValueFromPipeline= $true)]
        $ComputerProperties,
        [switch]
        $NoErrorCheck
    )

    Process {
        $ComputerProperties | Out-Null
        $NoErrorCheck | Out-Null
        $PSItem | Select-Object *, Volumes, DriveType, Capacity, PctFree | ForEach-Object {
            If (($NoErrorCheck) -or (($PSItem.Ping) -and ($PSItem.WMI))) {
                $Volumes = Get-CimInstance -ComputerName $PSItem.ComputerName -Query "Select DriveLetter,DriveType,Capacity,FreeSpace from Win32_Volume"
                $PSItem.Volumes = $PSItem.DriveType = $PSItem.Capacity = $PSItem.PctFree = $null
                $PSItem.Volumes = $Volumes[0].DriveLetter
                $PSItem.DriveType = $Volumes[0].DriveType
                $PSItem.Capacity = [Math]::Round(($Volumes[0].Capacity / 1GB), 0)
                If (($null -eq $Volumes[0].Capacity) -or ($Volumes[0].Capacity -eq 0)) {
                    $PSItem.PctFree = $null
                } else {
                    $PSItem.PctFree = [Math]::Round($Volumes[0].FreeSpace/$Volumes[0].Capacity*100,1)
                }
                $PSItem

                $Count = ($Volumes | Measure-Object).Count

                If ($Count -gt 1){
                    For ($i=1; $i -le $Count-1; $i++) {
                        $PSItem.Volumes = $PSItem.DriveType = $PSItem.Capacity = $PSItem.PctFree = $null
                        $PSItem.Volumes = $Volumes[$i].DriveLetter
                        $PSItem.DriveType = $Volumes[$i].DriveType
                        $PSItem.Capacity = [Math]::Round(($Volumes[$i].Capacity / 1GB), 0)
                        If (($null -eq $Volumes[$i].Capacity) -or ($Volumes[$i].Capacity -eq 0)) {
                            $PSItem.PctFree = $null
                        } else {
                            $PSItem.PctFree = [Math]::Round($Volumes[$i].FreeSpace/$Volumes[$i].Capacity*100,1)
                        }
                        New-Object PsObject $PSItem
                    }
                }
            } Else {
                $PSItem.Volumes = 'No Try'
                $PSItem.Capacity = 'No Try'
                $PSItem.DriveType = 'No Try'
                $PSItem.PctFree = 'No Try'
                $PSItem
            }
        }
    }
}