Replace or Remove Strings

Hi Guys,

See my following code then results which I’m trying to remove the @{}= symbols so only has the numbers. See Below

@{255.255.255.0=}

function get-NetInfo{
$NetAdapters = Get-Ciminstance -ClassName Win32_NetworkAdapterConfiguration | Where-Object IPEnabled -match ‘True’

if($netadapters -eq $null) {
Write-Output “No Active Network Cards Found”
} else {

$NetAdaptersReport = @()
Foreach ($netAdapter in $netAdapters){

$Name = $netAdapter.ServiceName
$Description = $netAdapter.Description
$MacAddress = $netAdapter.MACAddress
$IPAddress = $netadapter.IPAddress | Where-Object { $_ -like ‘...’ }
$IPAddress = $IPaddress -join ", "
$SubnetAddress = $netAdapter.IPSubnet | Select-Object $SubnetAddress -First 1
$GatewayAddress = if ($netAdapter.DefaultIPGateway -eq $null){“No Default Gateway Set”}
else {$netAdapter.DefaultIPGateway | Select-Object $GatewayAddress -ExpandProperty $GatewayAddress}
$DNSSuffix = if ($netAdapter.DNSDomain -eq $null){“No DNS Suffix Found”}
else {$netAdapter.DNSDomain}

$DNSServers = if ($netAdapter.DNSDomain -eq $null){“No DNS Server Found”}
else {($netAdapter.DNSServerSearchOrder -join "; ")}
$DHCPEnabled = $netAdapter.DHCPEnabled

$DHCPServers = if ($netAdapter.DHCPServer -eq $null){“DHCP Disabled”}
else {$netAdapter.DHCPServer}

$NetAdapterProp = New-Object -Type PSObject
$NetAdapterProp | Add-Member -MemberType NoteProperty -Name Name -Value $Name
$NetAdapterProp | Add-Member -MemberType NoteProperty -Name Description -Value $Description
$NetAdapterProp | Add-Member -MemberType NoteProperty -Name ‘MAC Address’ -Value $MACAddress
$NetAdapterProp | Add-Member -MemberType NoteProperty -Name ‘IP Address’ -Value $IPAddress
$NetAdapterProp | Add-Member -MemberType NoteProperty -Name ‘Subnet’ -Value $SubnetAddress
$NetAdapterProp | Add-Member -MemberType NoteProperty -Name ‘Default Gateway’ -Value $GatewayAddress
$NetAdapterProp | Add-Member -MemberType NoteProperty -Name ‘DNS Search Suffix’ -Value $DNSSuffix
$NetAdapterProp | Add-Member -MemberType NoteProperty -Name ‘DNS Server(s)’ -Value $DNSServers
$NetAdapterProp | Add-Member -MemberType NoteProperty -Name ‘DHCP’ -Value $DHCPEnabled
$NetAdapterProp | Add-Member -MemberType NoteProperty -Name ‘DHCP Server’ -Value $DHCPServers
$NetAdaptersReport += $NetAdapterProp

}
$NetAdaptersReport
}}

This worked for me.

function get-NetInfo{
$NetAdapters = Get-Ciminstance -ClassName Win32_NetworkAdapterConfiguration | Where-Object IPEnabled -match ‘True’

if($netadapters -eq $null) {
Write-Output “No Active Network Cards Found”
} else {

$NetAdaptersReport = @()
Foreach ($netAdapter in $netAdapters){

$Name = $netAdapter.ServiceName
$Description = $netAdapter.Description
$MacAddress = $netAdapter.MACAddress
$IPAddress = $netadapter.IPAddress | Where-Object { $_ -like ‘*.*.*.*’ }
$IPAddress = $IPaddress -join “, ”
$SubnetAddress = (($netAdapter.IPSubnet | Select-Object $SubnetAddress -First 1) -replace '@{','') -replace '=}',''
$GatewayAddress = if ($netAdapter.DefaultIPGateway -eq $null){“No Default Gateway Set”}
else {$netAdapter.DefaultIPGateway | Select-Object $GatewayAddress -ExpandProperty $GatewayAddress}
$DNSSuffix = if ($netAdapter.DNSDomain -eq $null){“No DNS Suffix Found”}
else {$netAdapter.DNSDomain}

$DNSServers = if ($netAdapter.DNSDomain -eq $null){“No DNS Server Found”}
else {($netAdapter.DNSServerSearchOrder -join “; “)}
$DHCPEnabled = $netAdapter.DHCPEnabled

$DHCPServers = if ($netAdapter.DHCPServer -eq $null){“DHCP Disabled”}
else {$netAdapter.DHCPServer}

$NetAdapterProp = New-Object -Type PSObject
$NetAdapterProp | Add-Member -MemberType NoteProperty -Name Name -Value $Name
$NetAdapterProp | Add-Member -MemberType NoteProperty -Name Description -Value $Description
$NetAdapterProp | Add-Member -MemberType NoteProperty -Name ‘MAC Address’ -Value $MACAddress
$NetAdapterProp | Add-Member -MemberType NoteProperty -Name ‘IP Address’ -Value $IPAddress
$NetAdapterProp | Add-Member -MemberType NoteProperty -Name ‘Subnet’ -Value $SubnetAddress
$NetAdapterProp | Add-Member -MemberType NoteProperty -Name ‘Default Gateway’ -Value $GatewayAddress
$NetAdapterProp | Add-Member -MemberType NoteProperty -Name ‘DNS Search Suffix’ -Value $DNSSuffix
$NetAdapterProp | Add-Member -MemberType NoteProperty -Name ‘DNS Server(s)’ -Value $DNSServers
$NetAdapterProp | Add-Member -MemberType NoteProperty -Name ‘DHCP’ -Value $DHCPEnabled
$NetAdapterProp | Add-Member -MemberType NoteProperty -Name ‘DHCP Server’ -Value $DHCPServers
$NetAdaptersReport += $NetAdapterProp

}
$NetAdaptersReport
}}

 

