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?

alessio89g
  • 91
  • 1
  • 2
  • 7

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,363
  • 15
  • 21
  • Thanks! I will test it soon – alessio89g Mar 13 '21 at 11:52
  • cim and wmi won't really matter when locally. Its good practice to switch, but given the nature of this question and how easy the answer is, OP is not likely going to do extensive wmi or cim queries. – LPChip Mar 13 '21 at 12:21
  • It will run locally, but if it's recommended, I will edit it with your suggestion. Now I can't test it on a machine, but I will do it very soon and I will report the result here . Many thanks again – alessio89g Mar 13 '21 at 13:48
  • I tested it ahead of schedule and it works flawlessly! Many thanks! – alessio89g Mar 13 '21 at 20:51
  • Note that `PCSystemType` (and `Win32_SystemEnclosure`) rely on the mfg properly coding the hardware, and they seem to be decent at it, but beware that it may not be 100% reliable. I saw someone mention that checking for e.g. SODIMM might be a good confirmation – Yorik Mar 17 '21 at 21:44
  • Yes, i thought it could happen, but it would be a rare case; not a big problem, in that case simply I will uninstall the app. For most of the cases will be ideal – alessio89g Mar 18 '21 at 16:54
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."}

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 26 '22 at 17:19
  • I don't need to get the differences of all that types of computer; My script it's enough for my needs. – alessio89g Apr 27 '22 at 07:17
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

  • 1
    This does not provide an answer to the question. Once you have sufficient [reputation](https://superuser.com/help/whats-reputation) you will be able to [comment on any post](https://superuser.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/1123605) – music2myear May 11 '22 at 01:03