YellowOnline
Technical User
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?
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
}
}