Advertisement
Lee_Dailey

playing with SystemInfo - v-3

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