3

I found this PS script that check if the PC is a Desktop or a Laptop

function Get-HardwareType {
    $hardwaretype = Get-WmiObject -Class Win32_ComputerSystem -Property PCSystemType
        If ($hardwaretype -ne 2)
        {
        return $true
        }
        Else
        {
        return $false
        }}

If (Get-HardwareType) { "$Env:ComputerName is a Desktop" } Else { "$Env:ComputerName is a Laptop" }

If the result is "Laptop", I need to run this other command

Add-AppxPackage -Path ".\28671Petrroll.PowerPlanSwitcher_0.4.4.0_x86__ge82akyxbc7z4.Appx"

or else to skip it. How can I combine them?

EDIT:

Seems that I need an internet connection for completely install the app; without internet the app doesn't start as long as I run the app with an internet connection. Does someone know what I need to do without an internet connection? Or it's impossible?

3 Answers3

6

Here's a cleaned up more readable version of your code:

function Test-IsLaptop {
    $HardwareType = (Get-WmiObject -Class Win32_ComputerSystem -Property PCSystemType).PCSystemType
    # https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-computersystem
    # Mobile = 2
    $HardwareType -eq 2
}

if (Test-IsLaptop) { Write-Host "$Env:ComputerName is a Laptop" Add-AppxPackage -Path "$PSScriptRoot\28671Petrroll.PowerPlanSwitcher_0.4.4.0_x86__ge82akyxbc7z4.Appx" } else { Write-Host "$Env:ComputerName is a Desktop" }

Edit: It's recommended to switch from Get-WmiObject to Get-CimInstance. The command would look like this in that case:

$HardwareType = (Get-CimInstance -Class Win32_ComputerSystem -Property PCSystemType).PCSystemType

And here's the reason why:

The big difference between the WMI cmdlets and the CIM cmdlets is that the CIM cmdlets use WSMAN (WinRM) to connect to remote machines. In the same way that you can create PowerShell remoting sessions, you can create and manage CIM sessions by using these cmdlets.

The big drawback to the WMI cmdlets is that they use DCOM to access remote machines. DCOM isn’t firewall friendly, can be blocked by networking equipment, and gives some arcane errors when things go wrong.

Source: https://devblogs.microsoft.com/scripting/should-i-use-cim-or-wmi-with-windows-powershell/

megamorf
  • 2,454
0

Your program does not prove that it is a desktop it just determines whether or not the system is a laptop. You are making an assumption that a system must be one or the other which is erroneous. This is the day of internet of things with embedded devices, server, workstations, and desktops in addition to laptops. Never assume.

Here are the possible values:

AppliancePC 6
System is an appliance PC

Desktop 1
System is a desktop

EnterpriseServer 4
System is an Enterprise Server

Maximum 8
Maximum enum value

Mobile 2
System is a mobile device

PerformanceServer 7
System is a performance server

SOHOServer 5
System is a Small Office and Home Office (SOHO) Server

Unspecified 0
System type is unspecified

Workstation 3
System is a workstation

Try this:

Function IsLaptop
{
    $isLaptop = $false
    if(Get-WmiObject -Class win32_systemenclosure | 
        Where-Object { $_.chassistypes -eq 8 -or $_.chassistypes -eq 9 -or $_.chassistypes -eq 10 -or $_.chassistypes -eq 14 -or $_.chassistypes -eq 30})
        { $isLaptop = $true }
    Return $isLaptop
}

If (IsLaptop) {"This is a laptop"} Else {"This is not a laptop."}

0

Well for now that may be true. Things change and then one day it isn't true any more. In my experience writing code to configure different types of PC hardware for more than 20 years, it pays to be as thorough as you can. I am just trying to save you grief down the road.

Look at all the different chassis types at this URL. All of these could potentially be reported by a laptop: Portable (8), Laptop (9), Notebook (10), Sub Notebook (14)

https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-systemenclosure