Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
432 views
in Technique[技术] by (71.8m points)

powershell - Get-WmiObject to combine to outputs

I was executing below script to obtain the server patch version of Skype for business servers.

I need the output as server patch name, version and computername.

$x = Get-Content "E:empservers.txt" 
foreach ($y in $x) 
{
Invoke-Command -ComputerName $y -scriptblock {Get-WmiObject -query ‘select name, version from win32_product’ | where {$_.name -like “*Skype for Business server 2015, core*”}} | Select-object name, Version, @{Name='ComputerName';Expression={$y}} | ft -AutoSize
}

But I am receiving output as below

Name                                            Version      ComputerName   
----                                            -------      ------------   
Skype for Business Server 2015, Core Components 6.0.9319.598 D221412xxxxxx

Name                                            Version      ComputerName   
----                                            -------      ------------   
Skype for Business Server 2015, Core Components 6.0.9319.598 D221412xxxxxxxx

Name                                            Version      ComputerName   
----                                            -------      ------------   
Skype for Business Server 2015, Core Components 6.0.9319.598 D221412xxxxxx



Name                                            Version      ComputerName   
----                                            -------      ------------   
Skype for Business Server 2015, Core Components 6.0.9319.598 D221412xxxxxxx


Name                                            Version      ComputerName   
----                                            -------      ------------   
Skype for Business Server 2015, Core Components 6.0.9319.598 D221412xxxxxx

I don't need my header tiles in every line of output. Any suggestions?

question from:https://stackoverflow.com/questions/65878997/get-wmiobject-to-combine-to-outputs

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  • You are getting headers for each computer because the select statement is inside the foreach loop.

  • Invoke-command accepts multiple computers so you dont need a foreach loop.

  • use server-side filtering where possible.

    $x = Get-Content "E:empservers.txt" 
    Invoke-Command -ComputerName $x -scriptblock {Get-WmiObject -query "select name, version from win32_product where name like 'Skype for Business server 2015, Core%'"} | 
     Select-object PSComputerName,name, Version
    

For future:

  • Use Get-CimInstance because Get-wmiobject is deprecated.
  • Do not use win32_product because it can potentially lead to msi corruption. Use the registry instead.

https://community.idera.com/database-tools/powershell/powertips/b/tips/posts/find-installed-software


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...