我发现这个 PS 脚本可以检查 PC 是台式机还是笔记本电脑
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"
}
Run Code Online (Sandbox Code Playgroud)
如果结果是“笔记本电脑”,我需要运行这个其他命令
Add-AppxPackage -Path ".\28671Petrroll.PowerPlanSwitcher_0.4.4.0_x86__ge82akyxbc7z4.Appx"
Run Code Online (Sandbox Code Playgroud)
否则跳过它。我怎样才能把它们结合起来?
编辑:
似乎我需要互联网连接才能完全安装该应用程序;没有互联网,只要我通过互联网连接运行该应用程序,该应用程序就不会启动。有人知道在没有互联网连接的情况下我需要做什么吗?或者这是不可能的?
这是您的代码的清理后更具可读性的版本:
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"
}
Run Code Online (Sandbox Code Playgroud)
编辑:建议从 切换Get-WmiObject
到Get-CimInstance
. 在这种情况下,该命令将如下所示:
$HardwareType = (Get-CimInstance -Class Win32_ComputerSystem -Property PCSystemType).PCSystemType
Run Code Online (Sandbox Code Playgroud)
原因如下:
WMI cmdlet 和 CIM cmdlet 之间的最大区别在于 CIM cmdlet 使用 WSMAN (WinRM) 连接到远程计算机。与创建 PowerShell 远程处理会话的方式相同,您可以使用这些 cmdlet 创建和管理 CIM 会话。
WMI cmdlet 的最大缺点是它们使用 DCOM 来访问远程机器。DCOM 不是防火墙友好的,可以被网络设备阻止,并且在出现问题时会出现一些神秘的错误。
来源:https : //devblogs.microsoft.com/scripting/should-i-use-cim-or-wmi-with-windows-powershell/
归档时间: |
|
查看次数: |
398 次 |
最近记录: |