3

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?

9
  • 2
    NB: Use $true instead of "True"; the first is a boolean value; the second a string. Commented Aug 11, 2017 at 14:44
  • If I've understood your question, you're saying that when the computer is online, you're not seeing the serial number or network adapter information; is that correct? Commented Aug 11, 2017 at 14:45
  • 2
    If so, maybe there's a permissions issue running those WMI queries against the remote machine? What happens if you run those statements on their own? Also, what's your $ErrorActionPreference set to (i.e. maybe it's set to SilentlyContinue, such that exceptions won't be displayed)? Commented Aug 11, 2017 at 14:47
  • @JasonSnell no i dont think so... @{Name="Computername"... just changes the word 'Address' to 'Computername' as described in the rest of the line Commented Aug 11, 2017 at 14:52
  • 1
    The If statement makes sense; I'm asking how it works without that statement to understand the problem; i.e. does the if statement cause the problem, or is it unrelated (which seems more likely) / is it just that the code calling get-wmiobject isn't producing any output; most likely because of some error that we're not seeing. Commented Aug 11, 2017 at 15:02

3 Answers 3

2

Boolean variables in PowerShell are $true and $false. If should be:

if ($online -eq $true) {

Or

if($online)
Sign up to request clarification or add additional context in comments.

4 Comments

In Powershell $true -eq "true" is also valid. For whatever reason, a string true or false can be compared against a bool.
"True" is a truthy value (stackoverflow.com/a/38831795/361842), but "false" is also truthy (e.g. run: $true -eq "false" ), so it's best to always use the defined values / be careful when running such checks.
True. It's weird how "false" is also truthy.
@JasonSnell; it's because the word "false" only has meaning to us as people; from a PowerShell comparison perspective, it casts this a a boolean to perform a comparison, and a non-zero-length string casts to true by definition. "false" -eq $true gives false because it converts $true to a string, "true", and so "false" -eq "true" evaluates to false. To avoid such confusion, always use the correct types when making comparisons.
2

Try this:

$computername = Read-Host 'Enter Computer Name'
$online = Test-Connection -Computername $computername -BufferSize 16 -Count 1 -Quiet
IF ($online -eq $true) {

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

    try {
        [PSObject[]]$systemEnclosures = Get-WmiObject win32_SystemEnclosure -computername $computername -ErrorAction Stop
        Write-Host "Found $($systemEnclosures.Count) System Enclosures"
        $systemEnclosures | select serialnumber
        [PSObject[]]$NetworkAdapterConfiguration = Get-wmiobject -class "Win32_NetworkAdapterConfiguration" -computername $computername -ErrorAction Stop
        Write-Host "Found $($NetworkAdapterConfiguration.Count) Network Adapter Configurations"
        $NetworkAdapterConfiguration = $NetworkAdapterConfiguration | Where{$_.IpEnabled}
        Write-Host "Found $($NetworkAdapterConfiguration.Count) IP Enabled Network Adapter Configurations"
        $NetworkAdapterConfiguration
    } catch {
        Write-Host "An Error Occurred"
        Write-Host $_.ToString() #show exception info
    }
} Else {
    Write-Host "Device is offline"
}

NB: I'm not suggesting you keep this code in your final script; just use this to understand what's happening behind the scenes.

Per comments; use $true instead of "true", as though both are truthy, using the wrong type will lead to a false understanding of the language / some really odd bugs down the line where you find that lines like if($true -eq "false") {write-output "Well, this is unusual"} will cause some odd behaviour.

Also you may wish to look into replacing Write-Host with Write-Output for any logical return values, or Write-Verbose/Write-Debug for any informative/investigation outputs; then call the code with the appropriate switches / preferences... but that's unrelated to your issue. More on that is in Write-Host Considered Harmful.


Update

Per comments, the issue you're seeing is a bug gotcha: https://github.com/PowerShell/PowerShell/issues/4552

If the data coming out of this code is just to be displayed in the console, you can avoid this issue by explicitly calling the Format-Table command:

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

If you need the output to go to the pipeline for consumption elsewhere, all's good as it is (i.e. without the format-table piece); the objects are being correctly written to the pipeline; the issue is simply that when it comes to displaying all of the results together, the first object causes PowerShell to create columns ComputerName and Ipv4Address, and PowerShell subsequently attempts to display those properties of the following objects, despite those not having such properties. That said, this could be improved by putting the different object types into different properties of a custom object for easy reference. For example,

$computername = Read-Host 'Enter Computer Name'
If (Test-Connection -Computername $computername -BufferSize 16 -Count 1 -Quiet) {
    (new-object -TypeName PSObject -Property @{
        ConnectionTest = Test-Connection $computername -count 1 | select @{Name="Computername";Expression={$_.Address}}, 'Ipv4Address'
        SystemEnclosures = Get-WmiObject win32_SystemEnclosure -computername $computername | select serialnumber
        NetworkAdapterConfigs = Get-wmiobject -class "Win32_NetworkAdapterConfiguration" -computername $computername | Where{$_.IpEnabled}
    })
} Else {
    Write-Host "Device is offline"
}

10 Comments

Did this work for you? you got the Sevice Tag and Mac addresses?
@LukeZeimet; Originally I'd not tried it. I've now added type definitions to ensure that when 1 object is returned, the objects are still arrays, so still have a Count property: e.g. [PSObject[]]$systemEnclosures.
John: My apologies for leading us down the wrong path initially: the behavior you're describing is actually as designed, I think, as I've since explained here. The asynchronous behavior of implicit Format-Table explained in @PetSerAl's answer can also be a problem, but it is unrelated to this issue.
No worries, thanks @mklement0. Will continue the discussion on GitHub as though I agree with your comments, I think the behaviour does have some issues (e.g. it's a gotcha for the unwary).
@mklement0 good point; amended (left bug with a strikethrough to keep a bit of historical context)
|
1

Test-Connection -Quiet returns a Boolean, not a string. Try if ($online) or if ($online -eq $true) if you want to be explicit.

https://learn.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Management/Test-Connection?view=powershell-5.1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.