Page 1 of 1

How to run as username

Posted: 24 May 2011 17:57
by MrShortcut
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

Re: How to run as username

Posted: 24 May 2011 19:40
by Ed Dyreen
the runas /? command may help u .

Re: How to run as username

Posted: 24 May 2011 21:09
by MrShortcut
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?

Re: How to run as username

Posted: 24 May 2011 21:16
by Ed Dyreen
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 .

Re: How to run as username

Posted: 24 May 2011 21:31
by MrShortcut
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).

Re: How to run as username

Posted: 24 May 2011 21:41
by Ed Dyreen
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 .

Re: How to run as username

Posted: 24 May 2011 21:51
by MrShortcut
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?

Re: How to run as username

Posted: 24 May 2011 22:11
by Ed Dyreen
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.

Re: How to run as username

Posted: 25 May 2011 12:13
by MrShortcut
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.

Re: How to run as username

Posted: 31 May 2011 03:25
by Ed Dyreen
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.