Wanted to execute .exe file located in current server to remote server without copying the file

Sri 106 Reputation points
2022-04-22T17:37:49.927+00:00

Hello Techies,

I'm just a noob in PS lengthy commands and scripting - still in a learning stage.

I'm struggling to find the command to execute mpam.exe on a remote server.

I have a file in C:\temp\mpam.exe in Server1. So wanted to execute this file in Server2 without copying the file to Server2.

Below are the attempts I tried but had no luck:

Invoke-Command -ComputerName Server2 -ScriptBlock {\Server1\c$\Temp\mpam.exe}
\Server1\c$\Temp\mpam.exe : The term

  • CategoryInfo : NotSpecified: (\Server1....exe : The term :String) [], RemoteException
  • FullyQualifiedErrorId : NativeCommandError
  • PSComputerName : Server2

Invoke-Command -ComputerName Server2 -ScriptBlock {powershell.exe \Server1\c$\Temp\mpam.exe}
\Server1\c$\Temp\mpam.exe : The term

  • CategoryInfo : NotSpecified: (\Server1....exe : The term :String) [], RemoteException
  • FullyQualifiedErrorId : NativeCommandError
  • PSComputerName : Server2

Invoke-Command -ComputerName Server2 -ScriptBlock {powershell.exe \Server1\c$\Temp\mpam.exe -ArgumentList '/slient' -Wait}
\Server1\c$\Temp\mpam.exe : The term

  • CategoryInfo : NotSpecified: (\Server1....exe : The term :String) [], RemoteException
  • FullyQualifiedErrorId : NativeCommandError
  • PSComputerName : Server2

This one came up with no errors, however, mpam hasn't been installed on Server2

Invoke-Command -ComputerName Server2 -ScriptBlock {cmd.exe \Server1\c$\Temp\mpam.exe -ArgumentList '/slient' -Wait}
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.

I've also tried using Enter-PSSession and it
[Server2]: PS C:\Users\user1\Documents> \Server1\c$\Temp\mpam.exe
The term '\Server1\c$\Temp\mpam.exe' is not recognized as the name of a cmdlet, function, script
file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct
and try again.

  • CategoryInfo : ObjectNotFound: (\Server1....\mpam.exe:String) [], CommandNotFoundException
  • FullyQualifiedErrorId : CommandNotFoundException

Any help would be highly appriciated.

Thanks in advance.

Cheers,
Sri

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,455 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. MotoX80 32,736 Reputation points
    2022-04-24T14:06:37.367+00:00

    Depending on how you have configured file sharing and user accounts, you are likely running into the double hop problem.

    https://learn.microsoft.com/en-us/powershell/scripting/learn/remoting/ps-remoting-second-hop?view=powershell-5.1

    Put simply, your credentials can get you from server1 to server2, but can't authenticate to a 3rd machine, even if it's back to server1.

    If you have Active Directory, you can follow the instructions on that page to implement a solution that fits your environment.

    Why don't you want to copy the executable to server2? That would be an easy solution. Copy mpam.exe to \server2\C$\windows\temp, then using Invoke-Command execute it and then delete it.

    You can also copy the file using PS remoting.

    https://richardspowershellblog.wordpress.com/2015/05/28/copy-files-over-ps-remoting-sessions/