Here we have GetHostInfo.ps1 – a full-featured wmi query script that you can use to retrieve data from a remote (or local) machine. The script can pull details from the following WMI classes:
- Win32_OperatingSystem
- Win32_ComputerSystem
- Win32_NetworkAdapter plus associated Win32_NetworkAdapterConfiguration
(this is done by associating Win32_NetworkAdapter.DeviceId with Win32_NetworkAdapterConfiguration.Index) - Win32_LogicalDisk
- Win32_PhysicalMemory
- Win32_Processor
- Win32_PageFileSetting
The script can also query the local administrators group members using ADSI. Note that ADSI calls does not have the ability to use alternate credentials so you can only query this information using your current logged-on credentials. If the logged-on credential don’t have access, you will get an “access denied” error on the ADSI calls.
All data is nicely displayed in human-readable format; e.g. Disk Space and Memory translated to GigaBytes and Tabular output where appropriate. You also have the ability to use alternate credentials if necessary (using the -Cred parameter). The -Cred parameter can be used interactively or with an encoded credentials file (see my other post for details on the Export-PSCredential and Import-PSCredential functions) .
One other note, the Win32_Processor output is in tabular format and can be quite wide; to avoid having data truncated, I suggest setting your console window width to about 160 characters.
Here are some sample outputs:
Win32_OperatingSystem
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Caption : Microsoft Windows 7 Ultimate N Version : 6.1.7601 csdversion : Service Pack 1 csname : WIN7DEV01 SystemDirectory : C:\Windows\system32 Organization : RegisteredUser : xb90 numberofusers : 1 TotalVisibleMemorySize : 3.00 GB TotalVirtualMemorySize : 6.00 GB InstallDate : 11/6/2009 9:39:51 PM LastBootUpTime : 3/25/2011 11:07:50 AM LocalDateTime : 3/25/2011 11:47:06 AM CurrentTimeZone : -600 (offset in minutes from GMT) SystemUptime : 0 days 0 hours 39 minutes |
Win32_ComputerSystem
|
1 2 3 4 5 6 7 8 |
Caption : PT01 Description : AT/AT COMPATIBLE DNSHostName : PT01 Domain : WORKGROUP manufacturer : VMware, Inc. model : VMware Virtual Platform numberofprocessors : 1 systemtype : X86-based PC |
Win32_NetworkAdpater + Win32_NetworkAdapterConfiguration
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
Active Adapter Found (win32_NetworkAdapter): DeviceId : 14 AdapterType : Ethernet 802.3 Description : Intel(R) PRO/1000 MT Network Connection NetConnectionId : Local Area Connection 2 ProductName : Intel(R) PRO/1000 MT Network Connection ServiceName : E1G60 Configuration (win32_NetworkAdapterConfiguration): Index : 14 DNSHostName : win7dev01 Caption : [00000014] Intel(R) PRO/1000 MT Network Connection Description : Intel(R) PRO/1000 MT Network Connection IPAddress : {192.168.61.130, fe80::6d88:1d71:7e56:a4c5} DHCPEnabled : True DefaultIPGateway : {192.168.61.2} DNSServerSearchOrder : {192.168.61.2} WINSPrimaryServer : WINSSecondaryServer : macaddress : 00:0C:29:BD:D5:04 DNSDomainSuffixSearchOrder: =========================== 1 : localdomain |
Win32_LogicalDisk
|
1 2 3 4 5 6 |
DeviceId VolumeName Description DriveType FreeSpace(GB) Size(GB) -------- ---------- ----------- --------- ------------- -------- A: 3 1/2 Inch Floppy Drive 2 0.00 0.00 C: Local Fixed Disk 3 41.08 63.90 D: CD-ROM Disc 5 0.00 0.00 E: CD-ROM Disc 5 0.00 0.00 |
Win32_PhysicalMemory
|
1 2 3 4 |
DeviceLocator Capacity DataWidth ------------- -------- ----------- RAM slot #0 2.00 GB 32 Bits RAM slot #1 1.00 GB 32 Bits |
Win32_Processor
|
1 2 3 4 |
Device Socket Clock Ext Cache Address Id Designation Caption Speed Clock Family Size Width Name Manufacturer ------ --------------- --------- --------- ------ ------- ------ -------- ------ -------------- CPU0 CPU socket #0 x64 Family 6 Model 23 Steppin... 2389 MHz 17 16384 32 bit Intel(R) Core(TM)2 Duo CPU P8600 ... GenuineIntel |
So, the best way to get started with this is to download the script and start using it.
Be aware that you can always use the “-Help” parameter for usage details:
|
1 |
<strong>./GetHostInfo.ps1 -Help</strong> |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
Param ([string]$HostName="localhost",$Option=1, $Cred=$null, [switch]$Help) $HelpText = @' Name : GetHostInfo.ps1 Date : 02/25/2011 Author : xb90@PoshTips.com Site : http://poshtips.com Purpose : Use WMI to lookup various host details Usage ./GetHostInfo [-HostName ][-Option |ALL][-Cred=|][-Help] where -HostName The host from which information will be queried (using WMI) and displayed to the console -Option Used to specify a specific Win32 Class where the value can be one or more of the following: 1 : Win32_OperatingSystem 2 : Win32_ComputerSystem 3 : Win32_NetworkAdapterConfiguration 4 : Win32_LogicalDisk 5 : Win32_PhysicalMemory 6 : Win32_Processor 7 : Win32_PageFileSetting 99 : Local Administrators Group members (using ADSI) Note: ADSI calls will ALWAYS use your logged-on credentials ALL : Show all options (multiple values must be comma-delimited) -Cred Enables use of alternate credentials that will be applied to the WMI queries if Filename is used, contents must originate from Export-Credential function otherwise user will be prompted to enter credentials interactively If credentials are passed via file, the Import-Credential function must be loaded into memory and setup with the alias "icred" -Help produces this help output '@ $MaintNotes = @' Maintenance Log Date By Updates ---------- ---- --------------------------------------------------------------------- 03/25/2011 xb90 New Script '@ if ($help){ $HelpText; exit } if ($cred){ if (test-path($Cred)){ $cred = icred -path $Cred } else{ write-host "Please enter your user credentials" -fore red $usr = read-host "Enter user account; e.g. `"mydomain\myname`" " $pwd = read-host "Password for $usr " -assecurestring $cred = new-object system.management.automation.pscredential($usr,$pwd) } } function HasVal{ param($array,$item) foreach ($i in $array){ if ($i -eq "all"){ $true return } if ($i -eq $item){ $true return } } $false } function BldQry { param ([string]$CmdStr) if ($cred){ "$cmdStr -credential `$cred" } else{ $cmdStr } } function WMIDateStringToDate($Bootup){ [System.Management.ManagementDateTimeconverter]::ToDateTime($Bootup) } function WriteHeader{ param ($hn, $cl, $op) write-host " $hn " -nonewline -back yellow -fore red write-host " $cl (option $op) ".padright(80-$($hn.length+2)) -back darkgreen -fore white } if (HasVal $Option 1){ WriteHeader $hostname "Win32_OperatingSystem" 1 $x = BldQry "gwmi win32_OperatingSystem -computer `$hostname" | iex $Uptime = (WMIDateStringToDate($x.localdatetime)) - (WMIDateStringToDate($x.LastBootUpTime)) $x | fl Caption,Version,csdversion,csname,SystemDirectory,Organization,RegisteredUser,numberofusers, ` @{Label="TotalVisibleMemorySize"; Expression={"{0:N2} GB" -f ($($_.TotalVisibleMemorySize)/1MB)}}, ` @{Label="TotalVirtualMemorySize"; Expression={"{0:N2} GB" -f ($($_.TotalVirtualMemorySize)/1MB)}}, ` @{Label="InstallDate"; Expression={"{0}" -f (WMIDateStringToDate(($_.InstallDate)))}}, ` @{Label="LastBootUpTime"; Expression={"{0}" -f (WMIDateStringToDate(($_.LastBootUpTime)))}}, ` @{Label="LocalDateTime"; Expression={"{0}" -f (WMIDateStringToDate(($_.LocalDateTime)))}}, ` @{Label="CurrentTimeZone"; Expression={"{0} (offset in minutes from GMT)" -f ($_.CurrentTimeZone)}}, ` @{Label="SystemUptime"; Expression={"{0} days {1} hours {2} minutes" -f ($uptime.days,$uptime.hours,$uptime.minutes)}} } if (HasVal $Option 2){ WriteHeader $hostname "Win32_ComputerSystem" 2 $x = BldQry "gwmi Win32_ComputerSystem -computer `$hostname" | iex $x | fl Caption,CSName,Description,Version,DNSHostName,Domain,manufacturer,model,numberofprocessors,systemtype } if (HasVal $Option 3){ WriteHeader $hostname "Win32_NetworkAdapter `& Win32_NetworkAdapterConfiguration" 3 # two step lookup: # (1) get the active adapter(s) # (2) lookup configuration for each adapter #if (!$cred){$adapters = gwmi -query "SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionStatus=2" -computer $hostname} #else {$adapters = gwmi -query "SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionStatus=2" -computer $hostname -credential $cred} $adapters = BldQry "gwmi -query `"SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionStatus=2`" -computer $hostname" | iex Foreach ($adapter in $adapters){ Write-host " Active Adapter Found (win32_NetworkAdapter):".PadRight(60," ") -backgroundcolor cyan -foregroundcolor black $adapter | fl DeviceId,AdapterType,Description,NetConnectionId,ProductName,ServiceName $qry = [string]::format("SELECT * FROM Win32_NetworkAdapterConfiguration where IPEnabled='True' and index={0}", $adapter.DeviceId) $x = BldQry "gwmi -query `$qry -computer `$hostname" | iex foreach ($entry in $x){ Write-host " Configuration (win32_NetworkAdapterConfiguration):".PadRight(60," ") -backgroundcolor cyan -foregroundcolor black $entry | fl Index,DNSHostName,Caption,Description,IPAddress,DHCPEnabled,DefaultIPGateway,DNSServerSearchOrder,WINSPrimaryServer,WINSSecondaryServer,macaddress | out-default "DNSDomainSuffixSearchOrder:" | out-default "===========================" | out-default $suffix_cnt = 0 foreach ($suffix in $entry.DNSDomainSuffixSearchOrder){ $suffix_cnt += 1 " $suffix_cnt : $suffix" | out-default } } "" } } if (HasVal $Option 4){ WriteHeader $hostname "Win32_LogicalDisk" 4 $wq = BldQry "gwmi -class win32_logicaldisk -computer $hostname" $x = $wq | iex $x | format-table -autosize ` DeviceId, ` VolumeName, Description, DriveType, ` @{Label="FreeSpace(GB)"; Alignment="right"; Expression={"{0:N2}" -f ($_.FreeSpace/1GB)}}, ` @{Label="Size(GB)"; Alignment="right"; Expression={"{0:N2}" -f ($_.size/1GB)}} ` | out-default } if (HasVal $Option 5){ WriteHeader $hostname "Win32_PhysicalMemory" 5 $x = BldQry "gwmi -class Win32_PhysicalMemory -computer `$hostname" | iex $x | format-table -autosize ` DeviceLocator, ` @{Label="Capacity"; Alignment="right"; Expression={"{0:N2} GB" -f ($_.Capacity/1GB)}}, ` @{Label="DataWidth "; Alignment="right"; Expression={"{0} Bits" -f ($_.datawidth)}}, ` @{Label="Speed"; Alignment="right"; Expression={" {0} MHz" -f ($_.speed)}} ` | out-default } if (HasVal $Option 6){ WriteHeader $hostname "Win32_Processor" 6 $x = BldQry "gwmi -class Win32_Processor -computer `$hostname" | iex $x | format-table ` @{Label="`nDevice`nId"; Alignment="left"; width=6; Expression={"{0}" -f ($_.DeviceId)}}, ` @{Label="`nSocket`nDesignation"; Alignment="left"; width=15; Expression={"{0}" -f ($_.SocketDesignation)}}, ` @{Label="`n`nCaption"; Alignment="left"; width=32; Expression={"{0}" -f ($_.Caption)}}, ` @{Label="Max`nClock`nSpeed"; Alignment="right"; width=9; Expression={"{0} MHz" -f ($_.MaxClockSpeed)}}, ` @{Label="`nExt`nClock"; Alignment="right"; width=6; Expression={"{0}" -f ($_.ExtClock)}}, ` @{Label="`n`nFamily"; Alignment="right"; width=7; Expression={"{0}" -f ($_.Family)}}, ` @{Label="L2`nCache`nSize"; Alignment="right"; width=6; Expression={"{0}" -f ($_.L2CacheSize)}}, ` @{Label="`nAddress`nWidth"; Alignment="right"; width=8; Expression={"{0} bit" -f ($_.AddressWidth)}}, ` @{Label="`n`nName"; Alignment="left"; width=36; Expression={"{0}" -f ([regex]::replace($_.Name,"\s+"," "))}}, ` @{Label="`n`nManufacturer"; Alignment="left"; width=18; Expression={"{0}" -f ($_.Manufacturer)}} ` | out-default } if (HasVal $Option 7){ WriteHeader $hostname "Win32_PageFileSetting" 7 $x = BldQry "gwmi Win32_PageFileSetting -computer `$hostname" | iex $x | ft name,InitialSize,MaximumSize -auto | out-default } if (HasVal $Option 99){ WriteHeader $hostname "Local Administrators Group" 99 $computer = [ADSI]("WinNT://" + $hostname + ",computer") $Group = $computer.psbase.children.find("Administrators") $members= $Group.psbase.invoke("Members") | %{$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)} ForEach($user in $members){ $user | out-default } } |
Very usefull script and it works great.
Thanks for sharing.
Hi,
Please can you update the script for getting the hostname from the text file and then export the output in html file.
Regards,
Shashi
Hi Sashi,
We don’t really need to modify the script to process a list of hostnames. You can achieve the same thing by calling the script from within a looping construct. Something like this ought to work:
gc .\MyHostFile.txt |% {“
nResults for: $_n”; ./gethostinfo.ps1 $_}For HTML output you could wrap the output from the above code with some HTML tags.
something like this:
“<html><head></head><body><pre>” | out-file hostfile.html
gc .\MyHostFile.txt |% {“
nResults for: $_n”; ./gethostinfo.ps1 $_} |out-file -append hostfile.html“</pre></body></html>” |out-file -append hostfile.html
One note, you would need to modify the WriteHeader function to write to stdout so that the headers can be captured to your html file. The write-host cmdlet forces output to go only to the console.
Hope This Helps!
Simply fantastic. I wish I could script as well as you. Thanks so much!!!