Link to home
Start Free TrialLog in
Avatar of Zahid Ahamed
Zahid AhamedFlag for United States of America

asked on

How to run windows batch file remote computer

Hi,
I want to run a windows batch file on a remote computer using PowerShell. I have tried with this following command but didn't work.

Invoke-Command -ComputerName "ccccccc" -credential domain\xxxx -ErrorAction Stop -ScriptBlock {Invoke-Expression -Command:"cmd.exe /E 'E:\_deployment\ServicePack\Install_ServicePack.bat'"}

Please help
Avatar of eridzone
eridzone
Flag of Australia image

If you have an AD domain you can add scripts as logon scripts
What exactly did not work? Do you get an error?
Avatar of oBdA
oBdA

There are two obvious errors here.
One is the /E - this is the argument to enable/disable command extensions, and it expects ":On" or ":Off" afterwards. What you need (probably instead of /E) is /C to run the command and then close the shell.
Then you can't use single quotes in a cmd.exe command line - this works in PS only, not in cmd.exe.
The Invoke-Expression is not required at all.
Try
Invoke-Command -ComputerName "ccccccc" -Credential domain\xxxx -ErrorAction Stop -ScriptBlock {& cmd.exe /C "E:\_deployment\ServicePack\Install_ServicePack.bat"}

Open in new window

Avatar of Zahid Ahamed

ASKER

I have received an error.

The system cannot find the path specified.
    + CategoryInfo          : NotSpecified: (The system cann...path specified.:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
    + PSComputerName        : cccccccccccccccccccc
Well,  what the message says: the path "E:\_deployment\ServicePack\Install_ServicePack.bat" does not exist on cccccccccccccccccccc ...
Invoke-Command -ComputerName cccccc -Credential Domain\xxx -ScriptBlock {& cmd.exe cd e \ "E:\_deployment\ServicePack\Install_ServicePack.bat"}

After execute this above command using run as administrator it says completed but don't see any action
Because all you're doing with that command is a "cd". Use the exact syntax I posted above; don't add arbitrary commands to the command line.
I ran this but I have received the below error:
Invoke-Command -ComputerName ccccc -Credential Domain\xxx -ScriptBlock {& cmd.exe /C "E:\_deployment\ServicePack\Install_ServicePack.bat"}


The system cannot find the path specified.
    + CategoryInfo          : NotSpecified: (The system cann...path specified.:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
    + PSComputerName        : cccccccccccccccccccc


My guess is the batch file is in my local computer E:\ drive. Should I put the batch file in the remote computer or the local computer?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank You