Link to home
Start Free TrialLog in
Avatar of John Myers
John Myers

asked on

Merge WMI scripts into one

I have three scripts that I run in PowerShell to get some information I need from remote workstations.   I would like this to be accomplished with just one script instead of three.   What I need to get from the remote stations is:

Hostname
IP Address
MAC Address
Operating System
Determine if the workstation is a 32-bit or 64-bit
Serial Number or Service Tag Number

I need to be able run these scripts using a list of either Hostnames or IP Addresses in a text file or csv file.  The reults also have to be exported to a CSV file.

Here are the scripts I use.   Not pretty, but they work.

Get-Content C:\Users\username\Desktop\IPs.txt |
  Where-Object { Test-Connection $_ -Count 2 -Quiet } |
  ForEach-Object {
    $IPAddress = $_
    Get-WmiObject Win32_NetworkAdapterConfiguration -Filter 'IPEnabled=TRUE' -Computer $IPAddress |
    Where-Object { $_.IPAddress -contains $IPAddress } |
    Select-Object @{n='IPAddress';e={ $IPAddress }}, MACAddress, DNSHostName
  } | Export-Csv C:\Users\username\Desktop\MACAddressReport.csv -NoTypeInformation

Open in new window


$computerlist = Get-Content C:\Users\username\Desktop\IPs.txt
foreach ($computer in $computerlist) {
    if((Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))
    {   
        Get-WMIObject Win32_OperatingSystem -ComputerName $computer |
        select-object CSName, Caption | export-csv -Path C:\Users\username\Desktop\MACAddressReport-2.csv -NoTypeInformation -Append
    }
}

Open in new window


get-content C:\Users\username\Desktop\HostNames.txt |
   select-object `
     @{n = 'IP'; e = { [Net.Dns]::GetHostEntry($_).AddressList } },
     @{n= 'Name'; e = { $_ } } |
   Export-Csv C:\Users\username\Desktop\IPAddressReport.csv -NoTypeInformation

Open in new window


Any help would be much appreciated.
Avatar of oBdA
oBdA

Something like this?
$IPList = 'C:\Temp\IPs.txt'
$Csv = 'C:\Temp\ComputerReport.csv'

$ErrorActionPreference = 'Stop'
Get-Content -Path $IPList | ForEach-Object {
	Try {
		$IPAddress = $_
		Write-Host "Processing $($IPAddress) ..." -ForegroundColor White -NoNewline
		$Return = $IPAddress | Select-Object -Property 'HostName', @{n='IPAddress'; e={$_}}, 'MACAddress', 'OS', 'Architecture', 'Serial', 'Error'
		Get-WmiObject -Query "Select Caption, CSName, OSArchitecture From Win32_OperatingSystem" -ComputerName $IPAddress | ForEach-Object {
			$Return.OS = $_.Caption
			$Return.HostName = $_.CSName
			$Return.Architecture = $_.OSArchitecture
		}
		Get-WmiObject -Query "Select IPAddress, MACAddress From Win32_NetworkAdapterConfiguration" -ComputerName $IPAddress | Where-Object {$_.IPAddress -contains $IPAddress} | ForEach-Object {
			$Return.MACAddress = $_.MACAddress
		}
		Get-WmiObject -Query "Select SerialNumber From Win32_BIOS" -ComputerName $IPAddress | ForEach-Object {
			$Return.Serial = $_.SerialNumber
		}
		Write-Host " OK: $($Return.HostName)" -ForegroundColor Green
	} Catch {
		$Return.Error = $_.Exception.Message
		Write-Host " ERROR: $($_.Exception.Message)" -ForegroundColor Red
	}
	$Return
} | Export-Csv -NoTypeInformation -Path $Csv

Open in new window

Avatar of John Myers

ASKER

Sorry I have been out of the country for a while and not able to work on this.

This script worked great, but was wondering how I could add in the TPM version for each machine.   I have messed around with it a bit but cannot get it working.

Any ideas?

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank.  I owe you.
Solution provided.