PsExec use MKDIR - cmd

Hi I try to create a folder on local user which doesn't have access right.
So the script is to help user creating a folder by using domain admin account and will be converted to EXE when it run.
So I figure out using PsExec to use it. But I'm no idea how to combine it with MKDIR.
Here is the BAT script.
#echo off
setlocal enableextensions
set today=%date:~10,4%-%date:~7,2%-%date:~4,2%
set todayyear=%date:~10,4%
set todaymonth=%date:~10,4%-%date:~7,2%
set pass_local=adminpass
set path_pskill="\\SERVER\$PROGRAM\PsKill.exe"
set path_psexec="\\SERVER\$PROGRAM\PsExec.exe"
set path_backup=\\SERVER\$WORKSTATIONS\UPGRADE
set path_sourcedrive=C:\Users
set path_source01=AppData\Local\Microsoft\Outlook
set path_source02=Contacts
set path_source03=Desktop
set path_source04=Documents
set path_source05=Downloads
set path_source06=Favorites
set path_source07=Pictures
set path_source08=Music
set path_source09=Videos
subst b: "%path_backup%"
%path_psexec% \\%computername% -u DOMAIN\Administrator -p %pass_local% "cmd /C \"mkdir \"b:\%todayyear%\%todaymonth%\%username%\"\""
subst b: /D
Other simple to understand is:
psexec.exe \\hostname -u DOMAIN\Administrator -p adminpass "cmd /C \"mkdir \"b:\%todayear%\%todaymonth%\%username\"\""
Anyone please can help ?

Use the below command to create a directory in remote location.
psexec \\\IPAddress -u username -p Password cmd /c mkdir c:\testfolder

Actually, you're on the right track as long as the login credentials that you're adding is correct, else, you'll receive an access denied error. Also, you may have to place some START /wait timers in the batch script to make sure that the lines before the psexec call get executed first.

Related

Batch script access denied even with admin privileges

I have a batch script in Windows7 to update the hosts file that fails.
I am logged as a user with administrative rights.
Even if I run the script with the "Run as administrator" option I get Access denied. 0 files copied when executing this part of the script:
for /f "tokens=1-2 delims=:" %%a in ('ipconfig^|find "IPv4"') do set ip=%%b
set ip=%ip:~1%
REM create changing part of hosts file...
if exist %temp%\temp.txt del %temp%\temp.txt
echo %ip% myproxy >> %temp%\temp.txt
REM check this...
set hostpath=C:\WINDOWS\system32\drivers\etc
REM add static part of hosts file
type "%hostpath%\hosts-static" >> %temp%\temp.txt
REM replace hosts file
copy /y %temp%\temp.txt "%hostpath%\hosts"
ipconfig /flushdns
netsh interface IP delete arpcache
pause
I also tried to create a shortcut and set the "Advanced -> Run as Administrator" option but no luck.
If I open a cmd shell as Administrator and then run the script from there everything works fine, but no way of running it directly double-clicking on the file (or its link).
Any idea?
EDIT:
added the whole script.
I tried creating a shortcut for the following command to execute as Administrator
C:\Windows\System32\cmd.exe /c script.bat
and it is also failing.
From the same shortcut (without arguments) I can open a window where I can execute the batch correctly. I really cannot see why.
Obviously a late response, but just solved this issue with a very straightforward solution so I thought I'd share:
Using ICACLS you can modify access control lists (ACLs) to bypass access denied errors.
Run the following command:
ICACLS C:\path\to\batch\file\directory\* /C
the parameter /C tells the batch file to bypass access denied errors. Cheers.
Try attrib -r -s -h -a "%hostpath%\hosts" before your copy command. If any file is attributed +r, +s, or +h, you'll get "Access is denied" if you try to overwrite it using copy.

Passing Y/N when executing a remote bat file from command line