This would be a little bit cleaner approach … worked just fine for me … try it.

function Get-NetInfo {
    $NetAdapters = Get-Ciminstance -ClassName Win32_NetworkAdapterConfiguration | Where-Object -Property IPEnabled -EQ -Value $true

    if ($netadapters) {
        Foreach ($netAdapter in $netAdapters) {
            [PSCustomObject]@{
                Name               = $netAdapter.ServiceName
                Description        = $netAdapter.Description
                MACAddress         = $netAdapter.MACAddress
                IPv4Address        = $netadapter.IPAddress | Where-Object { $_ -like '*.*.*.*' }
                IPAdresses         = $netadapter.IPAddress -join ', '
                SubnetMask         = $($netAdapter.IPSubnet)[0]
                DefaultGatewayIPv4 = if ($netAdapter.DefaultIPGateway) { $netAdapter.DefaultIPGateway | Where-Object { $_ -like '*.*.*.*' } }else { 'No Default Gateway Set' }
                DNSSearchSuffix    = if ($netAdapter.DNSDomain) { $netAdapter.DNSDomain }else { 'No DNS Suffix Found' }
                DNSServers         = if ($netAdapter.DNSDomain) { $netAdapter.DNSServerSearchOrder -join '; ' }else { 'No DNS Server Found' }
                DHCPEnabled        = $netAdapter.DHCPEnabled
                DHCP               = if ($netAdapter.DHCPServer) { $netAdapter.DHCPServer }else { 'DHCP Disabled' }
            }

        }
    }
    else {
        'No Active Network Cards Found'
    }
}

BTW: it looks better when you toggle the line wrap for the code. :wink:

 

Thanks for help on cleaner code…Still learning the ropes :wink:

Only issue when I come across a network interface with two IP Addresses it will report back as System.Object[]

Function will report IPv4Address as {FirstIP, SecondIP} but I need to remove brackets as I’m converting the results to HTML as per code below.

Need a way for both IPs to be displayed in a list or table but in the same column (next to each other)

#Capture IP Information on Active Cards
if ((get-NetInfo) -match ‘No Active Network Cards Found’) {
$NetAdapterInfoHTML = $NetAdapterInfoHTML | ConvertTo-HTML -Fragment -PreContent “

No IP Information Gathered


} else {
$NetAdapterInfoHTML = Get-NetInfo | ConvertTo-Html -Fragment -As List -PreContent “

Network IP Information

” | ForEach {$_ -replace “”,"<table class=“list”>"}
}
if ((get-NetInfo).Count -match 2) {
$NetAdapterInfoHTML = Get-NetInfo | ConvertTo-Html -Fragment -As Table -PreContent “

Network IP Information

” | ForEach {$_ -replace “
”,"<table class=“list”>"}
}

Have an issue still when IPV4 Address has multiple IP addresses I need this to be included but as I’m converting into HTML later I need to remove the {} symbols and see if can fit both in one table or list field.

At the moment two IPs will see as {FirstIP, SecondIP}

See Below (Sorry can’t get code to display properly?)

if ((get-NetInfo) -match ‘No Active Network Cards Found’) {
$NetAdapterInfoHTML = $NetAdapterInfoHTML | ConvertTo-HTML -Fragment -PreContent “

No IP Information Gathered


} else {
$NetAdapterInfoHTML = Get-NetInfo | ConvertTo-Html -Fragment -As List -PreContent “

Network IP Information

” | ForEach {$_ -replace “”,"<table class=“list”>"}
}
if ((get-NetInfo).Count -match 2) {
$NetAdapterInfoHTML = Get-NetInfo | ConvertTo-Html -Fragment -As Table -PreContent “

Network IP Information

” | ForEach {$_ -replace “
”,"<table class=“list”>"}
}

Issue I get is displayed as System.Object[] in html list fragment.

Worked out the first reply worked…using most of my code from before but using the replace string.

[quote quote=290494]Have an issue still when IPV4 Address has multiple IP addresses I need this to be included but as I’m converting into HTML later I need to remove the {} symbols and see if can fit both in one table or list field.

At the moment two IPs will see as {FirstIP, SecondIP}[/quote]

If you run a query in Powershell and the output is an array, a collection, more than one Powershells format system will show it in the console with curly braces … like this: {Element1,Element2,Element3,…}. You can use a Select-Object -ExpandProperty <PropertyName> to extract the elements of this array. Or you use the “dot notation” like ReturnObject.Property</strong> or <strong>_.Property in a pipeline.

Here you can read how that works: Guide to Posting Code. (I’d recommend the <> icon in the “Visual” view :wink: )

.