Opening cmd using psexec to a specific remote directory - windows

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

Related

How to delete the contents of temp folder in remote machine through command

I have a requirement where i need to delete the contents of the temp folder on number of remote windows machines.
i tried below wmic command to delete
WMIC /node:...** /user:xxxx /password:xxxx path cim_datafile WHERE "path='%Winddir%\temp*.tmp' AND Extension ='tmp'" delete**
But it throws an error saying
ERROR:
Description = The RPC server is unavailable.
Please suggest me a way to delete temp folder contents on remote machine.
Most simple way - if you're not strictly forced to use WMI, you can do that with psexec:
PsExec.exe \\<computer> cmd /c "rmdir /s /q %windir%\temp\"
or, if you're not logged into domain admin or global workstations admin account -
PsExec.exe -u <user> -p <password> \\<computer> cmd /c "rmdir /s /q %windir%\temp\"
If should not completely delete the folder since Windows keeps some files open and locked inside it, but anything not read-only or locked would be deleted, both files and folders.
Then, to do this with a list of computers, just use a cmd file like this (list.txt contains just computer names/ips, without \:
for /F %%s in (list.txt) do (
echo %%s
start "" /min PsExec.exe \\%%s cmd /c "rmdir /s /q %windir%\temp\"
)
Start command makes all psexec processes run in parallel and minimized so you don't have to wait for each computer to finish before starting another
psexec.exe is a part of sysinternals package from M. Russinovich, I'd like to leave a direct link but don't know if it's permitted or not.
I think it should be able to connect to remote pcs if you have access to file shares and remote computer management (and admin rights of course)

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.

How to run as admin?

How to make this bat script run as admin either on the local computer as a scheduled task or startup up scrip?
forfiles -p "C:\ProgramData\ESET\ESET Endpoint Antivirus\Logs\eScan" -s -m *.dat /D -0 /C "cmd /c del #path"
If you are just running this once, and not automating a number of scripts, you can just hold shift and right click to run as admin.

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"

PsExec use MKDIR

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.

Resources