none
Adding time stamp in log file RRS feed

  • Question

  • Hi Guys

    is there a simple way to add time stamp to a log file that is being created in a PS script?

    Thanks

    E

    Wednesday, January 31, 2018 2:41 PM

Answers

  • Then you have to add the time to every line you send to the log file. This is easiest don  if you create a function that logs messages.

    The following is unnecessary:

    Write-Output "$server has been inserted to Alteon!" | Out-File -FilePath "$logDir\$logFileName" -Append  

    This is all we need to do in PowerShell:

    "$server has been inserted to Alteon!" | Out-File -FilePath "$logDir\$logFileName" -Append  

    Now just add the time to the beginning.

    "$([datetime]::Now) $server has been inserted to Alteon!" | Out-File -FilePath "$logDir\$logFileName" -Append  


    \_(ツ)_/


    • Edited by jrv Thursday, February 1, 2018 7:03 AM
    • Proposed as answer by I.T Delinquent Thursday, February 1, 2018 9:05 AM
    • Marked as answer by EDolev Thursday, February 1, 2018 1:11 PM
    Thursday, February 1, 2018 7:03 AM

All replies

  • Yes, put the data into a variable and add the variable to the file in the same way that your are adding all the other information to it.

    $dateforlog = Get-Date

    You can also edit the Get-Date part to specify what parts you want to highlight. Check here

    Without knowing how you're writing the file or what format the file is, it's difficult for this forum to give you more detailed help.

    Wednesday, January 31, 2018 5:05 PM
  • Hi

    i will give you a small example:

                                                

    cls
    #customize log file
    $date = (Get-Date -Format d).ToString() | foreach {$_ -replace "/","_"} 
    $time = (Get-Date)

    $scriptDir = "D:\Scripts\ServerBuildToolkitT1\Alteon-In\"
    $logDir = "D:\Scripts\ServerBuildToolkitT1\Logs\Alteon-In\"

    $logFileName = "Alteon-In.log"
    #Delete old log file
    Remove-Item -Path "$logDir\$logFileName" -ErrorAction Ignore

    #Get the user name.
    $username = [Environment]::UserName

    #Read the server list from the GUI. 
    try
    {
        [xml]$serverList = Get-Content -Path "D:\Scripts\ServerBuildToolkitT1\serverList.xml" -ErrorAction stop
    }
    catch 
    {
        Write-Output "Couldn't read the file. Please make sure the file exist under '$scriptdir' `n" | Out-File -FilePath "$logDir\$logFileName"
    }

    # Variable declaretion
    $destination = $serverList.Section.Servers.Destination
    $MonitorPath = "D$\Sites\monitor\"
    $OutName = "nettest_1.html"
    $MonitorName = "nettest.html"

    #Function to instert servers to Alteon. 
    Function Alteon-In ()
    {
        #Validate the list is not empty. If empty throw an exception. else ranme the file for each site on each server.
        if($destination.Length -ne 0)
        {
          #foreach server in the list, read the current monitor file name. Then change it. 
          foreach ($server in $destination.Split(','))
          {

            if ($server -eq "")
            {

            }

            else
            {

            #Read the monitor file name from each site on each server.
            $CurrentMonitorName = Get-ChildItem -Path \\$server\$MonitorPath | Split-Path -Leaf


            #Check if orange site is already in Alteon. If yes, throw an exception. Else, rename the file.
            if($CurrentMonitorName -eq $MonitorName)
            {
                Write-Output "$server is already in Alteon! `n" | Out-File -FilePath "$logDir\$logFileName" -Append  
            }
            else
            {
                #Inserting the server to Alteon by renaming the file.
                Rename-Item \\$server\$MonitorPath\$CurrentMonitorName \\$server\$MonitorPath\$MonitorName -ErrorAction Ignore
                Write-Output "$server has been inserted to Alteon!" | Out-File -FilePath "$logDir\$logFileName" -Append  
            }  


          }
        }
        else
        {
           Write-output "The list is empty. Please update the list of servers under 'destination' section `n" | Out-File -FilePath "$logDir\$logFileName"
        }     
    }

    Alteon-In
    Write-Output "The operation completed successffully `n" | Out-File -FilePath "$logDir\$logFileName" -Append
    $richtext.Text += "The operation completed successffully `n"

    Copy-Item $logDir\$logFileName "$logDir\Alteon-In $date.log"

    i want that every line in the log will have date and time stamp

    many thanks

    E

    Thursday, February 1, 2018 6:57 AM
  • Then you have to add the time to every line you send to the log file. This is easiest don  if you create a function that logs messages.

    The following is unnecessary:

    Write-Output "$server has been inserted to Alteon!" | Out-File -FilePath "$logDir\$logFileName" -Append  

    This is all we need to do in PowerShell:

    "$server has been inserted to Alteon!" | Out-File -FilePath "$logDir\$logFileName" -Append  

    Now just add the time to the beginning.

    "$([datetime]::Now) $server has been inserted to Alteon!" | Out-File -FilePath "$logDir\$logFileName" -Append  


    \_(ツ)_/


    • Edited by jrv Thursday, February 1, 2018 7:03 AM
    • Proposed as answer by I.T Delinquent Thursday, February 1, 2018 9:05 AM
    • Marked as answer by EDolev Thursday, February 1, 2018 1:11 PM
    Thursday, February 1, 2018 7:03 AM
  • first of all thanks

    so do i need to write a function for $([datetime]::Now)?

    thanks

    Thursday, February 1, 2018 7:26 AM
  • No.  Just copy and paste that line at a prompt to see how it works.

    I recommend that you learn PowerShell before you try writing a script.  You cannot guess at how to use any advanced technology.


    \_(ツ)_/

    Thursday, February 1, 2018 7:31 AM
  • first of all thank you for your help

    second i have learned PS scripting but as you guesses i am a beginner in this area.

    i am entering the forums here to educate and get tips from experienced scripters as you.

    again many thanks and have a nice day

    Thursday, February 1, 2018 7:37 AM
  • You haven't learned PowerShell.  You need to study it completely and no just guess at things,  Your code shows that you are copying things that are no good PowerShell.

    Here is a link to a tutorial.  Watch it as many times as it takes for you to KNOW you have learned PowerShell basics.

    Learn PowerShell  

    Believe me you will be amazed at what PowerShell is and can do once you understand the basics.

    You cannot learn PowerShell by asking questions in a forum.  You can become more experienced by reading forums and blogs after you have learned basic PowerShell.

    Also, "tips" will not teach you PowerShell. If you do not know basic PowerShell how can you tell if a "tip" is good or just nonsense.


    \_(ツ)_/

    Thursday, February 1, 2018 8:34 AM
  • ok

    thank you, have a nice day

    Thursday, February 1, 2018 10:24 AM