Advertisement
Lee_Dailey

SystemInfo - v-4

Sep 10th, 2018
1,364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # playing with SystemInfo - v-4
  2.  
  3. # save the old Information Pref
  4. $Old_I_Pref = $InformationPreference
  5. # enable Information output
  6. $InformationPreference = 'Continue'
  7.  
  8. #region >> FunctionList
  9. function Get-PCSystemTypeName
  10.     {
  11.     [CmdletBinding()]
  12.     Param
  13.         (
  14.         [Parameter (
  15.             Mandatory,
  16.             Position = 0
  17.             )]
  18.             [int]
  19.             $PCSystemType
  20.         )
  21.     switch ($PCSystemType)
  22.         {
  23.         0 {'Unspecified'; break}
  24.         1 {'Desktop'; break}
  25.         2 {'Laptop_Or_Mobile'; break}
  26.         3 {'Workstation'; break}
  27.         4 {'Enterprise_Server'; break}
  28.         5 {'SOHO_Server'; break}
  29.         6 {'Appliance_PC'; break}
  30.         7 {'Performance_Server'; break}
  31.         8 {'Maximum'; break}
  32.         default {'Unknown_ID'}
  33.         }
  34.     }
  35. #endregion >> FunctionList
  36.  
  37. #region >> create & fill in the properties
  38. Write-Information 'Starting SysInfo collection ...'
  39.  
  40. Write-Information '    getting CIM_BIOSElement info ...'
  41. $CIM_BIOS = Get-CimInstance -ClassName CIM_BIOSElement
  42. Write-Information '    getting CIM_Processor info ...'
  43. $CIM_Processor = Get-CimInstance -ClassName CIM_Processor
  44. Write-Information '    getting CIM_PhysicalMemory info ...'
  45. $CIM_PhysicalMemory = Get-CimInstance -ClassName CIM_PhysicalMemory
  46. Write-Information '    getting CIM_ComputerSystem info ...'
  47. $CIM_ComputerSystem = Get-CimInstance -ClassName CIM_ComputerSystem
  48. Write-Information '    getting CIM_OperatingSystem info ...'
  49. $CIM_OperatingSystem = Get-CimInstance -ClassName CIM_OperatingSystem
  50. Write-Information '    getting Win32_TimeZone info ...'
  51. $CIM_TimeZone = Get-CimInstance -ClassName Win32_TimeZone
  52. Write-Information '    getting CIM_LogicalDisk info ...'
  53. $CIM_LogicalDisk = Get-CimInstance -ClassName CIM_LogicalDisk |
  54.     Where-Object {$_.Name -eq $CIM_OperatingSystem.SystemDrive}
  55. Write-Information '    getting CIM_NetworkAdapter & Win32_NetworkAdapterConfiguration info ...'
  56. # this is a regex OR, so put a '|' between each item you want to exclude
  57. #$ExcludedNetAdapterList = 'Npcap|VirtualBox'
  58. $ExcludedNetAdapterList = 'Npcap'
  59. $CIM_NetAdapter = Get-CimInstance -ClassName CIM_NetworkAdapter
  60. $CIM_NetConfig = Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration |
  61.     Where-Object {
  62.         $_.IPEnabled -and
  63.         $_.Description -notmatch $ExcludedNetAdapterList
  64.         }
  65.  
  66.  
  67. $SysInfoProps = [ordered]@{}
  68.  
  69.  
  70. # these CPU items likely need to be changed for multi-CPU boxes
  71. $SysInfoProps.Add('CPU_Description', $CIM_Processor.Description)
  72. $SysInfoProps.Add('CPU_Maker', $CIM_Processor.Manufacturer)
  73. $SysInfoProps.Add('CPU_Name', $CIM_Processor.Name)
  74. $SysInfoProps.Add('CPU_LoadPct', $CIM_Processor.LoadPercentage)
  75. $SysInfoProps.Add('CPU_LogicalCoreCount', $CIM_ComputerSystem.NumberOfLogicalProcessors)
  76. $SysInfoProps.Add('CPU_SocketCount', $CIM_ComputerSystem.NumberOfProcessors)
  77. $SysInfoProps.Add('CPU_SocketDesignation', $CIM_Processor.SocketDesignation)
  78. $SysInfoProps.Add('CPU_Speed_Mhz', $CIM_Processor.MaxClockSpeed)
  79.  
  80.  
  81. $SysInfoProps.Add('Drive_System', $CIM_OperatingSystem.SystemDrive)
  82. $SysInfoProps.Add('Drive_System_FreeSpace_GB', '{0:N2}' -f ($CIM_LogicalDisk.FreeSpace / 1GB))
  83. $SysInfoProps.Add('Drive_System_FreeSpace_Pct', '{0:N0}' -f (($CIM_LogicalDisk.FreeSpace / $CIM_LogicalDisk.Size) * 100))
  84. $SysInfoProps.Add('Drive_System_Size_GB', '{0:N2}' -f ($CIM_LogicalDisk.Size / 1GB))
  85.  
  86.  
  87. $SysInfoProps.Add('Net_Domain', $CIM_ComputerSystem.Domain)
  88. $SysInfoProps.Add('Net_DomainJoined', $CIM_ComputerSystem.PartOfDomain)
  89. foreach ($CNC_Item in $CIM_NetConfig)
  90.     {
  91.     $Index = '{0:D2}' -f $CNC_Item.Index
  92.     $SysInfoProps.Add("Net_${Index}_AdapterIndex", $CNC_Item.Index)
  93.     $SysInfoProps.Add("Net_${Index}_AdapterName", $CNC_Item.Description)
  94.     $SysInfoProps.Add("Net_${Index}_AdapterManufacturer", $CIM_NetAdapter[$Index].Manufacturer)
  95.     # using 1e9 instead of 1GB since the NIST uses base 10
  96.     $SysInfoProps.Add("Net_${Index}_AdapterSpeed_Gbit", $CIM_NetAdapter[$Index].Speed / 1e9)
  97.  
  98.     $DefaultGateway = if ($CNC_Item.DefaultIPGateway -is [array])
  99.         {
  100.         $CNC_Item.DefaultIPGateway[0]
  101.         }
  102.         else
  103.         {
  104.         $Null
  105.         }
  106.     $SysInfoProps.Add("Net_${Index}_DefaultGateway", $DefaultGateway)
  107.  
  108.     $SysInfoProps.Add("Net_${Index}_DHCP_Enabled", $CNC_Item.DHCPEnabled)
  109.     $SysInfoProps.Add("Net_${Index}_DHCP_Server", $CNC_Item.DHCPServer)
  110.  
  111.     $SysInfoProps.Add("Net_${Index}_DNS_HostName", $CNC_Item.DNSHostName)
  112.  
  113.     # on my system the addresses are stored [0]=IPv4, [1]=IPv6
  114.     #    the [ipaddress] conversion fails with ipv6 subnet info
  115.     #    "64" becomes "0.0.0.64" [ipv4] instead of "64" or "::64" [ipv6]
  116.     $SysInfoProps.Add("Net_${Index}_IPv4_Address",
  117.         ([ipaddress[]]$CNC_Item.IPAddress).
  118.         Where({$_.AddressFamily -eq 'Internetwork'})  -join '; ')
  119.     $SysInfoProps.Add("Net_${Index}_IPv4_AddressSubnet", $CNC_Item.IPSubnet[0])
  120.     $SysInfoProps.Add("Net_${Index}_IPv4_DNS_Servers",
  121.         ([ipaddress[]]$CNC_Item.DNSServerSearchOrder).
  122.         Where({$_.AddressFamily -eq 'Internetwork'}) -join '; ')
  123.  
  124.     $SysInfoProps.Add("Net_${Index}_IPv6_Address",
  125.         ([ipaddress[]]$CNC_Item.IPAddress).
  126.         Where({$_.AddressFamily -eq 'InternetworkV6'})  -join '; ')
  127.     $SysInfoProps.Add("Net_${Index}_IPv6_AddressSubnet", $CNC_Item.IPSubnet[1])
  128.     # IPV6 DNS servers are shown in "ipconfig /all", but not with CIM
  129.     #    so this will be blank on my system
  130.     $SysInfoProps.Add("Net_${Index}_IPv6_DNS_Servers",
  131.         ([ipaddress[]]$CNC_Item.DNSServerSearchOrder).
  132.         Where({$_.AddressFamily -eq 'InternetworkV6'}) -join '; ')
  133.  
  134.     $SysInfoProps.Add("Net_${Index}_MAC_Address", $CNC_Item.MACAddress)
  135.     }
  136.  
  137.  
  138. $SysInfoProps.Add('OS_Architecture', $CIM_OperatingSystem.OSArchitecture)
  139. $CultureInfo = [System.Globalization.CultureInfo]::GetCultureInfo([int]$CIM_OperatingSystem.OSLanguage)
  140. $SysInfoProps.Add('OS_Language_DisplayName', $CultureInfo.DisplayName)
  141. $SysInfoProps.Add('OS_Language_LCID', $CultureInfo.LCID)
  142. $SysInfoProps.Add('OS_Language_Name', $CultureInfo.Name)
  143. $SysInfoProps.Add('OS_LastBootUpTime', $CIM_OperatingSystem.LastBootUpTime)
  144. $SysInfoProps.Add('OS_TimeZone', $CIM_TimeZone.Caption)
  145. $SysInfoProps.Add('OS_TimeZone_DST_Active', $CIM_ComputerSystem.DaylightInEffect)
  146. $SysInfoProps.Add('OS_TimeZone_Offset', $CIM_TimeZone.Bias)
  147. $SysInfoProps.Add('OS_TimeZone_Offset_Current', $CIM_OperatingSystem.CurrentTimeZone)
  148. $SysInfoProps.Add('OS_Version', $CIM_OperatingSystem.Version)
  149. $SysInfoProps.Add('OS_Version_BuildNumber', $CIM_OperatingSystem.BuildNumber)
  150. $SysInfoProps.Add('OS_Version_Caption', $CIM_OperatingSystem.Caption)
  151. $SysInfoProps.Add('OS_Version_ServicePack', $CIM_OperatingSystem.CSDVersion)
  152.  
  153.  
  154. # the original number returned is in KBytes, not Bytes
  155. $SysInfoProps.Add('RAM_Free_GB', [math]::Round($CIM_OperatingSystem.FreePhysicalMemory / 1MB, 2))
  156. # this presumes each RAM bank will run at the same speed - almost certainly true [*grin*]
  157. $SysInfoProps.Add('RAM_Speed_Mhz', $CIM_PhysicalMemory[0].Speed)
  158. # the original number returned is in Bytes
  159. $SysInfoProps.Add('RAM_Total_GB', [math]::Round($CIM_ComputerSystem.TotalPhysicalMemory / 1GB, 2))
  160.  
  161.  
  162. $SysInfoProps.Add('System_Manufacturer', $CIM_ComputerSystem.Manufacturer)
  163. $SysInfoProps.Add('System_Model', $CIM_ComputerSystem.Model)
  164. $SysInfoProps.Add('System_Name', $CIM_ComputerSystem.Name)
  165. $SysInfoProps.Add('System_PCSystemType', $CIM_ComputerSystem.PCSystemType)
  166. $PCSystemTypeName = Get-PCSystemTypeName -PCSystemType $CIM_ComputerSystem.PCSystemType
  167. $SysInfoProps.Add('System_PCSystemType_Name', $PCSystemTypeName)
  168. $SysInfoProps.Add('System_SerialNumber', $CIM_BIOS.SerialNumber)
  169. $SysInfoProps.Add('System_SystemType', $CIM_ComputerSystem.SystemType)
  170.  
  171. #endregion >> create & fill in the properties
  172.  
  173. $SysInfo = [PSCustomObject]$SysInfoProps
  174.  
  175. $SysInfo
  176.  
  177. # restore the Information Pref
  178. $InformationPreference = $Old_I_Pref
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement