Quantcast
Channel: PowerShell
Viewing all articles
Browse latest Browse all 15370

Run a command on remote machine using powershell

$
0
0

hi,

I have a command to be executed on a remote machine using powershell. (Had tried using PsExec. But can't execute commands on a remote server (web server) as it was not shared over network.

I have the following commands.

Code 1:

Powershell
Enter-PSSession-ComputerNameserver1-Credentialusername$command="C:\\com1.bat"Invoke-Expression"$command"

Code 2:

Powershell
Invoke-Command-ComputerName[COMPUTER NAME]-ScriptBlock{[COMMAND TO BE EXECUTED]}-credential[USERNAME]

Code 3:

Powershell
Enter-PSSession-ComputerName[COMPUTER NAME]-Credential[USERNAME]cmd/crename"path\file1""file2"cmd/cxcopy"file""path\"

All of these will work in powershell ISE. But fails when run from ps1 file.

Update 1:It gives an error like system cannot find file specified. When I check into that, it was trying to rename the file from local machine instead...


Viewing all articles
Browse latest Browse all 15370

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>