0

I log network status (nslookup, ping, tracert) into a log file. The log file size grows and is quite difficult to work with after a while.

I am looking for a way to have a new log file created for every day. I am sure it's easy but I did not find a way.

When I set log file name to $log_file = ".\network_" + (Get-Date -f yyyy-MM-dd) + ".log" hoping it will create a new file when date changes, it does not work. Instead I get Non-authoritative answer:.

Sorry for newbie question but I really did not find any answer. Thx!

edit:

it's really simple script (shortened example below):

$log_file = ".\network_" + (Get-Date -f yyyy-MM-dd_HH-mm) + ".log"
$server = "server.com"
$gateway = (Get-wmiObject Win32_networkAdapterConfiguration | ?{$_.IPEnabled}).DefaultIPGateway
& ipconfig /all >> $log_file

while($true) {
$timestamp = "rnrn[" + (Get-Date -f yyyy-MM-dd) + " " +(Get-Date -f HH:mm:ss) + "]"
$timestamp >> $log_file
"rnrn" >> $log_file
"ping to $server)" >> $log_file
& ping $server >> $log_file
}
himi
  • 71
  • 2
  • 13
  • 2
    We need more context; it looks like you're reporting an error from NSLookup. Please post a [Minimal, Complete, and Verifiable Example](https://stackoverflow.com/help/mcve) of the code that is generating the error so that others can provide useful assistance. – Jeff Zeitlin Jun 08 '17 at 12:48
  • it's really simple script (shortened example below): $log_file = ".\tracert_" + (Get-Date -f yyyy-MM-dd_HH-mm) + ".log" $server = "server.com" $gateway = (Get-wmiObject Win32_networkAdapterConfiguration | ?{$_.IPEnabled}).DefaultIPGateway & ipconfig /all >> $log_file while($true) { $timestamp = "`r`n`r`n[" + (Get-Date -f yyyy-MM-dd) + " " +(Get-Date -f HH:mm:ss) + "]" "`r`nping to '$server)'" >> $log_file & ping $server >> $log_file } – himi Jun 08 '17 at 12:52
  • Please don't post code in the comments, rather edit your question to contain it. – Nick Jun 08 '17 at 12:52
  • realized that, sorry. post updated. – himi Jun 08 '17 at 13:14

3 Answers3

3

Here's what I usually do.

$Date = get-date -format yyyy-MM-dd
$log_file = "\\Share\folder\folder\FileName-$date.log"
Nick
  • 1,178
  • 3
  • 24
  • 36
  • yep, have that too, see above `$log_file = ".\network_" + (Get-Date -f yyyy-MM-dd_HH-mm) + ".log"`. what was suggested below is to use scheduler .. can i do it without scheduler? – himi Jun 09 '17 at 05:15
  • Yes, what I was saying is that you can't put `get-date` in the file name. If you define `$date` and then use it in the file path, it will work. – Nick Jun 09 '17 at 18:17
1

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
iRon
  • 20,463
  • 10
  • 53
  • 79
  • Many thanks @iRon, I will surely have a look .. but for now (back to original question), is it possible to have a new file created automatically every day or hour? – himi Jun 08 '17 at 18:49
1

I would suggest including the date/time (yyyy-MM-dd_HH-mm-ss) in the log file name (to prevent duplicate file names) and scheduling your script to run daily / hourly or whenever. Just tell the task scheduler service to end the task if it runs for X number of hours, where X is right before it is scheduled to start again. This should give you the intended result of a new file on the schedule you decide is best. This also ensures your script continues running even if the computer reboots.

If the size of the log file is your main issue, then I would suggest starting a new file based on the file size and not the date. This way you can choose exactly what size files you want to work with.

To do this, just change while($true) to while((Get-Item $log_file).Length -lt "100000") where the length is the size in bytes that you want the script to stop at.

To make your script create a new log file when the while statement is triggered, just wrap it in a function and then call the function using another while $true statement.

Here is the change:

function NetworkLogging {
    $log_file = "$PSScriptRoot\network_" + (Get-Date -f yyyy-MM-dd_HH-mm-ss) + ".log"
    $server = "server.com"
    $gateway = (Get-wmiObject Win32_networkAdapterConfiguration | ?{$_.IPEnabled}).DefaultIPGateway
    & ipconfig /all >> $log_file

    while((Get-Item $log_file).Length -lt "100000") {
    $timestamp = "rnrn[" + (Get-Date -f yyyy-MM-dd) + " " +(Get-Date -f HH:mm:ss) + "]"
    $timestamp >> $log_file
    "rnrn" >> $log_file
    "ping to $server)" >> $log_file
    & ping $server >> $log_file
    }
}

while ($true){NetworkLogging}
twconnell
  • 111
  • 5
  • This does not provide an answer to the question. Once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment](http://stackoverflow.com/help/privileges/comment) on any post. Also check this [what can I do instead](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). – thewaywewere Jun 09 '17 at 01:10
  • thx. can i do it without scheduler, just from the script itself? is that even possible? – himi Jun 09 '17 at 05:15
  • Yes it is possible, but the script will stop running if the system running the script reboots. A task will ensure the script is always running. – twconnell Jun 09 '17 at 07:26
  • that is no problem for now .. so how do i do that? – himi Jun 09 '17 at 08:39