Powershell script

Louai Al Obaidi 0 Reputation points
2024-04-12T13:18:55.0033333+00:00
Hi i want to create a script that runs every time a regular user logs in to one of your clients
your domain writes a report with the name “yyyymmdd_computername.txt” and
content “hh:mm:ss – username” to a central repository. Someone every time
otherwise you log in, it will be added to an existing file or a new one
created if it is the first logged in user for that day. User name
change by the name of the logged in user, computer name by the name of the
computer used to log in, yyyy by the year, mm by the month, dd by the
day, hh:mm:ss by the hour, minute and seconds respectively.
Make sure this script runs on all your clients when logging in to a regular one
End User.
Have this central repository on each administrative device (there may be more than one)
is linked in an efficient manner and becomes accessible from Windows
Scout.
How to do that? i want to a notepad will be created on my file server in this path for example \\FS-2024\Logon\
i want to apply that for via group policy via a GPO
Thanks
Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
6,642 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,552 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,595 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Ahamed Musthafa Careem 461 Reputation points
    2024-04-12T14:40:53.9966667+00:00

    Hi Just try running this command and let me know please.

    # Get the current date and time

    $DateTime = Get-Date

    $Date = $DateTime.ToString("yyyyMMdd")

    $Time = $DateTime.ToString("HH:mm:ss")

    # Get the computer name and username

    $ComputerName = $env:COMPUTERNAME

    $Username = $env:USERNAME

    # Set the log file path

    $LogPath = "\\FS-2024\Logon\"

    $LogFileName = "$Date" + "_" + "$ComputerName" + ".txt"

    $LogFilePath = Join-Path -Path $LogPath -ChildPath $LogFileName

    # Create the log file if it doesn't exist

    if (-not (Test-Path -Path $LogFilePath)) {

    New-Item -Path $LogFilePath -ItemType File | Out-Null

    }

    # Write log entry to file

    "$Time - $Username" | Out-File -FilePath $LogFilePath -Append

    0 comments No comments

  2. Ian Xue 37,706 Reputation points Microsoft Vendor
    2024-04-15T01:21:39.21+00:00

    Hi,

    Please create a GPO on your domain controller and add a logon script under "User Configuration" > "Policies" > "Windows Settings" > "Scripts ( Logon/Logoff)" > "Logon"

    The script could look like this.

    $file = (Get-Date -format "yyyyMMdd").ToString() + "_" + $env:COMPUTERNAME + ".txt"
    $content = (Get-Date -format "hh:mm:ss").ToString() + " - " + $env:USERNAME
    $content | Out-File -Path  \\file\share\$file
    

    Best Regards,

    Ian Xue


    If the Answer is helpful, please click "Accept Answer" and upvote it.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.