0

I am currently working on a powershell application using windows forms inside powershell. Building the forms is simple enough and I am familiar, however I have an issue that some commands that work in powershell seem to not work when putting it into my form. For example I am attempting to list the Network IP of a computer as the text of a label. The code is:

$IPAddress = Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object {$_.Ipaddress.length -gt 1} 
$netIP = New-Object System.Windows.Forms.Label
$netIP.Text = "Network IP: $IPAddress.ipaddress[0]"
$netIP.Location  = New-Object System.Drawing.Point(0,45)
$netIP.AutoSize = $true
$mainForm.Controls.Add($netIP)

Namely these work perfectly outside the form:

$IPAddress = Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object {$_.Ipaddress.length -gt 1}
    Write-Host "Network IP:" $IPAddress.ipaddress[0]

The command itself works perfectly in Powershell but when I put it in the form as the label text it gives me the output of:

\\computername\root\cimv2:Win32_NetworkAdapterConfiguration.Index=11.ipaddress(0)

Any thoughts on this would be greatly appreciated.

Thanks for any assistance

-Tairros

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • 1
    for your first issue, references to a member/property of an object need to be wrapped in a *subexpression* operator `$(...)` when in a string. So, it should be: `$netIP.Text = "Network IP: $($IPAddress.ipaddress[0])"`. – Abraham Zinala Apr 13 '22 at 13:50
  • 1
    It's always the basics that come bite you in the butt. Thank you very much! That fixed the first issue perfectly. – Tairros Apr 13 '22 at 14:05
  • @Tairros, in the interest of having questions focused on just one problem, please create a _new_ question post for your second question. – mklement0 Apr 13 '22 at 14:50
  • 1
    @mklement0 Will do then. – Tairros Apr 13 '22 at 17:12

0 Answers0