获取包含作业位置和作业结果的表格的Powershell方法是什么?

huangapple go评论65阅读模式
英文:

Powershell - how to get Location of job with job result in one table?

问题

我想在一个表格中获取带有PC名称的远程PC的序列号。
$PCNameArray是我想要获取序列号的远程PC的数组。

$complete = $false
$arrayJobs = @()

Foreach($a in $PCNameArray) {
    $arrayJobs += Get-WmiObject -ComputerName $a -Class Win32_BIOS -asjob
}

while (-not $complete) {
    $arrayJobsInProgress = $arrayJobs |
        Where-Object { $_.State -match running }
    if (-not $arrayJobsInProgress) { 所有任务已完成 ; $complete = $true }
}

$result = $arrayJobs | Receive-Job

# 添加PC名称到结果中
$resultWithPCNames = $result | Select-Object @{Name="PCName";Expression={$PCNameArray[$_]}}, SerialNumber

$resultWithPCNames

这段代码将为您提供带有PC名称的序列号。感谢您的提问。

英文:

I want to get serial numbers of remote PC's with the name of PC, in one table.
$PCNameArray is array of remote PC I want to get serial numbers.

$complete = $false
$arrayJobs = @()

Foreach($a in $PCNameArray) {
    $arrayJobs += Get-WmiObject -ComputerName $a -Class Win32_BIOS -asjob
}

while (-not $complete) {
    $arrayJobsInProgress = $arrayJobs |
        Where-Object { $_.State -match ‘running’ }
    if (-not $arrayJobsInProgress) { “All Jobs Have Completed” ; $complete = $true }
}

$arrayJobs | Receive-Job

this code will give me only serial numbers. How can I add each PC names to this result?
Thank you.

答案1

得分: 1

以下是翻译好的内容:

计算机名称作为返回的WMI对象的属性返回,只是默认情况下不显示。您可以这样显示它:

$arrayJobs = Get-WmiObject -ComputerName $PCNameArray -Class Win32_BIOS -asjob

Wait-Job $arrayJobs

$arrayJobs |
    Receive-Job |
    Format-Table PsComputerName, SerialNumber -AutoSize

这会产生如下输出:

PSComputerName SerialNumber                    
-------------- ------------                    
Server1        0000-0000-2620-2195-4000-8654-21
Server2        0000-0013-3219-1963-9428-8113-87
Server3        0000-0004-9473-4695-9509-1141-89

PsComputerName由PowerShell添加到WMI对象中,但对象本身还有一个__Server属性,其中也包含计算机名称。您可以查看所有返回的内容,将上面示例中的Format-Table PsComputerName, SerialNumber -AutoSize替换为Format-List *

英文:

The computer name is returned as a property of the returned WMI object, it's just not shown by default. You can show it like this:

$arrayJobs = Get-WmiObject -ComputerName $PCNameArray -Class Win32_BIOS -asjob

Wait-Job $arrayJobs

$arrayJobs |
    Receive-Job |
    Format-Table PsComputerName, SerialNumber -AutoSize

This gives output like this:

PSComputerName SerialNumber                    
-------------- ------------                    
Server1        0000-0000-2620-2195-4000-8654-21
Server2        0000-0013-3219-1963-9428-8113-87
Server3        0000-0004-9473-4695-9509-1141-89

PsComputerName is added to the WMI object by PowerShell, but the object itself has a __Server property that also contains the computer name. You can see everything that is returned by replacing Format-Table PsComputerName, SerialNumber -AutoSize in the above example with Format-List *

huangapple
  • 本文由 发表于 2023年7月18日 14:53:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76710186.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定