powershell计算机资源清册



我必须识别我们组织中的所有笔记本电脑。名为"computers"的单个OU。所有计算机都以使用它的用户的名称命名。没有有关计算机类型的信息。为了只在笔记本电脑上部署软件,我必须"隔离"它们。我运行了以下脚本。

$ErrorActionPreference = "silentlyContinue"
$coms=Get-ADComputer -Filter * -SearchBase "CN=Computers,DC=domain,DC=com" |select -exp name
Foreach($com in $coms)
{
Get-WmiObject win32_computersystem -comp $com  | Select-Object PCSystemType,Name,Manufacturer,Model | format-table
} 

效果很好。但分类不那么容易。

我想让它变得更好,并运行了以下程序:

$ErrorActionPreference = "silentlyContinue"
$coms=Get-ADComputer -Filter * -SearchBase "CN=Computers,DC=domain,DC=com" |select -exp name
Foreach($com in $coms)
{
Get-WmiObject win32_computersystem -comp $com| Select-Object PCSystemType,Name,Manufacturer,Model | format-table
if ($com.PCSystemType -eq '2'){Write-host "$com is a laptop"} 
else {Write-host "$com is a desktop"} 
}

我现在有这样的结果:

COMP1 is a desktop
COMP2 is a desktop
COMP3 is a desktop
COMP4 is a desktop
LAPTOP1 is a desktop
LAPTOP2 is a desktop
LAPTOP3 is a desktop

根据我的脚本,笔记本电脑和台式机都是台式机。我做错了什么?任何小费都会有帮助!感谢

Format-*cmdlet的目的是创建到控制台的漂亮输出;以便于人类阅读。它通过创建不同的对象来完成目的。因此,您有不可用的对象,并且本质上是在访问$null

删除| Format-Table将解决您的问题。

if循环中对WMI查询进行求值之前,需要将其值存储在变量中。

略有修改的版本:

$coms = @("localhost")
Foreach($com in $coms) {
$wmi = Get-WmiObject win32_computersystem -comp $com
switch ($wmi.PCSystemType) {
'0' { "$com is a Unspecified " }
'1' { "$com is a Desktop " }
'2' { "$com is a Mobile " }
'3' { "$com is a Workstation " }
'4' { "$com is a Enterprise Server" }
'5' { "$com is a SOHO Server" }
'6' { "$com is a Appliance PC" }
'7' { "$com is a Performance Server" }
'8' { "$com is a Maximum" }
Default { "Unable to get correct value for $com" }
}
}

最新更新