2 Replies Latest reply on Feb 28, 2012 12:08 PM by mlambert

    VBScript to get username and pass it as a variable to a command line SWD

    Apprentice

      Using LANDesk 9.0 sp2, large mix of windows XP Pro and windows 7, when using the %username% in a batch file it is ignoring the variable and erroring out.  Testing out Startasuser.exe is also not working on win XP as in the past.  Found on the fourms it also does not work in win 7.  Figured out a workaround for assigning the username variable and passing it to a command line.  Batch file package, now.bat as main package, and loggedON1.vbs
      as an additional file.  This is a test batch file to test this out, works in XP and win 7, have to change the batch file for C:\users, etc.

       

      now.bat:

      ------------------------

      @echo on
      cls
      title testing get current user

       

      :start

      ::pre-cleanup

      ::make sure tmp file is not there
      if exist C:\WINDOWS\TEMP\loggedonuser.tmp (del /Q C:\WINDOWS\TEMP\loggedonuser.tmp) ELSE (goto next2)
      echo.
      echo.

       

      :next2
      ::vbs script to get logged on user
      ::loggedON1.vbs outputs tmp file to C:\WINDOWS\TEMP\loggedonuser.tmp
      start /wait cscript //NOLOGO loggedON1.vbs
      echo.
      echo.

      for /f "tokens=1" %%A in (C:\WINDOWS\TEMP\loggedonuser.tmp) do (
      set username=%%A
      )
      echo.
      echo.

       

      ::do something with it

      ::copy file
      xcopy /y "c:\windows\saplogon.ini" "C:\Documents and Settings\%username%\Application Data\SAP\Common\"
      echo.
      echo.

       

      ::cleanup
      if exist C:\WINDOWS\TEMP\loggedonuser.tmp (del /Q C:\WINDOWS\TEMP\loggedonuser.tmp) ELSE (goto end)
      echo.
      echo.


      :end
      exit /B 0

       

      --------------------------------------

       

      loggedON1.vbs

      --------------------------------------

      Option Explicit
      Dim oWMI, oProcess
      Dim sComputer, sWMIQuery, sUser, sDomain, sAllUsers
      Dim colProcesses
      Dim fso, fl

      sComputer = "."

      Set oWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!//" & sComputer & "/root/cimv2")
      sWMIQuery = "SELECT * FROM Win32_Process WHERE Name = 'explorer.exe'"
      Set colProcesses = oWMI.ExecQuery(sWMIQuery)

      For Each oProcess In colProcesses
        oProcess.GetOwner sUser, sDomain
        sAllUsers = sAllUsers & sUser & vbCrLf
      Next
      'Msgbox sAllUsers, vbOkOnly, "Logged on Users"
      'Wscript.echo sAllUsers

      Set fso = CreateObject("Scripting.FileSystemObject")
      'OpenTextFile Parameters:
      '-Filename
      '-The 2 is for writing... 1 is reading and 8 is appending
      '-The "True" is to create if not already there.
      Set fl = fso.OpenTextFile("C:\WINDOWS\TEMP\loggedonuser.tmp", 2, True)
      fl.Write(sAllUsers)
      fl.Close : Set fl = Nothing
      Set fso = Nothing

      --------------------------------------

       

      Batch file and VBS script were frankensteined from the interwebs.

        • 1. Re: VBScript to get username and pass it as a variable to a command line SWD
          mlambert Apprentice

          This won't answer your question directly, but I can offer another potential approach.  Instead of only targeting the currently logged on user, you can perform the intended action (such as a copy a file) on each user profile on the system.  A while back I had a scenario that required replacing a simple INI file (if it existed) that was stored in the user's app data directory.  The app also put a shortcut on individual user desktops, so i removed that and put it on the all users desktop.  Many of the target machines were used by multiple users, so it was critical that I targeted all the profiles on each machine. 

           

          Here is what the batch script looked like:

          ************************************************

          rem Resets USER.INI (if necessary)

          for /f "tokens=*" %%i in ('dir /ad /b "%systemdrive%\documents and settings\"') do (

               if exist "%systemdrive%\documents and settings\%%i\Application Data\The College Board\Recruitment PLUS\user.ini" (
                    copy /y "new_user.ini" "%systemdrive%\documents and settings\%%i\Application Data\The College Board\Recruitment PLUS\user.ini"
                    )
               )
          rem if RPLUS shortcut is on any profile's desktop, delete it
          for /f "tokens=*" %%i in ('dir /ad /b "%systemdrive%\documents and settings\"') do if EXIST "%systemdrive%\documents and settings\%%i\desktop\rplus.url" del /q "%systemdrive%\documents and settings\%%i\desktop\rplus.url"
          rem copy RPLUS shortcut from working directory to All Users desktop
          copy "RPlus.url" "%ALLUSERSPROFILE%\Desktop\"

          ************************************************

           

          I was working with all Win XP devices, so this may have to be adapted for Win7, but you get the idea. 

          • 2. Re: VBScript to get username and pass it as a variable to a command line SWD
            mlambert Apprentice

            Also - I ran a quick test using the standard environmental variables %USERNAME% and %USERPROFILE% in a batch package using the option to run as "current user's account". 

             

            echo %userprofile% >> temp.txt

            echo %username% >> temp.txt

            As long as a user was logged in when the package tried to execute, it worked correctly.  When no user was logged in, I received an "invalid parameter" error from the batch file. 

             

            Based on what I am seeing, you should be able to get by without using the VBS or the Tmp file.  Instead, just use the %USERPROFILE% variable in your path and then either add logic to your script to test before using them or use a delivery method that waits until user logon. 

             

            The test would looke something like:

             

            if exist %userprofile% echo %userprofile% >>

             

                - or -

             

            if "%USERPROFILE%" == "" echo "no username defined" >> temp.txt ELSE echo %USERPROFILE% >> temp.txt