How to run as username

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
MrShortcut
Posts: 7
Joined: 24 May 2011 17:48

How to run as username

#1 Post by MrShortcut » 24 May 2011 17:57

I have to find a way to easily verify that hundreds of stations rebooted within the last 24 hours. Unfortunately they do not support PSEXEC so I have to come up with a new script.

With what very limited knowledge I have I found the following script and was able to easily compile and incorporate the IP addresses I need to check, however it only runs correctly for the workstations on my domain. Is there a way I can define what username and password to run it as so that I can run it as the workstations local Admin account?

Code: Select all

echo off
:// Give the name of the target computer

Set strComputer=10.11.1.21
for /f "Tokens=2 Delims==" %%i in ('wmic /node:%strComputer% OS Get LastBootUpTime /format:list') Do Set LastBootUpTime=%%i
Set LBT-Year=%LastBootUpTime:~0,4%||:   yyy
Set LBT-Month=%LastBootUpTime:~4,2%||:   mm
Set LBT-Day=%LastBootUpTime:~6,2%||:     dd
Set LBT-Hour=%LastBootUpTime:~8,2%||:    hh
Set LBT-Minute=%LastBootUpTime:~10,2%||: mm
Set LBT-Second=%LastBootUpTime:~12,2%||: ss
cls
@echo %strComputer% Last Boot Date= %LBT-Month%/%LBT-Day%/%LBT-Year%>>Lastboot.txt


Set strComputer=10.11.1.22
for /f "Tokens=2 Delims==" %%i in ('wmic /node:%strComputer% OS Get LastBootUpTime /format:list') Do Set LastBootUpTime=%%i
Set LBT-Year=%LastBootUpTime:~0,4%||:   yyy
Set LBT-Month=%LastBootUpTime:~4,2%||:   mm
Set LBT-Day=%LastBootUpTime:~6,2%||:     dd
Set LBT-Hour=%LastBootUpTime:~8,2%||:    hh
Set LBT-Minute=%LastBootUpTime:~10,2%||: mm
Set LBT-Second=%LastBootUpTime:~12,2%||: ss
cls
@echo %strComputer% Last Boot Date= %LBT-Month%/%LBT-Day%/%LBT-Year%>>Lastboot.txt


Set strComputer=10.11.1.23
for /f "Tokens=2 Delims==" %%i in ('wmic /node:%strComputer% OS Get LastBootUpTime /format:list') Do Set LastBootUpTime=%%i
Set LBT-Year=%LastBootUpTime:~0,4%||:   yyy
Set LBT-Month=%LastBootUpTime:~4,2%||:   mm
Set LBT-Day=%LastBootUpTime:~6,2%||:     dd
Set LBT-Hour=%LastBootUpTime:~8,2%||:    hh
Set LBT-Minute=%LastBootUpTime:~10,2%||: mm
Set LBT-Second=%LastBootUpTime:~12,2%||: ss
cls
@echo %strComputer% Last Boot Date= %LBT-Month%/%LBT-Day%/%LBT-Year%>>Lastboot.txt

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How to run as username

#2 Post by Ed Dyreen » 24 May 2011 19:40

the runas /? command may help u .

MrShortcut
Posts: 7
Joined: 24 May 2011 17:48

Re: How to run as username

#3 Post by MrShortcut » 24 May 2011 21:09

Ed Dyreen wrote:the runas /? command may help u .


I've tried each variation I can but cannot get it to work. Instead of connecting to the remote computer and then trying to run the batch commands as the specified user it instead tries to run it as that user on my workstation (which doesn't exist). Am I trying to do something that cant be done?

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How to run as username

#4 Post by Ed Dyreen » 24 May 2011 21:16

Well a batch can be run from a workstation and get or change it's privileges to that of for example an administrator that must exist on that workstation.
It can connect to another workstation but if the credintials don't match, they have to be supplied by the batch.
It can then run a batch on that computer .

MrShortcut
Posts: 7
Joined: 24 May 2011 17:48

Re: How to run as username

#5 Post by MrShortcut » 24 May 2011 21:31

Ed Dyreen wrote:Well a batch can be run from a workstation and get or change it's privileges to that of for example an administrator that must exist on that workstation.
It can connect to another workstation but if the credintials don't match, they have to be supplied by the batch.
It can then run a batch on that computer .



That sounds like exactly what I am trying to do, I just cant get the code right. For examply I am trying to run the batch as on my computer and once it dials into 10.11.1.21 it runs the rest as "employee" which is the local admin. However each time it runs I get an access denied because it tries to run as mycomputername\employee instead of just employee (if that makes sense).

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How to run as username

#6 Post by Ed Dyreen » 24 May 2011 21:41

If you are connecting to another machine and the batch must run from that machine, then it has to be started from that machine !
for example

Code: Select all

start "!TITLE!" /low /min /wait /D "!SystemROOT!\system32" "%comspec%" /c "%%~!"

/D is the active directory for the batch, in your case somewhere on the employees computer .

MrShortcut
Posts: 7
Joined: 24 May 2011 17:48

Re: How to run as username

#7 Post by MrShortcut » 24 May 2011 21:51

I guess this wont work then. I'm assuming that since the machine isn't mapped to my PC I wouldn't be able to get the results added to the log?

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How to run as username

#8 Post by Ed Dyreen » 24 May 2011 22:11

the batch that u use to login to the client starts a batch on the client and waits for that batch to complete. it then copies the result back to your pc. that is possible.

MrShortcut
Posts: 7
Joined: 24 May 2011 17:48

Re: How to run as username

#9 Post by MrShortcut » 25 May 2011 12:13

Thanks Ed. I'm not sure they'll like me copying a batch file to the workstation (even temporarily) so I'll keep searching. I'll keep it in mind in case they OK it or it can be added to a different task.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How to run as username

#10 Post by Ed Dyreen » 31 May 2011 03:25

You really don't get it !

You don't need to copy anything, just login with their username they use on that machine as administrator. And do your thing.

Code: Select all

>nul 2>&1    net use * /delete /yes
>nul 2>"%%~?" net Use /Persistent:No !Drive.%ServerReferName%! "%%~!" "!User.Administrator.Passw!" /user:Administrator


and the next

Code: Select all

>nul 2>&1    net use * /delete /yes
>nul 2>"%%~?" net Use /Persistent:No !Drive.%ServerReferName2%! "%%~!" "!User.Administrator2.Passw!" /user:Administrator2


You could spawn a new instance of CMD with runas just as easy.

Post Reply