evetsleep

Example of getting system info

Feb 20th, 2015
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [CmdletBinding()]Param(
  2.     [Parameter( Position                        = 0,
  3.                 Mandatory                       = $false,
  4.                 ValueFromPipeline               = $true,
  5.                 ValueFromPipelineByPropertyName = $true
  6.                 )]
  7.         [String[]]$ComputerName = 'localhost'
  8. )
  9.  
  10. begin{
  11.     $remoteQueue = New-Object System.Collections.ArrayList
  12.     $wmiQueue = New-Object System.Collections.ArrayList
  13.    
  14.     foreach($computer in $ComputerName){
  15.         try{
  16.             Test-WSMan -ComputerName $computer -ErrorAction STOP | Out-Null
  17.         }
  18.         catch{
  19.             Write-Verbose -Message ('Using WMI: {0}' -f $computer)
  20.             [void]$wmiQueue.Add($computer)
  21.             continue
  22.         }
  23.        
  24.         Write-Verbose -Message ('Using Remoting: {0}' -f $computer)
  25.         [void]$remoteQueue.Add($computer)
  26.     }
  27.    
  28. }
  29.  
  30. process{
  31.     if($remoteQueue.count -gt 0){
  32.    
  33.         $localWMI = {
  34.             $wmiParam = @{
  35.                 ComputerName    = 'localhost'
  36.                 ErrorAction     = 'STOP'
  37.             }
  38.            
  39.             try{
  40.                 $wmiSystem      = Get-WmiObject -Class Win32_ComputerSystem @wmiParam
  41.                 $wmiMemory      = Get-WmiObject -Class Win32_PhysicalMemory @wmiParam
  42.                 $wmiProcessor   = Get-WmiObject -Class Win32_Processor @wmiParam
  43.                 $wmiSysInfo     = Get-WmiObject -Class MS_SystemInformation -Namespace root\wmi @wmiParam
  44.                 $wmiLDisk       = Get-WmiObject -Class Win32_LogicalDisk -Filter "DeviceID='C:'" @wmiParam
  45.                 $wmiEnclosure   = Get-WmiObject -Class Win32_SystemEnclosure @wmiParam
  46.                 $wmiNetwork     = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE @wmiParam
  47.             }
  48.             catch{
  49.                 Write-Warning -Message ('Unable to collect WMI data for {0}: {1}' -f $env:ComputerName,$_.exception.message)
  50.                 return
  51.             }
  52.  
  53.             $wmiData = New-Object -TypeName PSObject -Property @{
  54.                 ComputerName    = $wmiSystem.Name
  55.                 Manufacturer    = $wmiSystem.Manufacturer
  56.                 Model           = $wmiSystem.Model
  57.                 ProductNumber   = $wmiSysInfo.SystemSKU
  58.                 SerialNumber    = $wmiEnclosure.SerialNumber
  59.                 Processor       = $wmiProcessor | foreach { $_.name }
  60.                 Memory          = [Math]::Round((($wmiMemory | Measure-Object -Property Capacity -Sum).sum/1gb),2)
  61.                 HDD             = [Math]::Round((($wmiLDisk | Measure-Object -Property Size -Sum).sum/1gb),2)
  62.                 IPAddresses     = $wmiNetwork | foreach { $_.IPAddress }
  63.             }
  64.            
  65.             Write-Output $wmiData
  66.         }
  67.        
  68.         Invoke-Command -ComputerName $remoteQueue -ScriptBlock $localWMI | Select-Object ComputerName,Manufacturer,Model,ProductNumber,SerialNumber,Processor,Memory,HDD,IPAddresses
  69.     }
  70.  
  71.     foreach ($computer in $wmiQueue){
  72.         $wmiParam = @{
  73.             ComputerName    = $computer
  74.             ErrorAction     = 'STOP'
  75.         }
  76.        
  77.         try{
  78.             $wmiSystem      = Get-WmiObject -Class Win32_ComputerSystem @wmiParam
  79.             $wmiMemory      = Get-WmiObject -Class Win32_PhysicalMemory @wmiParam
  80.             $wmiProcessor   = Get-WmiObject -Class Win32_Processor @wmiParam
  81.             $wmiSysInfo     = Get-WmiObject -Class MS_SystemInformation -Namespace root\wmi @wmiParam
  82.             $wmiLDisk       = Get-WmiObject -Class Win32_LogicalDisk -Filter "DeviceID='C:'" @wmiParam
  83.             $wmiEnclosure   = Get-WmiObject -Class Win32_SystemEnclosure @wmiParam
  84.             $wmiNetwork     = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE @wmiParam
  85.         }
  86.         catch{
  87.             Write-Warning -Message ('Unable to collect WMI data for {0}: {1}' -f $computer,$_.exception.message)
  88.             continue
  89.         }
  90.        
  91.         $wmiData = New-Object -TypeName PSObject -Property @{
  92.             ComputerName    = $wmiSystem.Name
  93.             Manufacturer    = $wmiSystem.Manufacturer
  94.             Model           = $wmiSystem.Model
  95.             ProductNumber   = $wmiSysInfo.SystemSKU
  96.             SerialNumber    = $wmiEnclosure.SerialNumber
  97.             Processor       = $wmiProcessor | foreach { $_.name }
  98.             Memory          = [Math]::Round((($wmiMemory | Measure-Object -Property Capacity -Sum).sum/1gb),2)
  99.             HDD             = [Math]::Round((($wmiLDisk | Measure-Object -Property Size -Sum).sum/1gb),2)
  100.             IPAddresses     = $wmiNetwork | foreach { $_.IPAddress }
  101.         }
  102.        
  103.         Write-Output $wmiData | Select-Object ComputerName,Manufacturer,Model,ProductNumber,SerialNumber,Processor,Memory,HDD,IPAddresses
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment