PowerShell 'if' condition

Viewed 6904

Here is a quick little PowerShell blurp that is supposed to get the device's IP address, Service Tag, and MAC address of any active MAC addresses on the device (active meaning there is a connection).

Anyway, if I don't use an if statement, this works perfectly, but if I do, it only executes the first line:

Test-Connection $computername -count 1 | select @{Name="Computername";Expression={$_.Address}},Ipv4Address

If the device is on... and the last line of the device is off.

Write-Host "Device is offline"

Here is my little PowerShell 'script':

$computername = Read-Host 'Enter Computer Name'
$online = Test-Connection -Computername $computername -BufferSize 16 -Count 1 -Quiet
IF ($online -eq "True") {
    Test-Connection $computername -count 1 | select @{Name="Computername";Expression={$_.Address}},Ipv4Address
    Get-WmiObject win32_SystemEnclosure -computername $computername | select serialnumber
    Get-wmiobject -class "Win32_NetworkAdapterConfiguration" -computername $computername |Where{$_.IpEnabled -Match "True"}
} Else {
    Write-Host "Device is offline"
}

Why does this happen? What might I be doing wrong?

3 Answers
Related