I am trying to execute remote batch file. I could invoke the batch file using PsExec but unable to complete due to :choice in the batch file.
Here is the snippet from batch file
:choice
set /P c=Are you sure you want to continue [Y/N]?
if /I "%c%" EQU "Y" goto :execute_script
if /I "%c%" EQU "N" goto :END
goto :choice
which populating question
I want to handle this question from the command which is like :
cmd /c start C:\temp\PSEXEC\PsExec.exe \\server -u username -p password cmd /c (^cd C:\BatchExecutors ^& SnapExecutor.bat location^)
Suggestions appreciated. Thanks in Advance.
You can use a strange CMD behaviour when 2 batch files have the same label, to bypass the question. But any code prior to the question would be ignored.
To do this, create another batch file with this inside:
call :execute_script
goto:eof
:execute_script
cd /D C:\BatchExecutors
SnapExecutor.bat %*
So what happens here is that this script will call SnapExecutor.bat, but instead of starting from the begining of the script, it will start from :execute_script
The problem now is how to execute this remotely. You might be able to create this script in a remote writable folder using this command:
cmd /c start C:\temp\PSEXEC\PsExec.exe \\server -u username -p password cmd /c "cd /D c:\[temp folder] &echo call :execute_script>temp.bat &echo goto:eof>>temp.bat &echo :execute_script>>temp.bat &echo cd /D C:\BatchExecutors>>temp.bat &echo SnapExecutor.bat %%*>>temp.bat"
(This will create the batch file, then you use this to call it:
cmd /c start C:\temp\PSEXEC\PsExec.exe \\server -u username -p password cmd /c "C:\[temp folder]\temp.bat"
NOTE:
Change [temp folder] to a writeable folder on the remote PC.

PSEXEC will copy .bat file but wont run it on remote computer?

i have looked around a fair bit, but cant seem to find an answer to this.
I am creating a script that is a part of the off boarding process for our company. As part of the process, it grants permssion for another user to access the exiting users profile share (working fine). The next part will map a network drive remotely (im having trouble with this. I am using PSEXEC to dispatch a bat script which maps the users drive:
#echo off
::Welcome note
echo Welcome to the User EXIT script!
:Start
:: set variable to be used throughout script for the username of the person exiting.
set /p uname="Please enter the username for exit:"
set /p cleanupu="Please enter the username for homedrive & mailbox cleanup:"
set /p computermap="Please enter the Computer to clean up the Homedrive:"
echo The username for exit is: %uname%
echo The username that is cleaning up is: %cleanupu%
echo The computer for the homedrive to be mapped to is %computermap%
set /P c=Is this correct [Y/N]?
if /I "%c%" EQU "Y" goto :init_confirm
if /I "%c%" EQU "N" goto :start
:init_confirm
::confirmation....
echo This script will exit the user: %uname%
pause
icacls "\\server\home$\%uname%" /grant DOMAIN\%cleanupu%:(OI)(CI)F
pause
echo net use z: \\server\home$\%uname%\ > map_temp.bat
psexec \\%computermap% -c -i -d map_temp.bat
pause
exit
This will copy the file to the remote computer and open up a blank command prompt window.
Can anyone see why this wont actually run map_tem.bat?
cheers

Batch file doesn't continue until after I exit psexec remote connection

I'm trying to make a batch file to run a script on remote server. If I enter the commands below in the cmd prompt seperately it works fine but it seems to hang after I enter the psexec commands. It only continues the rest of the batch file when I exit the psexec remote connection. The rest of the commands are run on my local computer(which I don't want). Anyone have any ideas or suggestions?
psexec \\ServName -u DOMAIN\UserName -p password cmd.exe
pause
cd c:\Users\UserName
pause
cscript \\NetworkName\filepath\blankTest.vbs
You can bundle the commands into one batch file and then execute that using one line:
psexec \\ServName -u DOMAIN\UserName -p password cmd.exe -c mybatchfile.bat
That will cause the file to be copied to the remote machine first.
Alternatively if you have problems with that, copy the file first, then execute it; note the different parameter, /c vs -c:
copy mybatchfile.bat \\ServName\Admin$
psexec \\ServName -u DOMAIN\UserName -p password cmd.exe /c mybatchfile.bat
Personnaly if I want to start a background process in a batch I use this :
start /B cmd /C "mycommand" which in your case should give :
start /B cmd /C "psexec \\ServName -u DOMAIN\UserName -p password cmd.exe"

Opening cmd using psexec to a specific remote directory

The goal is to delete the temporary internet files on a remote computer.
start psexec -u domain\username -p password -s \\xxx.xxx.xxx.xxx
cmd cd C:\Documents and Settings\USERACCOUNT\Local Settings\Temporary Internet Files
Which is connecting to the remote computer but just opening to c:windows\system. I am then able to cd to that directory and use del /f /s /q *.* to delete all the problem files.
I tried using psexec \\computer cmd /c del fileName but had even less luck with that.
Why bother cd'ing to directory first? This should work:
psexec \\1.2.3.4 -u "domain\username" -p "password" cmd /c del /f /s /q "C:\Document and Settings\USERACCOUNT\Local Settings\Temporary Internet Files"
I finally got it to work using
psexec -u domain\user -p password \\xxx.xxx.xxx.xxx -s cmd /c rd "C:\Documents and Settings\%USERACCOUNT%\Local Settings\Temporary Internet Files\Content.IE5\" /s /q
which connects to the remote server, goes straight to the directory I want and proceeds to delete the particular temp internet files folder that's causing issues without bothering me at all.
Your command is okay but missing the server name
psexec \\\servername -u ...
Use the below command to create a directory in remote location.
psexec \\\IPAddress -u username -p Password cmd /c mkdir c:\testfolder

Resources