2017-05-11 3 views
-1

컴퓨터 목록에 대한 컴퓨터 정보를 얻고 표에 추가하려고합니다. 어떻게 해시 테이블에서 데이터를 가져 와서 하나의 테이블에 추가 할 수 있는지 혼란스러워합니다. 아래는 제가 지금까지 가지고있는 코드입니다. 어떤 도움을 주시면 감사하겠습니다!여러 해시 테이블 값을 Powershell의 표 형식으로 변환해야합니다.

$name = Get-Content .\computers.txt 


foreach($c in $Name){ 


$test = Test-Connection -ComputerName $c -Count 1 -Quiet 

if($test -eq $true) 
{ 
$os = Get-WmiObject -Class win32_operatingsystem -ComputerName $c -ErrorAction SilentlyContinue 
$cs = Get-WmiObject -Class win32_computersystem -ComputerName $c -ErrorAction SilentlyContinue 
$pr = Get-WmiObject -Class win32_processor -ComputerName  $c -ErrorAction SilentlyContinue 
$bs = Get-WmiObject -Class win32_bios -ComputerName $c -ErrorAction SilentlyContinue 
$ps = Get-WmiObject -Class win32_processor -ComputerName  $c -ErrorAction SilentlyContinue 
$us = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $c -ErrorAction SilentlyContinue 



[hashtable]$osProperties = @{ 
    ‘Model’=$cs.model; 
    ‘Manufacturer’=$cs.manufacturer; 
    ‘RAM’=$cs.totalphysicalmemory/1GB -as [int] ; 
    ‘Cores’=$cs.numberoflogicalprocessors; 
    ‘SystemType’=$cs.SystemType 
    ‘CPU’=$pr.name; 
    ‘Serial’=$bs.serialnumber 
    ‘OSVersion’=$os.caption; 
    'User'=$us.username; 
    } 



    #$obj = New-Object -TypeName PSCustomObject -Property $osProperties | select Manufacturer, Model, Serial, OSVersion, CPU, Cores, @{name="Ram (GB)";expression={$($_.Ram)}}, User | ft 

    } 




else{ 
$c=$Null 
} 

} 

답변

0

시작하려면 한 번에 하나의 컴퓨터를 실행하지 마십시오. 사이클을 낭비하고 있습니다 ...

$name = Get-Content .\computers.txt 
#Get a list of online servers 
$online = Test-Connection $name -count 1 -ea 4|% Address 
#Define what we want to do on those servers 
$ScriptBlock = { 
    $os = Get-WmiObject -Class win32_operatingsystem -ErrorAction SilentlyContinue 
    $cs = Get-WmiObject -Class win32_computersystem -ErrorAction SilentlyContinue 
    $pr = Get-WmiObject -Class win32_processor -ErrorAction SilentlyContinue 
    $bs = Get-WmiObject -Class win32_bios -ErrorAction SilentlyContinue 
    $ps = Get-WmiObject -Class win32_processor -ErrorAction SilentlyContinue 

    [PSCustomObject]@{ 
     ‘Model’=$cs.model; 
     ‘Manufacturer’=$cs.manufacturer; 
     ‘RAM’=$cs.totalphysicalmemory/1GB -as [int] ; 
     ‘Cores’=$cs.numberoflogicalprocessors; 
     ‘SystemType’=$cs.SystemType 
     ‘CPU’=$pr.name; 
     ‘Serial’=$bs.serialnumber 
     ‘OSVersion’=$os.caption; 
     'User'=$cs.username; 
    } 
} 
#Execute the scriptblock on each server, returning results as objects 
$Results = Invoke-Command -scriptblock $ScriptBlock -ComputerName $Online 
#Display those objects as a table 
$Results | FT