I am trying to write a bat file that will accomplish the following:
psexec \\host1 cmd
d: #to change the remote server's drive
cd\
dir /s/b "file1" #searches for this file in host1 in its d: drive
How can I go about doing this
If you are on a current day Windows system, you can invoke the command on a remote computer using PowerShell from within your cmd .bat script. No need for psexec. The remote machine does need to be setup for PowerShell remoting. Get-Help about_Remote
powershell -NoProfile -Command "invoke-command HOST01 { cmd /C dir /S /B D:\file1 }"
If you are running in PowerShell:
invoke-command HOST01 { cmd /C dir /S /B D:\file1 }
Of course, in PowerShell you might as well use PowerShell cmdlets.
icm HOST01 { gci -n -rec D:\file1 }
-or-
Invoke-Command HOST01 { Get-ChildItem -Name -Recurse D:\file1 }
psexec \\host1 cmd /c "d: & cd\ & dir /s/b file1"
or simply
psexec \\host1 cmd /c "dir /s/b d:\file1"
The console in which the cmd is executed is automatically closed when the command finishes executing, so you won't actually see the result. You could leave cmd running (and console along with it) by using /k instead of /c, but that doesn't make much sense either. You appear to have an XY problem.
Related
How can I fix my batch file??
When I run the command in Windows 10 1903, it run totally fine. But when I run it in Windows 10 2004, the batch keep deleting file that's in the batch folder.
Here is the script (not full) :
ipconfig /flushdns
cd %windir%/temp
powershell ri * -recurse -force >NUL
cd %temp%
powershell ri * -recurse -force >NUL
this is the line that keep deleting my file in Windows 10 2004
Can someone plz help me, I already try to solve it. But I didn't find a way out
Rather than changing directory, directly pass the path of the directory to the ri command.
ipconfig /flushdns
powershell ri %windir%/temp/* -recurse -force >NUL
powershell ri %temp%/* -recurse -force >NUL
The Temp directory you're trying to empty is inside the protected %SystemDrive%\Windows location. In order for it to be emptied, you must, in the first instance, have permission to do that, and generally for that you should be running your script elevated, i.e. 'Run as administrator'.
Also, I would have thought it would be quicker to do this directly with native cmd.exe commands, rather than calling out to an external utility like %SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe:
#"%SystemRoot%\System32\ipconfig.exe" /FlushDNS
#PushD "%SystemRoot%\Temp" && (RD /S /Q . 2> NUL & PopD)
#PushD "%Temp%" && (RD /S /Q . 2> NUL & PopD)
I have a folder that contains over 40,000 video files.
I want to be able to list them all in a text file
I've used the command:
#echo off
set dirpath=%location
dir %dirpath% /-p /o:gn > "%dirpath%\DirContents.txt"
exit
However, this just list the directory command thats in cmd.
I want to be able to do this: have a text file that has:
File Name, Date Modified
import glob
import os
import time
with open("DirContents.txt","w") as fOut:
fOut.write("FileName, DateModified\n")
files = glob.glob("~/Videos/*")
for fName in files:
stat = time.ctime(os.stat(fName).st_mtime)
fOut.write("\""+ fName + "\", \"" + stat+"\"\n")
This is easily done using PowerShell. If you are on a supported version of Windows, it will have PowerShell.
Run this in a cmd.exe shell.
SET "LOCATION=C:\src\t"
powershell -NoLogo -NoProfile -Command ^
"Get-ChildItem -Path %LOCATION% |" ^
"ForEach-Object { '{0},{1}' -f #($_.Name, $_.LastWriteTime) }"
If you can use PowerShell without cmd.exe, it is even easier. Run this in a PowerShell shell.
$Location = 'C:\src\t'
Get-ChildItem -Path $Location |
ForEach-Object {'{0},{1}' -f #($_.Name, $_.LastWriteTime)} |
Out-File -FilePath "$Env:USERPROFILE\Desktop\DirContents.txt" -Encoding ascii
If you want to just stick with the old ways, run this in a cmd.exe shell.
FORFILES /P C:\src\t /M *.txt /C "cmd /c ECHO #file,#fdate #ftime"
I have a batch file with the following script (which i found here at stackoverflow)
#if (#CodeSection == #Batch) #then
#echo off
start runas "/user:domain\username" "cmd.exe"
CScript //nologo //E:JScript "%~F0"
goto :EOF
#end
WScript.CreateObject("WScript.Shell").SendKeys("mypass{ENTER}");
where mypass is the username's password.
this works perfectly and a new commandline window opens as the new user!
the problem is that i now need to run an sqlcmd with parameter and I can't find any way to write a batch that does both things together (open the new window as another user together with the command with parameters)
the sql command:
"C:\Program Files\Microsoft SQL Server\110\Tools\Binn\SQLCMD.EXE" -S "my.server.com, 1234" -d "myDataBase" -E -b -q "EXEC myDataBase.[dbo].[myTable] #parameter=1"
I tried moving the whole sql command to another batch (myBatch.bat) file and change the first batch to this:
start runas "/user:domain\username" "myBatch.bat"
but it doesn't work.
any help please?
runas /user:domain\user "cmd.exe /k c:\path\to\mybatch.bat test"
cmd /k to keep the console open after the batch execution, or cmd /c to close it
Hi I am New to scripting .
I have to remotely execute a PSEXEC command to delete al files in a certain drive in a few computers.
I do have a TXT file with every ip address of all the computers.
Is there a way to use PSEXEC to cmd command all the computers to delete the folders by executing every command for every IP in my txt file?
currently i am using this line whereby i need to manually enter the ip address in the command line for eg . PsTools>PsExec.exe \ 1.1.1.1 , 1.1.1.2 cmd /c rmdir /S /Q D:\
FOR /F %%i IN (FileWithEveryIP.txt) DO PSEXEC \\%%i CMD /C DIR folderToDelete
Use %i instead of %%i if you run it from command prompt window instead of batch file.
Replace DIR with RMDIR /S /Q if it works.
I am trying to run this batch file remotely It will kill the IE process's but when I try to open a .lnk file it won't do it. When I go onto that machine, open up the command prompt and type in the command to run the .lnk file it works with no issues.. please help!
Code to remotely execute batch file:
psexec -u Administrator -p password -i -d \\hostname "c:\Emergency_POD\test.bat"
Code on machine to run: (Only the taskill command works.. not the for command)
cd/
taskkill /im iexplore.exe /f
for %a in ("C:\Emergency_POD\*.lnk") do #start "" "%a"
Command to run on cmd (This command works with no issues:
for %a in ("C:\Emergency_POD\*.lnk") do #start "" "%a"
You'd probably be better off with %%a in place of %a in the batch file.