Tests/Unit/MSFT_xWindowsFeature.TestHelper.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
<#
    .SYNOPSIS
    Returns false if the OS is windows 7 and true otherwise.
#>

function Get-IsWin8orAbove
{
    [OutputType([System.Boolean])]
    [CmdletBinding()]
    param ()

    $operatingSystemVersion = (Get-CimInstance -Class Win32_OperatingSystem).Version

    # For Win 7 OS
    if ($operatingSystemVersion -like '6.1*')
    {
        return $false
    }

    return $true
}

<#
    .SYNOPSIS
    Checks if the computer is running a Windows Server operating system.
    To be used in 'should run' methods.
#>

function Get-IsServerSKU
{
    [OutputType([System.Boolean])]
    [CmdletBinding()]
    param (
        [String]
        $TargetComputerName
    )

    if ($TargetComputerName)
    {
        $operatingSystem = Get-CimInstance -ClassName  Win32_OperatingSystem -ComputerName $TargetComputerName
    }
    else
    {
        $operatingSystem = Get-CimInstance -ClassName  Win32_OperatingSystem
    }

    # We should not run this test on client skus
    return ($operatingSystem.ProductType -ne 1)
}

<#
    .SYNOPSIS
    Checks if the computer is running Windows Server 2008 R2 Server Core.
#>

function Get-IsServer2008R2Core
{
    [OutputType([System.Boolean])]
    [CmdletBinding()]
    param ()

    $datacenterServerCore = 12
    $standardServerCore = 13
    $enterpriseServerCore = 14

    $operatingSystem = Get-CimInstance -Class Win32_OperatingSystem
    if ($operatingSystem.Version.StartsWith('6.1.')) 
    {
        if (($operatingSystem.OperatingSystemSKU -eq $datacenterServerCore) -or ($operatingSystem.OperatingSystemSKU -eq $standardServerCore) -or ($operatingSystem.OperatingSystemSKU -eq $enterpriseServerCore))
        {
            return $true
        }
    }
    
    return $false
}

<#
    .SYNOPSIS
    Checks if the computer is running Windows Server 2012 Server Core.
#>

function Get-IsServer2012Core
{
    [OutputType([System.Boolean])]
    [CmdletBinding()]
    param ()

    $operatingSystem = Get-CimInstance -Class Win32_OperatingSystem
    if ($operatingSystem.Version.StartsWith('6.2.'))
    {
        $hasKey = Test-Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Server\ServerLevels" 
        if (-not $hasKey) 
        { 
            return $false
        }

        $extendedKey = Get-Item "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Server\ServerLevels" 
        if ($extendedKey.GetValue("ServerCoreExtended") -eq 0 -or ($extendedKey.GetValue("ServerCore") -eq 1 -and -not ($extendedKey.GetValue("Server-Gui-Mgmt") -eq 1 -and $extendedKey.GetValue("Server-Gui-Shell") -eq 1))) 
        { 
             return $true
        }
    }

    return $false
}

<#
    .SYNOPSIS
    Checks if the computer is running Windows Server 2012 Server Core or Windows Server 2008 R2 Server Core.
#>

function Get-IsWMFServerCore
{
    [OutputType([System.Boolean])]
    [CmdletBinding()]
    param ()

    return (Get-IsServer2012Core) -or (Get-IsServer2008R2Core)
}

Export-ModuleMember -Function `
    Get-IsWin8OrAbove, `
    Get-IsServerSKU, `
    Get-IsWMFServerCore