I am running the below batch file to connect the remote machine & run a powershell script on the remote machine using psexec.
Running batch file on local machine (myscript.bat)
Running Command: myscript.bat \\mymachine
set machinename=%1
#echo " started"
PsExec.exe %machinename% -u myID -p myPwd -i -d cmd /c mkdir C:\test
xcopy DirChk.ps1 %machinename%\C$\test
psexec.exe %machinename% -u myID -p myPwd cmd.exe /c 'echo .|powershell.exe -file C:\Test\DirChk.ps1'
#echo "Completed"
Error:
Starting PsExec service on \\mymachine ...Processing -File
'C:\Test\DirChk.ps1'' failed because the file does not have a '.ps1'
extension. Specify a valid PowerShell script file name, and then try
again.
Try double quotes. Batch files are interpreted by cmd, which doesn't understand single quotes. This should work:
psexec.exe %machinename% -u myID -p myPwd cmd.exe /c "echo .|powershell.exe -file C:\Test\DirChk.ps1"
Related
I want to the fetch the 'Status' of Tomcat service using PsExec and Powershell. As there are few approvals pending with Windows Security team I am forced to use PsExec
I tried executing the following command using PsExec and it works fine
PS C:\> .\PSExec.exe \\10.x.x.x -u DOMAIN\sathish -p mypwd cmd /c "powershell -noninteractive -command Get-Service -Name Tomcat"
Status Name DisplayName
------ ---- -----------
Running Tomcat Apache Tomcat 8.5
However, I would like to fetch the status of Tomcat by filtering the output and it failed with the following message
PS C:\> .\PSExec.exe \\10.x.x.x -u DOMAIN\sathish -p mypwd cmd /c "powershell -noninteractive -command Get-Service -Name Tomcat | Select -ExpandProerty Status"
'Select' is not recognized as an internal or external command, operable program or batch file.
cmd exited on 10.165.135.81 with error code 255.
How to filter the output so that I can get the status of Tomcat ?
I tried the following and it worked
C:\> .\PSExec.exe \\10.x.x.x -u DOMAIN\sathish -p mypwd cmd /c 'powershell -noninteractive -command "Get-Service -Name Tomcat | Select -ExpandProperty Status" '
Open the cmd with single-quotes and execute powershell commands within double-quotes
Thanks all for the help
I am trying to execute PowerShell script on 10 computers on same $env:userprofile path using psexec.But it won't work. If i specify path like "c:\Users\bob\" , it works. Below script is not working.
psexec -u domain\bob -p password -h -d cmd /c powershell.exe -Command "(New-Object System.Net.Webclient).DownloadFile('http://x.x.x.x/1.exe','$env:userprofile\1.exe')"
When I run this command locally on a remote machine it works:
powershell -Command "(New-Object Net.WebClient).DownloadFile('<WebPath>', 'C:\Users\<user>\Desktop\file.exe')"
When I try the same command remotely using PsExec in a batch file:
(Set downloadFileCommand = powershell -Command "(New-Object Net.WebClient).DownloadFile('%WebPath%', 'C$\Users\<user>\Desktop\file.exe')")
PsExec.exe \\<remote_machine> -u %username% -p %password% -s -d cmd /c %downloadFileCommand%
I am getting "cmd started on remote_machine with process ID #id_number."
However, nothing happened and the download wasn't executed. The suggestions here:
Run PowerShell scripts on remote PC
didn't work for me.
Any suggestions?
Edit:
I managed to download the file through the command line (in cmd) using this command:
PsExec.exe \\<remote_machine> -u <username> -p <password> -d powershell -Command (New-Object Net.WebClient).DownloadFile('<url address','C:\file.exe')
But it doesn't work when I try this in a batch file:
(Set DownloadInstaller = "powershell -Command (New-Object Net.WebClient).DownloadFile('<url address','C:\file.exe')")
PsExec.exe \\<remote_machine> -u %username% -p %password% -h -d cmd /c %DownloadInstaller%
This worked - all in one line. I have a loop variable, a remote computer (%%a). I assumed that when I use PsExec, C:\ ... \file.exe "thinks" of C:\ locally in the remote computer, but that probably wasn't the case.
I had to write it all in one line, since the path in DownloadFile (The location I want to download the file to) is locally in the remote computer (which is a loop variable):
PsExec.exe \\%%a -u %username% -p %password% -h -d cmd /c powershell.exe -Command "&{ (New-Object System.Net.WebClient).DownloadFile('%url%','\\%%a\C$\Users\<username>\Desktop\%file%')}")
I run the script from the file in Jenkins. Command:
psexec -i -s cmd.exe /c echo . | powershell.exe -file "c:\Program Files (x86)\Applications\Jenkins\jobs\Deploy\workspace\Deploy\script.ps1"
How can I pass a value for the "build" variable?
Please, help me
Thanks
You would simply follow the path with your arguments. Such as:
psexec -i -s cmd.exe /c echo . | powershell.exe -file "c:\Program Files (x86)\Applications\Jenkins\jobs\Deploy\workspace\Deploy\script.ps1" '-build 1.0'
You can see the command line syntax for PowerShell.exe here. It shows:
PowerShell[.exe]
[-File <FilePath> [<Args>]]
If that does not work for you, you can try and dot source the script in a -command scriptblock, such as:
psexec -i -s cmd.exe /c echo . | powershell.exe -command {. "c:\Program Files (x86)\Applications\Jenkins\jobs\Deploy\workspace\Deploy\script.ps1" -build 1}
I made mistake in the script. I forgot to insert the following code:
Param(
[int32]$build=0
)
As TheMadTechnician said, the command to run the script is:
psexec -i -s cmd.exe /c echo . | powershell.exe -file "c:\Program Files (x86)\Applications\Jenkins\jobs\Deploy\workspace\Deploy\script.ps1" -build %BUILD_NUMBER%
I am using psexec to execute a .bat on a system. This will fingerprint the system and return the info back to me. The command I use is:
psexec \\server -u username -p password -s -i -c .\fingerprint.bat
This works with most of the servers, but some of them won't work with the -i switch. Is there a way to say, if this fails, try to do it again without the -i? I am using powershell to execute the script.
$servers = get-content .\input.txt
foreach($server in $servers) {
psexec \\$server -u username -p password -s -i -c .\fingerprint.bat
}
Check the $LastExitCode automatic variable. This gets set when PowerShell invokes an EXE. Whatever exit code the EXE returns gets put in $LastExitCode. I'm pretty sure psexec returns 0 for success and anything else means failure e.g.:
foreach($server in $servers) {
psexec \\$server -u username -p password -s -i -c .\fingerprint.bat
if ($LastExitCode) {
psexec \\$server -u username -p password -s -c .\fingerprint.bat
}
}
This is using a PowerShell trick where if ($LastExitCode) will if evaluate to true if $LastExitCode is anything but 0.