Test-WMiSMUGIsLaptop.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
function Test-IsLaptop {
    <#
    .SYNOPSIS
        return a boolean for if a machine is a laptop
    .DESCRIPTION
        return a boolean for if a machine is a laptop
    .PARAMETER Computer
        computer to query
    .PARAMETER cimsession
        cimsession to query
    .EXAMPLE
        C:\PS> test-islaptop
        returns local computer laptop status
    .EXAMPLE
        C:\PS>
        Another example of how to use this cmdlet
    #>

    [CmdletBinding(DefaultParameterSetName = 'ComputerName')]
    [Alias('Test-WMiSMUGIsLaptop')]
    param (
        [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true, ValueFromPipeline = $true, ParameterSetName = 'ComputerName')]
        [Alias('Connection', 'PSComputerName', 'PSConnectionName', 'IPAddress', 'ServerName', 'HostName', 'DNSHostName')]
        [string[]]$ComputerName = $env:COMPUTERNAME,
        [Parameter(Mandatory = $false, ParameterSetName = 'CimSession')]
        [CimSession[]]$CimSession
    )
    begin {
        $GetEnclosureSplat = @{
            ClassName = 'Win32_SystemEnclosure'
        }
        $LaptopChassisTypes = '8', '9', '10', '11', '14', '18', '21'
        Write-Debug "Valid laptop chassis types [`$LaptopChassisTypes= $([string]::Format("'{0}'", [string]::Join("', '", $LaptopChassisTypes)))]"
    }
    process {
        foreach ($Connection in (Get-Variable -Name $PSCmdlet.ParameterSetName -ValueOnly -Scope Local)) {
            Write-Debug "Checking chassis type by leveraging $($PSCmdlet.ParameterSetName) parameter on Get-CimInstance"
            $Computer = switch ($PSCmdlet.ParameterSetName) {
                'ComputerName' {
                    $Connection
                }
                'CimSession' {
                    $GetEnclosureSplat[$PSCmdlet.ParameterSetName] = $Connection
                    $Connection.ComputerName
                }
            }
            Write-Debug "GetEnclosureSplat variable declaration complete"
            [string]$ChassisType = (Get-CimInstance @GetEnclosureSplat).ChassisTypes
            Write-Verbose ([string]::Format("{0} has a chassis type of {1}", $Computer, $ChassisType))
            [PSCustomObject]@{
                ComputerName = $Computer
                IsLaptop     = $ChassisType -in $LaptopChassisTypes
            }
        }
    }
}

Export-ModuleMember -Function Test-IsLaptop -Alias 'Test-WMiSMUGIsLaptop'