Test-IsLaptop extension

Topics: Archive - Toolkit Extensions
Feb 27, 2015 at 9:48 PM
#region Function Test-IsLaptop
Function Test-IsLaptop {
    Tests whether the local machine HAS a battery.
    Tests whether the local machine HAS a battery and returns true/false.
    This a re-work of PSADT's Test-Battery function
    I wanted to use ACLineStatus field in SYSTEM_POWER_STATUS from this:
    because [http://stackoverflow.com/questions/4849447/detect-if-application-is-running-on-laptop]
    ACLineStatus = 0 => The system is not using AC power > Laptop + battery
    ACLineStatus = 1 => The system is using AC power => Laptop + AC
    ACLineStatus = 255 => AC power status is unknown => Desktop

    And maybe later expose SYSTEM_POWER_CAPABILITIES too but I'm a C++ idiot
    *********** Use at your own risk ***************
    Param (
    Begin {
        # Get the name of this function and write header
        [string]${CmdletName} = $PSCmdlet.MyInvocation.MyCommand.Name
        Write-FunctionHeaderOrFooter -CmdletName ${CmdletName} -CmdletBoundParameters $PSBoundParameters -Header
        # PowerStatus class found in this assembly is more reliable than WMI in cases where the battery is failing.
        Add-Type -Assembly System.Windows.Forms -ErrorAction 'SilentlyContinue'
    Process {
        Write-Log -Message 'Check if system HAS a battery...' -Source ${CmdletName}
        [System.Windows.Forms.PowerStatus]$PowerStatus = [System.Windows.Forms.SystemInformation]::PowerStatus
        ## Get the current battery charge status. Possible values: High, Low, Critical, Charging, NoSystemBattery, Unknown.
        [string]$BatteryChargeStatus = $PowerStatus.BatteryChargeStatus
        If (($BatteryChargeStatus -eq 'NoSystemBattery') -or ($BatteryChargeStatus -eq 'Unknown')) {
#           [single]$BatteryLifePercent = 0.0
            Write-Output $false
        } else {
            Write-Output $true
    End {
        Write-FunctionHeaderOrFooter -CmdletName ${CmdletName} -Footer