Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Filter IPAddress and write to a file

Status
Not open for further replies.

YellowOnline

Technical User
Mar 31, 2004
144
BE
Hello,

I'm rather new to PowerShell and made this otherwise perfectly working script. I have to problems though:
1) When querying the IPAddress and the MAC-address I get q respons for every device that can be considered a network device. I can exclude a lot by using "-filter IPEnabled=TRUE", but in that case I still get too much hits. I only want the active connection, which can be defined as any connection with an IP other than 0.0.0.0. Unfortunatel, I don't find the right syntax. I tried 101 variation on "-filter IPAddress>null" and it just doesn't seem to work (the script just exits). I wonder if someone can show me the right syntax...
2) I would like to write the exact screen output to a text file or a CSV, but whatever I try doesn't work. I tried piping the script, piping the write-host lines and piping the foreach statements -> I get always get an empty output file (or no file at all). What can I try?

Code:
############################################################################
# Error handling. ErrorAction points to the trap.                          #
############################################################################

$ErrorActionPreference = “SilentlyContinue”

trap 	{
	Continue
	}



############################################################################
# IP Range. Syntax: w.x.y.(z->upperlimit)                                  #
############################################################################

$w = 10
$x = 224
$y = 253
$z = 0
$upperlimit = 254



############################################################################
# Check online hosts and write their addresses to a file called "NETWORK". #
############################################################################

$location = "NETWORK"

#WHILE ($Z -lt $upperlimit)
#	{ 
#	$address = "$w.$x.$y.$z"
#	$z++
#	$address
#	PING -n 1 -w 50 $address > $null
#	IF ($Lastexitcode -eq 0)
#	{$address >> $location}
#	}



############################################################################
# Fill the array "Computers" with the addresses from the "NETWORK"-file    #
############################################################################

$arrComputers = get-Content -Path "NETWORK"




############################################################################
# Fetch the objectitems from the different classes on the hosts.           #
############################################################################

FOREACH ($strComputer in $arrComputers)
	{

	$colItems = Get-WmiObject -class "Win32_ComputerSystem" -namespace "root\CIMV2" -computername $strComputer -EA Stop

	FOREACH ($objItem in $colItems)
   		{
		write-host | Out-File test.txt
		Write-host "Computer Name: " $objItem.Name
		Write-host "Domain       : " $objItem.Domain
		}

	$colItems = Get-WmiObject -class "Win32_NetworkAdapterConfiguration" -namespace "root\CIMV2" -computername $strComputer -EA Stop
	FOREACH ($objItem in $colItems)
  		{
		Write-host "IP address   : " $objItem.IPAddress 
		Write-host "MAC address  : " $objItem.MACAddress 
   		}
	
	$colItems = Get-WmiObject -class "Win32_BIOS" -namespace "root\CIMV2" -computername $strComputer -EA Stop
	FOREACH ($objItem in $colItems)
  		{
		Write-host "Manufacturer : " $objItem.Manufacturer 
		write-host "BIOS Version : " $objItem.SMBIOSBIOSVersion 
		write-host "Serial Number: " $objItem.SerialNumber 
   		}

   	$colItems = Get-WmiObject -class "Win32_ComputerSystem" -namespace "root\CIMV2" -computername $strComputer -EA Stop
	FOREACH ($objItem in $colItems)
   		{
		write-host "Model Name   : " $objItem.Model 
		$CalcMem = [math]::round($objItem.TotalPhysicalMemory/(1024*1024*1024),2)
		write-host "Total RAM    : " $CalcMem GB 
		write-host 
		}

	}
 
Take a look at this FAQ
You can remotely query the registry of each PC to get its DHCP address.

Or you might want to do a PING -A to return the name of the PC for a given IP address.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top