You might consider the Log-Entry
framework (also on GitHub) I published awhile ago:
It has basically all the features were you ask for:
- File is automatically truncated if it grows over ~100Kb (default)
- Time stamps
- Inline logging
- Proper type casting (e.g. notice that the
$gateway
has also a $Null
property in my case)
Function Main {
Log -File ".\Network.log"
$server = Log "Server:" "192.168.1.1" ?
$gateway = Log "Gateway:" (Get-wmiObject Win32_networkAdapterConfiguration | ?{$_.IPEnabled}).DefaultIPGateway ?
Log "IP Config:" ((& ipconfig /all) -Join "`r`n")
Log "Ping to $Server" ((& ping $server) -Join "`r`n")
}
The log will look like this:
2017-06-08 Test (version: 01.00.02, PowerShell version: 5.X.X5063.296)
09:13:14.01 C:\Users\User\Network.ps1
15:28:14.67 Server: 192.168.X.X
15:28:14.75 Gateway: @($Null, "192.168.X.X")
15:28:14.78 IP Config: Windows IP Configuration
Host Name . . . . . . . . . . . . : Computer
Primary Dns Suffix . . . . . . . :
Node Type . . . . . . . . . . . . : Hybrid
IP Routing Enabled. . . . . . . . : No
WINS Proxy Enabled. . . . . . . . : No
DNS Suffix Search List. . . . . . : lan
Ethernet adapter Ethernet:
Connection-specific DNS Suffix . : lan
Description . . . . . . . . . . . : Intel(R) 82579LM Gigabit Network Connection
Physical Address. . . . . . . . . : XX-XX-XX-XX-XX-XX
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
Link-local IPv6 Address . . . . . : XXXX::XXXX:XXXX:XXXX:XXXX%8(Preferred)
IPv4 Address. . . . . . . . . . . : 192.168.X.X(Preferred)
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Lease Obtained. . . . . . . . . . : Thursday, June 8, 2017 9:08:58 AM
Lease Expires . . . . . . . . . . : Friday, June 9, 2017 9:08:57 AM
Default Gateway . . . . . . . . . : 192.168.X.X
DHCP Server . . . . . . . . . . . : 192.168.X.X
DHCPv6 IAID . . . . . . . . . . . : 9808.X.X
DHCPv6 Client DUID. . . . . . . . : XX-XX-XX-XX-XX-XX-XX-XX-XX-XX-XX-XX-XX-XX
DNS Servers . . . . . . . . . . . : 192.168.X.X
NetBIOS over Tcpip. . . . . . . . : Enabled
Ethernet adapter VirtualBox Host-Only Network:
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : VirtualBox Host-Only Ethernet Adapter
Physical Address. . . . . . . . . : XX-XX-XX-XX-XX-XX
DHCP Enabled. . . . . . . . . . . : No
Autoconfiguration Enabled . . . . : Yes
Link-local IPv6 Address . . . . . : XXXX::XXXX:XXXX:XXXX:XXXX%5(Preferred)
IPv4 Address. . . . . . . . . . . : 192.168.X.X(Preferred)
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . :
DHCPv6 IAID . . . . . . . . . . . : 420085799
DHCPv6 Client DUID. . . . . . . . : XX-XX-XX-XX-XX-XX-XX-XX-XX-XX-XX-XX-XX-XX
DNS Servers . . . . . . . . . . . : XXXX:0:0:XXXX:.X.X
XXXX:0:0:XXXX::2%1
XXXX:0:0:XXXX::3%1
NetBIOS over Tcpip. . . . . . . . : Enabled
Wireless LAN adapter Wi-Fi:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . : lan
Description . . . . . . . . . . . : Intel(R) Centrino(R) AdvancXX-N 6235
Physical Address. . . . . . . . . : XX-XX-XX-XX-XX-XX
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
Wireless LAN adapter Local Area Connection* 2:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Microsoft Wi-Fi Direct Virtual Adapter
Physical Address. . . . . . . . . : XX-XX-XX-XX-XX-XX
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
Ethernet adapter Bluetooth Network Connection:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Bluetooth Device (Personal Area Network)
Physical Address. . . . . . . . . : XX-XX-XX-XX-XX-XX
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
15:28:17.82 Ping to 192.168.X.X Pinging 192.168.X.X with 32 bytes of data:
Reply from 192.168.X.X: bytes=32 time<1ms TTL=64
Reply from 192.168.X.X: bytes=32 time=1ms TTL=64
Reply from 192.168.X.X: bytes=32 time<1ms TTL=64
Reply from 192.168.X.X: bytes=32 time<1ms TTL=64
Ping statistics for 192.168.X.X:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 1ms, Average = 0ms
15:28:17.84 End