【问题标题】:i trying to collect different info with Get-ADComputer, Get-WmiObject and Get-windowsfeature我尝试使用 Get-ADComputer、Get-WmiObject 和 Get-windowsfeature 收集不同的信息
【发布时间】:2021-12-21 15:31:45
【问题描述】:

我正在尝试从我的 AD 中列出的所有计算机收集不同的属性,但我无法获取重新收集查询的格式

我运行 get-adcomputer 查询

$computerList = Get-ADComputer -Filter 'Name -like "SCPW*" -and ObjectClass -eq "Computer"' | Select-Object -Property Name,objectClass` 

我从 $computerlist 中列出的所有计算机收集信息

$list = foreach ($computer in $computerList) {Get-WmiObject -Class Win32_Product | Select-Object -Property Name }
$WFeature = foreach ($computer in $computerList) {Get-windowsfeature | Where {$_.installed -Eq $true} | Select-Object -Property Name}
$Wrole = foreach ($computer in $computerList) {Get-WmiObject -Class Win32_ServerFeature -Property * | select pscomputername,name }

最后,我尝试将收集到的所有信息列在一个独特的表格中

%{[PSCustomObject]@{
        'ComputerName' = $computer.Name
        'features'     = $WFeature.name
        'Role'    = $Wrole.name
        'list'    = $list.name
    } } | Format-table

但是列出的表格只有一行

ComputerName features                                                                  Role                                                                                       list            
------------ --------                                                                  ----                                                                                       ----            
SCPWEXC0101  {File-Services, FS-FileServer, Remote-Desktop-Services, RDS-RD-Server...} {Web Server (IIS), File Services, Print and Document Services, Remote Desktop Services...} {Microsoft Ap...

the table listed only in one line (screenshot)

如果有人给我一些帮助,我真的会非常感激,我正在学习 PS,我真的很迷茫这个查询。我一直在运行 Ps 5.1

谢谢!!

【问题讨论】:

  • 你肯定不想使用Select-Object -Property Name,否则你将失去$list$WFeature$Wrole之间的相关性。

标签: powershell active-directory get-wmiobject


【解决方案1】:

您必须将查询放入每台计算机的循环中,以保持收集的信息与计算机名称之间的相关性。这样的事情应该可以帮助您入门:

$computerList = Get-ADComputer -Filter 'Name -like "SCPW*" -and ObjectClass -eq "Computer"'
$Result = 
foreach ($computer in $computerList) { 
    $CimSession = New-CimSession -ComputerName $computer.name

    [PSCustomObject]@{
        ComputerName         = $computer.Name
        WindowsFeatureList   = (Get-windowsfeature -ComputerName $computer.name | Where-Object { $_.installed } | Select-Object -Property Name) -join ', '
        W32ServerFeatureList = (Get-CimInstance -CimSession $CimSession -ClassName Win32_ServerFeature | Select-Object -Property Name) -join ', '
        W32ProductList       = (Get-CimInstance -CimSession $CimSession -ClassName Win32_Product | Select-Object -Property Name) -join ', '
    }
}
$Result

【讨论】:

  • 同意,如果他需要为每个对象输出 1 个获胜功能、服务器功能和产品,则另一种选择是 for 循环以组合 3 个生成的 arrays
  • @SantiagoSquarzon OK,但是这三个循环不查询远程计算机。他们多次查询本地计算机,因为$Computerlist 中有计算机;-)
  • 是的,我的意思是从你的代码 sn-p,而不是使用 -join 替代方法是将 3 个结果保存在变量中,然后 for 循环它们。
  • @SantiagoSquarzon 啊……我明白了……你说得对。有改进的余地。 ;-) 这可能是亚历杭德罗的下一步。 ;-)
猜你喜欢
  • 2016-11-29
  • 1970-01-01
  • 1970-01-01
  • 2021-08-22
  • 2021-07-02
  • 2013-05-02
  • 1970-01-01
  • 2019-09-09
  • 1970-01-01
相关资源
最近更新 更多