|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+@echo off
|
|
|
2
|
+setlocal enabledelayedexpansion
|
|
|
3
|
+
|
|
|
4
|
+:: Output filename
|
|
|
5
|
+set "output_file=allowed_pcs.csv"
|
|
|
6
|
+
|
|
|
7
|
+echo [INFO] Extracting session information...
|
|
|
8
|
+
|
|
|
9
|
+:: 1. Get the Computer Name
|
|
|
10
|
+set "pc_name=%COMPUTERNAME%"
|
|
|
11
|
+
|
|
|
12
|
+:: 2. Get the current Username and Session ID
|
|
|
13
|
+:: Using 'query user' for more stable output parsing compared to qwinsta
|
|
|
14
|
+set "found=false"
|
|
|
15
|
+for /f "skip=1 tokens=1,2,3" %%i in ('query user %USERNAME%') do (
|
|
|
16
|
+ :: %%i = Username, %%j = SessionName or ID, %%k = ID or State
|
|
|
17
|
+
|
|
|
18
|
+ :: Check if the 2nd column is a number (Session ID)
|
|
|
19
|
+ echo %%j | findstr /r "^[0-9][0-9]*$" >nul
|
|
|
20
|
+ if not errorlevel 1 (
|
|
|
21
|
+ set "user_name=%%i"
|
|
|
22
|
+ set "session_id=%%j"
|
|
|
23
|
+ ) else (
|
|
|
24
|
+ :: If 2nd column is SessionName (e.g., console), ID is in the 3rd column
|
|
|
25
|
+ set "user_name=%%i"
|
|
|
26
|
+ set "session_id=%%k"
|
|
|
27
|
+ )
|
|
|
28
|
+ :: Remove the ">" prefix from the username if present
|
|
|
29
|
+ set "user_name=!user_name:>=!"
|
|
|
30
|
+ set "found=true"
|
|
|
31
|
+)
|
|
|
32
|
+
|
|
|
33
|
+if "%found%"=="false" (
|
|
|
34
|
+ echo [ERROR] Could not identify active session.
|
|
|
35
|
+ pause
|
|
|
36
|
+ exit /b
|
|
|
37
|
+)
|
|
|
38
|
+
|
|
|
39
|
+:: 3. Prompt user for a description
|
|
|
40
|
+set /p desc="Enter a description for this PC (e.g. Sales-Desktop): "
|
|
|
41
|
+
|
|
|
42
|
+:: 4. Output to CSV in append mode
|
|
|
43
|
+:: Wrapping in parentheses to prevent accidental file redirection issues
|
|
|
44
|
+(echo %pc_name%,%user_name%,%session_id%,%desc%)>> "%output_file%"
|
|
|
45
|
+
|
|
|
46
|
+echo.
|
|
|
47
|
+echo [SUCCESS] Information added to %output_file%
|
|
|
48
|
+echo Data: %pc_name%,%user_name%,%session_id%,%desc%
|
|
|
49
|
+echo.
|
|
|
50
|
+pause
|