Source/Public/Write-3LNetworkConfigurationToFile.ps1
Function Write-3LNetworkConfigurationToFile { <# .SYNOPSIS Find text in a file .NOTES Author : Frits van Drie (3-Link.nl) Versions : 2021.09.24 .EXAMPLE SaveNetworkConfigurationToFile -openFile .EXAMPLE SaveNetworkConfigurationToFile -path 'C:\data\network.txt' #> [CmdletBinding()] param ( [string]$path = "$($env:USERPROFILE)\Documents\$($env:COMPUTERNAME)-$($MyInvocation.MyCommand)-$(Get-Date -Format yyyyMMdd_HHmmss).txt", [string]$header, [switch]$openFile, [switch]$noFile ) [array]$result = @() $result += "=================================================================================================" $result += "Computer : $($env:COMPUTERNAME)" $result += "Date : $(Get-Date -Format 'yy-MM-dd (HH:mm:ss)')" if ($MyInvocation.BoundParameters.ContainsKey('header')) { $result += "Description: $header" } $result += "=================================================================================================`n" # Gather info try { $result += "`n=================================================================================================" $result += "Get-NetAdapter" $result += "=================================================================================================`n" $result += Get-NetAdapter -ErrorAction Stop | ft -AutoSize $result += "`n=================================================================================================" $result += "Get-NetIPAddress" $result += "=================================================================================================`n" $result += Get-NetIPAddress -ErrorAction Stop | ft -AutoSize $result += "`n=================================================================================================" $result += "Get-NetRoute" $result += "=================================================================================================`n" $result += Get-NetRoute -ErrorAction Stop | ft -AutoSize $result += "`n=================================================================================================" $result += "Get-DnsClientCache" $result += "=================================================================================================`n" $result += Get-DnsClientCache -ErrorAction Stop | ft -AutoSize $result += "`n=================================================================================================" $result += "Show-DnsServerCache" $result += "=================================================================================================`n" (Get-DnsClientServerAddress -Family IPv4 | Where serveraddresses ).serveraddresses | foreach { $result += "`n====================" $result += "DNS: $_" $result += "====================`n" try { $result += Show-DnsServerCache -ComputerName $_ -ea Stop } catch { $result += "Cache of $_ could not be read" } } $result += "`n=================================================================================================" $result += "Get-NetTCPConnection" $result += "=================================================================================================`n" $result += Get-NetTCPConnection -ErrorAction Stop | ft -AutoSize $result += "`n=================================================================================================" $result += "Get-Process" $result += "=================================================================================================`n" $result += Get-Process -ErrorAction Stop | ft -AutoSize $result += "`n=================================================================================================" $result += "Get-DnsClientServerAddress" $result += "=================================================================================================`n" $result += Get-DnsClientServerAddress -ErrorAction Stop | ft -AutoSize $result += "`n=================================================================================================" $result += "Certificates" $result += "=================================================================================================`n" $result += GCI 'Cert:\LocalMachine\My' -ErrorAction Stop | ft -AutoSize $result += GCI 'Cert:\CurrentUser\My' -ErrorAction Stop | ft -AutoSize } catch { Write-Error $error[0] break } if ( $noFile ) { $openFile = $true $path = Join-Path $env:TEMP (Split-Path $path -Leaf) } try { $result | Out-File $path -Append -ErrorAction Stop -Width 200 } catch { Write-Error $error[0] break } if ($openFile) { notepad.exe $path } if ($noFile) { Write-Verbose "Network configuration saved to $path" Start-Sleep 2 Remove-Item $path -Force -ea SilentlyContinue } else { Write-Host "Network configuration saved to $path" -f Yellow } } |