ThrowingFitz
ThrowingFitz

Reputation: 17

Get IP of network adapters and display in messagebox...gets errors when connection is not active

I am trying to have a pop up box come up when a user clicks on a desktop icon...this will provide them with their username, computer name, connected network IPs and other additional information...the code works properly, however if a network connection is not active, there is an error displayed in a powershell window. I was wondering if anyone could help with this? Basically if there is no connection, don't display anything...if there is a connection display the IP. Also would be nice to not have the powershell window open when running this...I think this can be accomplished with VBS? Thank you in advance.

Add-Type -AssemblyName System.Windows.Forms
$ethAddress = (Get-NetIPConfiguration -InterfaceAlias 'Ethernet' | Where-Object {$_.IPv4DefaultGateway -ne $null -and $_.NetAdapter.status -ne "Disconnected"}).IPv4Address.ipAddress
$wiAddress = (Get-NetIPConfiguration -InterfaceAlias 'Wi-Fi' | Where-Object {$_.IPv4DefaultGateway - ne $null -and $_.NetAdapter.status -ne "Disconnected"}).IPv4Address.ipAddress
$vpnAddress = (Get-NetIPConfiguration -InterfaceAlias 'VPN' | Where-Object {$_.IPv4DefaultGateway -ne $null -and $_.NetAdapter.status -ne "Disconnected"}).IPv4Address.ipAddress
$celAddress = (Get-NetIPConfiguration -InterfaceAlias 'Cellular' | Where-Object {$_.IPv4DefaultGateway -ne $null -and $_.NetAdapter.status -ne "Disconnected"}).IPv4Address.ipAddress
$pcname = [System.Net.Dns]::GetHostName()
$userName = $env:UserName
$serialNum = (gwmi win32_bios).SerialNumber
$model = (gwmi win32_computersystem).model
$manufacturer = (gwmi win32_computersystem).manufacturer
$buildNumber = (gwmi win32_operatingsystem).buildnumber
$releaseID = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ReleaseID).ReleaseID

[System.Windows.Forms.MessageBox]::Show("Please provide the following information for the IT  Technician.
`nUsername:`t$userName 
Computer Name:`t$pcname  
`nWired IP:`t`t$ethAddress
Wireless IP:`t$wiAddress
VPN IP:`t`t$vpnAddress 
Cellular IP:`t$celAddress
`nManufacturer:`t$manufacturer
 Model:`t`t$model
 Service Tag:`t$serialNum
 `nW10 Release ID:`t$releaseID
 W10 Build:`t$buildNumber",'PC Information','OK','Information')

Upvotes: 0

Views: 386

Answers (1)

AdminOfThings
AdminOfThings

Reputation: 25001

You can redirect (>) the error stream (2) to null and then check the variable for contents. Then you can choose to take action based on the variable contents. This will work because by default variables store success stream (1) results.

$ethAddress = (Get-NetIPConfiguration -InterfaceAlias 'Ethernet' |
    Where-Object {$_.IPv4DefaultGateway -ne $null -and $_.NetAdapter.status -ne "Disconnected"}).IPv4Address.ipAddress 2>null
$wiAddress = (Get-NetIPConfiguration -InterfaceAlias 'Wi-Fi' |
    Where-Object {$_.IPv4DefaultGateway - ne $null -and $_.NetAdapter.status -ne "Disconnected"}).IPv4Address.ipAddress 2>null
$vpnAddress = (Get-NetIPConfiguration -InterfaceAlias 'VPN' |
    Where-Object {$_.IPv4DefaultGateway -ne $null -and $_.NetAdapter.status -ne "Disconnected"}).IPv4Address.ipAddress 2>null
$celAddress = (Get-NetIPConfiguration -InterfaceAlias 'Cellular' |
    Where-Object {$_.IPv4DefaultGateway -ne $null -and $_.NetAdapter.status -ne "Disconnected"}).IPv4Address.ipAddress 2>null
if (!($ethAddress -and $wiAddress -and $celAddress -and $vpnAddress)) { exit }

Upvotes: 1

Related Questions