Windows Remote Execution

This article compares different ways to run commands or executable remotely on one or multiple windows computers. I have came up with 4 option:

- WMI script

- WMIC

- Power Shell

- PSExac

- Task Scheduler

Those options are build in in Operating System with exception of PSexec that is part of system internal tools (now part of Microsoft) also available to download for free. There are many 3-rd party products that can execute command remotely like Microsoft System Center Configuration Manager, Microsoft System Center Operation manager, IBM Tivoli, 1E, BNC, HP OpenView, Novell ZenWork, but all of them requires that you have an agent installed on remote computer.

Differences between methods

I made a small comparison chart that summarizes the differences between these methods:

Syntax Examples:

WMIC"

WMIC /node:ComputerName process create call "cmd.exe /c ipconfig /all >c:\ipconfig.txt"
or
wmic /NODE: "RemoteComputer bios get serialnumber

PSexec:

psexec \\computername ipconfig /all

PowerShell:

Run the PSscript.ps1 script on the Server1 computer. The script is located on the local computer. The script runs on the remote computer and the results are returned to the local computer:

PS C:\> invoke-command -filepath c:\scripts\PSScript.ps1 -computerName Server1

Task Scheduler:

SCHTASKS /create /tn "My test Script" /tr "\"c:\script.cmd\" arguments" /sc daily /sd 3/29/2015 /st 11:00

WMI Script:

    '  Initiate WMI connection to remote computer
    '--------------------------------------------------------------------------
    Set objWMIService = GetObject("winmgmts:& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2:Win32_Process")
   ' Run script
   '-----------------------------------------------------------------------------
    Return = objWMIService.Create(strCmd,null,null,intProcessID)
    if Return = 0 Then
        Wscript.Echo strCmd & " Started with a process ID of & intProcessID & "."
    Else
        Wscript.Echo strCmd & " Could not be started. Error: " & Return & "."
    End If