简体   繁体   中英

How can I execute psexec commands in bat file

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM