Two quick functions to return a system's firmware interface.

Discussion in 'Scripting' started by GodHand, Jul 11, 2018.

  1. GodHand

    GodHand MDL Senior Member

    Jul 15, 2016
    341
    319
    10
    The first function uses a C# method to determine and return the type of firmware interface a system is using.

    The second function uses a compiled Win32 API to test a system's firmware, returning a boolean value of "True" or "False" depending on whether or not UEFI is detected.

    Code:
    <#
        .NOTES
        ===========================================================================
         Created with:     SAPIEN Technologies, Inc., PowerShell Studio 2018 v5.5.150
         Created on:       4/19/2018 10:15 AM
         Created by:       BenTheGreat
         Filename:         DetectSystemFirmware
        ===========================================================================
        .DESCRIPTION
            Two functions, one returning a string value and the other a boolean value, to retrieve a system's firmware interface (BIOS).
    #>
    
    Function Get-FirmwareInterface
    {
    <#
        .SYNOPSIS
            Retrieves the system's firmware interface (BIOS).
     
        .DESCRIPTION
            Retrieves the system's firmware interface (BIOS) and returns a string indicating the firmware type: Legacy BIOS, UEFI or Unknown.
     
        .EXAMPLE
            PS C:\> Get-FirmwareInterface
    #>
        [CmdletBinding()]
        [OutputType([System.String])]
        Param ()
    
        Begin
        {
            Add-Type @'
    using System;
    using System.Runtime.InteropServices;
    public class FirmwareInterface
    {
        [DllImport("kernel32.dll")]
        static extern bool GetFirmwareType(
            ref uint FirmwareType);
        public static uint GetFirmwareType()
        {
            uint firmwaretype = 0;
            if (GetFirmwareType(ref firmwaretype))
                return firmwaretype;
            else
                return 0;
        }
    }
    '@
        }
        Process
        {
            If ([FirmwareInterface]::GetFirmwareType() -eq 1) { $FirmwareInterface = "Legacy BIOS" }
            ElseIf ([FirmwareInterface]::GetFirmwareType() -eq 2) { $FirmwareInterface = "UEFI" }
            ElseIf ([FirmwareInterface]::GetFirmwareType() -eq 3) { $FirmwareInterface = "Unknown" }
            Return $FirmwareInterface
        }
    }
    
    Function Test-UEFI
    {
    <#
        .SYNOPSIS
            A compiled Win32 API that tests the system's firmware for a UEFI interface.
     
        .DESCRIPTION
            Tests the system's firmware for a UEFI interface and returns a bool value of True if a UEFI interface is detected and False if a Legacy BIOS is detected.
     
        .EXAMPLE
            Test-UEFI
            If (Test-UEFI) { Write-Output "UEFI Interface detected." } Else { Write-Output "Legacy BIOS Interface detected." }
    #>
        [CmdletBinding()]
        [OutputType([System.Boolean])]
        Param ()
     
        Begin
        {
            Add-Type @'
    using System;
    using System.Runtime.InteropServices;
    public class TestUEFI
    {
        [DllImport("kernel32.dll", SetLastError = true)]
        static extern UInt32
        GetFirmwareEnvironmentVariableA(
            string lpName,
            string lpGuid,
            IntPtr pBuffer,
            UInt32 nSize);
        const int ERROR_INVALID_FUNCTION = 1;
        public static bool IsUEFI()
        {
            GetFirmwareEnvironmentVariableA("", "{00000000-0000-0000-0000-000000000000}", IntPtr.Zero, 0);
            if (Marshal.GetLastWin32Error() == ERROR_INVALID_FUNCTION)
                return false;
            else
                return true;
        }
    }
    '@
        }
        Process
        {
            [TestUEFI]::IsUEFI()
        }
    }
    # SIG # Begin signature block
    # MIIL6AYJKoZIhvcNAQcCoIIL2TCCC9UCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB
    # gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR
    # AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQUBrhqOhW2qVcV16l0N6lNegkW
    # WhGgggj8MIIDfTCCAmWgAwIBAgIQfY66zkudTZ9EnV2nSZm8oDANBgkqhkiG9w0B
    # AQsFADBFMRQwEgYKCZImiZPyLGQBGRYEVEVDSDEVMBMGCgmSJomT8ixkARkWBU9N
    # TklDMRYwFAYDVQQDEw1PTU5JQy5URUNILUNBMB4XDTE4MDMxMzIxNTY1OFoXDTIz
    # MDMxMzIyMDY1OFowRTEUMBIGCgmSJomT8ixkARkWBFRFQ0gxFTATBgoJkiaJk/Is
    # ZAEZFgVPTU5JQzEWMBQGA1UEAxMNT01OSUMuVEVDSC1DQTCCASIwDQYJKoZIhvcN
    # AQEBBQADggEPADCCAQoCggEBAO6V7MmlK+QuOqWIzrLbmhv9acRXB46vi4RV2xla
    # MTDUimrSyGtpoDQTYK2QZ3idDq1nxrnfAR2XytTwVCcCFoWLpFFRack5k/q3QFFV
    # WP2DbSqoWfNG/EFd0qx8p81X5mH09t1mnN/K+BX1jiBS60rQYTsSGMkSSn/IUxDs
    # sLvatjToctZnCDiqG8SgPdWtVfHRLLMmT0l8paOamO0bpaSSsTpBaan+qiYidnxa
    # eIR23Yvv26Px1kMFYNp5YrWfWJEw5udB4W8DASO8TriypXXpca2jCEkVswNwNW/n
    # Ng7QQqECDVwVm3BVSClNcf1J52uU+Nvx36gKRl5xcogW4h0CAwEAAaNpMGcwEwYJ
    # KwYBBAGCNxQCBAYeBABDAEEwDgYDVR0PAQH/BAQDAgGGMA8GA1UdEwEB/wQFMAMB
    # Af8wHQYDVR0OBBYEFH/3cqyAb+6RpNGa2+j3ldMI8axTMBAGCSsGAQQBgjcVAQQD
    # AgEAMA0GCSqGSIb3DQEBCwUAA4IBAQBYMivmEQPQpT1OfiPLVFaGFbnKmWo0dTWo
    # vkCQMq54NdUqvnCkOIC9O3nrsBqdQhTPAtDow1C1qWQipGf/JyMCTh9ZIEoz3u4z
    # RsiKMjIlPJkar1OsTsvKcAaya+a10LTcBMfF4DyOFaGqvKNrTaD3MmFQIBblQ8TS
    # QOzQPOXUwY/2IgI9w1AA8VO0N2coYzvj4i79RSQ77eg1iefjBRqs347o4/b7pWtS
    # 95+FBGr7JhhV3i9EI95172O4jmEkmoJQgr2mzvThjp9WiyeyjpnBAikV14YmEIyu
    # DmKue5ZuxG+D3W3ZwFyGytUCHYWwMshTRwI0z236dZG9OhYDSfibMIIFdzCCBF+g
    # AwIBAgITIQAAAAV87PzZFzK4xAAAAAAABTANBgkqhkiG9w0BAQsFADBFMRQwEgYK
    # CZImiZPyLGQBGRYEVEVDSDEVMBMGCgmSJomT8ixkARkWBU9NTklDMRYwFAYDVQQD
    # Ew1PTU5JQy5URUNILUNBMB4XDTE4MDQxODEyMjAzNloXDTE5MDQxODEyMjAzNlow
    # UzEUMBIGCgmSJomT8ixkARkWBFRFQ0gxFTATBgoJkiaJk/IsZAEZFgVPTU5JQzEO
    # MAwGA1UEAxMFVXNlcnMxFDASBgNVBAMTC0JlblRoZUdyZWF0MIIBIjANBgkqhkiG
    # 9w0BAQEFAAOCAQ8AMIIBCgKCAQEA9xWMMTEOCpdnZu3eDTVbytEzoTnHQYeS/2jg
    # wGLYU3+43C3viMoNVj+nLANJydTIRW5Dca+6JfO8UH25kf0XQ+AiXirQfjb9ec9u
    # I+au+krmlL1fSR076lPgYzqnqPMQzOER8U2J2+uF18UtxEVO3rq7Cnxlich4jXzy
    # gTy8XiNSAfUGR1nfq7HjahJ/CKopwl/7NcfmV5ZDzogRob1eErOPJXGAkewJuKqp
    # /qItYzGH+9XADCyO0GYVIOsXNIE0Ho0bdBPZ3eDdamL1vocTlEkTe0/drs3o2AkS
    # qcgg2I0uBco/p8CxCR7Tfq2zX1DFW9B7+KGNobxq+l+V15rTMwIDAQABo4ICUDCC
    # AkwwJQYJKwYBBAGCNxQCBBgeFgBDAG8AZABlAFMAaQBnAG4AaQBuAGcwEwYDVR0l
    # BAwwCgYIKwYBBQUHAwMwDgYDVR0PAQH/BAQDAgeAMB0GA1UdDgQWBBSIikO7ZjAP
    # GlMAUcP2kulHiqpJnDAfBgNVHSMEGDAWgBR/93KsgG/ukaTRmtvo95XTCPGsUzCB
    # yQYDVR0fBIHBMIG+MIG7oIG4oIG1hoGybGRhcDovLy9DTj1PTU5JQy5URUNILUNB
    # LENOPURPUkFETyxDTj1DRFAsQ049UHVibGljJTIwS2V5JTIwU2VydmljZXMsQ049
    # U2VydmljZXMsQ049Q29uZmlndXJhdGlvbixEQz1PTU5JQyxEQz1URUNIP2NlcnRp
    # ZmljYXRlUmV2b2NhdGlvbkxpc3Q/YmFzZT9vYmplY3RDbGFzcz1jUkxEaXN0cmli
    # dXRpb25Qb2ludDCBvgYIKwYBBQUHAQEEgbEwga4wgasGCCsGAQUFB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nAHMAIABmAGkAcgBtAHcA
    # YQByAGUAIABpAG4AdABlAHIAZgBhAGMAZQAgACgAQgBJAE8AUwApAC4wDQYJKoZI
    # hvcNAQEBBQAEggEAhfbLR8e0gcr/xsucqRQA3y7opnSeqVuuDXS38mZKWttotCey
    # uej9IW5HtylwVtzjZEYVDCrDb5riFgpm0mSirId7t1sKMJOnMKMFkpyKV0MQ/2tM
    # UyN95nHQufzsMddITs1O8q6fKR3pb5eXfqBanFRR19QYG6CPNlNNDkn2BpxUx7uY
    # ohKY/2HejZeuP7+nqa7NwkQiLCC2W3yiJJ2BxxrRfWX299gwuDqlp6VPKVP4mHd0
    # hKWcTmA5oiBkOXNqocCw7F45DkBGTrZtdncanCPy4548plhyL1+HfkAytBsBGf5S
    # 9J9Eegz52KVHEDa0uV/5oGu4VWNboYsnnTAqvQ==
    # SIG # End signature block
    
    To run, simply enter Test-UEFI or Get-FirmwareInterface