Solved

batch file to check my login status MSSQL machines

Posted on 2013-11-05
6
388 Views
Last Modified: 2013-11-06
Intend to come up with a batch script that will verify which servers I am currently logged into.
Seen all the powershell scripts but would like help getting this to work.

Script name is login.bat  username to verify

sqlcmd -L>servers.log


for  /f  %%? in  (servers.log))  do
 ('qwinsta /server:%i | find %1 ')
  SET EXITRC=%ERRORLEVEL%GOTO
  if EXITRC==0
 ( 'echo %pathName% is logged into %? ') >output.log

Thanks in advance
MNT
0
Comment
Question by:mngong_rc
  • 3
  • 3
6 Comments
 
LVL 82

Accepted Solution

by:
oBdA earned 500 total points
Comment Utility
Try this (currently queries servers with more than one SQL instance multiple times; this could be changed if it is an issue for you):
@echo off
setlocal
set LogFile=output.log
if "%~1"=="" (
	echo Syntax:
	echo %~nx0 ^<User name^>
	pause & goto :eof
)
set User=%~1
if exist "%LogFile%" del "%LogFile%"
for /f "skip=2 tokens=1 delims=\ " %%a in ('sqlcmd.exe -L') do (
	if /i not "%%a"=="(local)" (
		qwinsta.exe /server:%%a | findstr.exe /i "\<%User%\>" >NUL
		if errorlevel 1 (
			echo %%a: %User% not logged on.
		) else (
			echo %%a: %User% logged on.
			>>"%LogFile%" echo %%a;%User%
		)
	)
)

Open in new window

0
 

Author Comment

by:mngong_rc
Comment Utility
Thank you, that came as close as it ever got.
This scans all the hundreds of servers in the domain and returns a value for each server.
************
What do I need to do to  show only servers with a  LOGIN. Not interested in servers
without a login and those the user is not allowed to login to.
*************

Thanks again
MNT
0
 
LVL 82

Expert Comment

by:oBdA
Comment Utility
The log file should only show machines where the user is logged on; only the console output shows both.
If you want to suppress it from the console output as well, just put a REM in front of line 15.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:mngong_rc
Comment Utility
I've requested that this question be closed as follows:

Accepted answer: 0 points for mngong_rc's comment #a39627333

for the following reason:

Gave me what I wanted.
0
 
LVL 82

Expert Comment

by:oBdA
Comment Utility
You should accept the answer that helped you, not your own comment.
0
 

Author Comment

by:mngong_rc
Comment Utility
Not a problem
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

by Nathan Brom/Bromy2004 Introduction There are numerous websites out there for any different type of program you can imagine.  Of those, you'll need to decide which ones are legitimate and aren't trying to steal your money or infect your comput…
Many admins will agree: WSUS is is a nice invention but using it on the client side when updating a newly installed computer is still time consuming as you have to do several reboots and furthermore, the procedure of installing updates, rebooting an…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now