?
Solved

batch file to check my login status MSSQL machines

Posted on 2013-11-05
6
Medium Priority
?
411 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 85

Accepted Solution

by:
oBdA earned 2000 total points
ID: 39626610
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
ID: 39627333
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 85

Expert Comment

by:oBdA
ID: 39627352
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:mngong_rc
ID: 39627404
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 85

Expert Comment

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

Author Comment

by:mngong_rc
ID: 39628662
Not a problem
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The way I use Experts Exchange to assist me in analyzing and diagnosing a problem is I first enter a Verbose Question at Experts Exchange like: Office 2007 will hang when opening and saving files I then launch WordPad (any text editor will do) an…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

649 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