英文:
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 *
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论