This scripts denotes to get the User logon details of the Terminal Services (RDP) and IT admin can retrieve the user logon time, User IDs etc with the Text box to indicate the user who trying to login on the specific RDP server. This scripts writes the user logon details(login Timings, Username) and write to the textfile where i used to store in the D drive of the Terminal services server. Use this powershell script to access from the user logon script.

Report will be generated and stored in the %userprofile% and it will copy to the D drive in the folder "user". You can change the path whereever you want.

 

PowerShell
Edit|Remove
$vUserName = (Get-Item env:\username).Value 
$filepath = (Get-ChildItem env:\userprofile).value 
$name = (Get-Item env:\Computername).Value 
$datestr = (Get-date).ToString('ddMMyyy') 
 
    if (test-Path $filepath\$vUserName.txt)  
    {  
    remove-Item $filepath\$vUserName.txt;     
    } 
 
Add-Type -AssemblyName System.Windows.Forms 
Add-Type -AssemblyName System.Drawing 
 
$form = New-Object System.Windows.Forms.Form  
$form.Text = "Data Entry Form" 
$form.Size = New-Object System.Drawing.Size(300,200)  
$form.StartPosition = "CenterScreen" 
 
$OKButton = New-Object System.Windows.Forms.Button 
$OKButton.Location = New-Object System.Drawing.Point(75,120) 
$OKButton.Size = New-Object System.Drawing.Size(75,23) 
$OKButton.Text = "OK" 
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK 
$form.AcceptButton = $OKButton 
$form.Controls.Add($OKButton) 
 
$CancelButton = New-Object System.Windows.Forms.Button 
$CancelButton.Location = New-Object System.Drawing.Point(150,120) 
$CancelButton.Size = New-Object System.Drawing.Size(75,23) 
$CancelButton.Text = "Cancel" 
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel 
$form.CancelButton = $CancelButton 
 
$label = New-Object System.Windows.Forms.Label 
$label.Location = New-Object System.Drawing.Point(10,20)  
$label.Size = New-Object System.Drawing.Size(280,20)  
$label.Text = "Please enter your Name in the below Textbox:" 
$form.Controls.Add($label)  
 
$textBox = New-Object System.Windows.Forms.TextBox  
$textBox.Location = New-Object System.Drawing.Point(10,40)  
$textBox.Size = New-Object System.Drawing.Size(260,20)  
$form.Controls.Add($textBox)  
 
$form.Topmost = $True 
 
$form.Add_Shown({$textBox.Select()}) 
$result = $form.ShowDialog() 
 
if ($result -eq [System.Windows.Forms.DialogResult]::OK) 
{ 
    $x = $textBox.Text 
    $x 
} 
     
$Report = "$((Get-Item env:\username).Value) $(get-date) Logged-In $x" 
$Report  >> "$filepath\$vUserName.txt" 
$Report  >> "d:\user\report.txt" 
 
if (test-Path d:\user\$vUserName.txt)  
{  
remove-Item d:\user\$vUserName.txt;     
} 
 
Copy-Item -Path $filepath\$vUserName.txt -Destination "d:\user\" -Force 
 
if (test-Path $filepath\$vUserName.txt)  
{  
remove-Item $filepath\$vUserName.txt;     
}