Hopefully "ScriptingTipsAndTricks" helps you with your batch file or vbscript scripting :-)
[Bottom][Contents][Prev]: IsIpAddress() - determine is a string looks valid as an IP4 address[Next]: LOGGING a Whole Scripts Execution to STDOUT and FILE - uses tee.exe
\->Batch Files->LoggedOnLocallyUsers - useful over remote computers

[anchor]

The Code for: "LoggedOnLocallyUsers [useful over remote computers].cmd"

This is the example, a shortcut was installed to this code :

@echo off
::@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
:: $Header:   C:/DBAREIS/Projects.PVCS/Win32/ScriptingTipsAndTricks/EXAMPLE[cmd].LoggedOnLocallyUsers [useful over remote computers].cmd.pvcs   1.0   11 Jul 2014 19:31:02   USER "Dennis"  $
::@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION


echo Looking for logged on users on my own machine, should be me of course :-)
echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
call :LoggedOnLocallyUsers "ANSWER" "%COMPUTERNAME%"
echo ANSWER=%ANSWER%
goto :EOF






::===========================================================================
:LoggedOnLocallyUsers
::    P1 = Name of an Environment Variable to get results
::    P2 = Name of a computer
::===========================================================================
    ::--- Initialization ----------------------------------------------------
    set     %~1=
    set     TmpFile=%TEMP%\LoggedOnLocallyUsers.%RANDOM%.tmp
    set TrimVbsFile=%TEMP%\Trim[Leading+Trailing]Whitespace.%random%.vbs
    if "%TAB%" == "" call :SetChar TAB  9
    call :CREATE_VBS_TRIM "%TrimVbsFile%"

    ::--- Get user info -----------------------------------------------------
    set    CMD=psLoggedOn.exe /l /AcceptEULA \\%~2
    %CMD%  2>nul > "%TmpFile%"
    set  LineCnt=0
    set UserList=
    for /F "usebackq delims=" %%L in ("%TmpFile%") do call :LoggedOnLocallyUsersLine "%%L"
    set %~1=%UserList%

    ::--- Cleanup -----------------------------------------------------------
    del  "%TmpFile%"     >nul 2>&1
    del  "%TrimVbsFile%" >nul 2>&1
    goto :EOF

::===========================================================================
:LoggedOnLocallyUsersLine
::===========================================================================
    ::--- Need to ignore the first line -------------------------------------
    set /a LineCnt=LineCnt + 1
    if "%LineCnt%" == "1" goto :EOF

    ::--- Handle "<unknown time>" type entries -------------------------------
    set Line="%~1"
    set Line=%Line:<={%
    set Line=%Line:>=}%
    set Line=%Line:"=%

    ::--- Now Extract the info and return -----------------------------------
    for /F "tokens=1,* delims=%TAB%" %%A in ("%Line%") do set LogOnTime=%%A& set LogonUser=%%B
    call :TRIM "LogOnTime" "%LogOnTime%"
    call :TRIM "LogonUser" "%LogonUser%"
    set LogOnTime=%LogOnTime:{unknown time}=%

    if  not "%UserList%" == "" set UserList=%UserList%, &rem
    set UserList=%UserList%%LogonUser%(%LogonTime%)
    goto :EOF


::===========================================================================
:Trim
::    P1 = Name of an Environment Variable to get trimmed results
::    P2 = String to trim
::====================[ v14.157 ]============================================
    set TrimTmpFile=%TEMP%\TRIM-%random%.tmp
    echo %~2 | cscript.exe //NoLogo "%TrimVbsFile%" > "%TrimTmpFile%"
    set /P %~1= < "%TrimTmpFile%"
    del "%TrimTmpFile%" >nul 2>&1
    goto :EOF


::===========================================================================
:CREATE_VBS_TRIM
::  P1 = Full name of the VBS file to create
::===========================================================================
    echo '--- Takes one or more lines PIPED into STDIN and trims them to STDOUT --- > "%~1"
    echo.                                                                          >> "%~1"
    echo do while wscript.stdin.AtEndOfStream ^<^> true                            >> "%~1"
    echo    wscript.stdout.WriteLine trim(wscript.stdin.ReadLine)                  >> "%~1"
    echo loop                                                                      >> "%~1"
    goto :EOF





@REM **************[v08.178]******************
:SetChar
@REM *****************************************
    set VbsFile=%TEMP%\SetChar-%random%.vbs
    set CmdFile=%TEMP%\SetChar-%random%.cmd
    echo if Wscript.Arguments.Count ^<^> 2 then                                                                        > "%VbsFile%"
    echo    wscript.echo "REM Invalid parameters, expected 2 (1=EnvVar 2=AsciiCode), got " ^& Wscript.Arguments.Count >> "%VbsFile%"
    echo else                                                                                                         >> "%VbsFile%"
    echo    wscript.echo "@echo off"                                                                                  >> "%VbsFile%"
    echo    wscript.echo "SET " ^& Wscript.Arguments(0) ^& "=" ^& chr(Wscript.Arguments(1))                           >> "%VbsFile%"
    echo end if                                                                                                       >> "%VbsFile%"
    cscript.exe //NoLogo "%VbsFile%" "%~1" "%~2"                                                                       > "%CmdFile%"
    call "%CmdFile%"
    del "%VbsFile%" >nul 2>&1
    del "%CmdFile%" >nul 2>&1
    goto :EOF

Please note that that I am not trying to show how great I am by producing batch files 9,000 characters long on one line that no one will understand or be able to debug when they go wrong. I am going out of my way to comment the code and make it verbose so beginners and advanced users will both benefit. I don't claim to be an expert that knows everything, if I'm wrong or make a mistake then please contact me and let me know :-)


Microsoft awarded me an MVP (Most Valuable Professional award) in 2004, 2005, 2006, 2007, 2008 & 2009 for the Windows SDK (Windows Installer) area.Please email me any feedback, additional information or corrections.
See this page online (look for updates)

[Top][Contents][Prev]: IsIpAddress() - determine is a string looks valid as an IP4 address[Next]: LOGGING a Whole Scripts Execution to STDOUT and FILE - uses tee.exe


ScriptingTipsAndTricks© is (C)opyright Dennis Bareis 2003-2008 (All rights reserved).
Sunday September 07 2014 at 12:50pm
Visit ScriptingTipsAndTricks's Home Page
Microsoft awarded me an MVP (Most Valuable Professional award) in 2004, 2005, 2006, 2007, 2008 & 2009 for the Windows SDK (Windows Installer) area.