How can I execute a program on a remote machine using powershell?

link|improve this question

feedback

7 Answers

up vote 5 down vote accepted

The cool new way to do this is with WinRM. I've seen this demo'd on Windows Server 2008 R2, although there is a download with powershell v2 & WinRM for other windows operating systems.

The not so cool (or new) way to do this is to use psexec, which isn't powershell, but I'm sure there's some way to invoke it via powershell-esque syntax.

link|improve this answer
agreed - what he said. – Trondh Oct 23 '09 at 12:08
1  
Just to add a little more detail, for a quick test setup, do a "winrm quickconfig" on the server to set it up to receive requests. Use the Invoke-Command cmdlet from the client to send a command to the server. – Ameer Deen May 23 '11 at 14:29
feedback

You could also use WMI and remotely start a process. It won't be interactive and you'll have to trust that it will end on its own. This doesn't require anything on the remote computer other than open ports for WMI.

Function New-RemoteProcess {
    Param([string]$computername=$env:computername,
        [string]$cmd=$(Throw "You must enter the full path to the command which will create the process.")
    )

    $ErrorActionPreference="SilentlyContinue"

    Trap {
        Write-Warning "There was an error connecting to the remote computer or creating the process"
        Continue
    }    

    Write-Host "Connecting to $computername" -ForegroundColor CYAN
    Write-Host "Process to create is $cmd" -ForegroundColor CYAN

    [wmiclass]$wmi="\\$computername\root\cimv2:win32_process"

    #bail out if the object didn't get created
    if (!$wmi) {return}

    $remote=$wmi.Create($cmd)

    if ($remote.returnvalue -eq 0) {
        Write-Host "Successfully launched $cmd on $computername with a process id of" $remote.processid -ForegroundColor GREEN
    }
    else {
        Write-Host "Failed to launch $cmd on $computername. ReturnValue is" $remote.ReturnValue -ForegroundColor RED
    }
}

Sample usage:

New-RemoteProcess -comp "puck" -cmd "c:\windows\notepad.exe"
link|improve this answer
I'm getting errors on this and I can't say what it is happening. Is there any way to print the error? – Gabriel Guimarães Oct 26 '10 at 14:48
feedback

Here's the psexec/powershell link.

link|improve this answer
feedback

RE: Nick's answer

Yes, PowerShell v2 (with WinRM 2.0) is bundled with Server 2008 R2 and Windows 7. A downlevel version should be available soon for XP, Vista, 2003 and 2008.

You always have the option of using WMI also to run remote stuff, but it is must interact with the console, that won't be a viable method.

link|improve this answer
feedback

it all works really well - but ... I wanna start the application / program in another profile/user. Now it starts as in my credentials which I can't use. I want to start it in another users credentials.

I'm domain admin - and I don't have the password on the other user.

link|improve this answer
feedback

Interesting enough I used this to run notepad on a remote computer and it didn't appear. I checked the Task manager and the process ID that the call returned was indeed there!

Windows stated that this was a security concept and the process would run hidden/ or in the background!

link|improve this answer
feedback

I'm not sure if you would be able to do this. We use Proxy's Remote Computer software at my job, but I am on the user end, not the admin side.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.