Remotely execute cmd.exe commands on multiple computers

This script invokes whatever command you can use in cmd.exe on one or more computers remotely like {del , netstart , ipconfig /flushdns}

4.2 Star
Add to favorites
Operating System
E-mail Twitter Digg Facebook
  • Script Error
    2 Posts | Last post August 24, 2014
    • I keep getting this error:
      Missing closing ')' in expression.
      At C:\Scripts\Flush-Register-DNS-2.ps1:5 char:3
      + 		 <<<< [string]$HostName) 
          + CategoryInfo          : ParserError: (CloseParenToken:TokenId) [], ParentContainsErrorRecordException
          + FullyQualifiedErrorId : MissingEndParenthesisInExpression
      Here is my script it appears to be using the syntax from your base code:
      function Run-RemoteCMD { 
      get-content "C:\scripts\prod\dnschangelist7-20-14.txt" | Run-RemoteCMD 
              begin { 
                  $command = Read-Host "ipconfig /flushdns && ipconfig /registerdns" 
                  [string]$cmd = "CMD.EXE /C " +$command 
              process { 
                  $newproc = Invoke-WmiMethod -class Win32_process -name Create `
                      -ArgumentList ($cmd) -ComputerName $HostName 
                  if ($newproc.ReturnValue -eq 0 ) 
                          { Write-Output " Command $($command) invoked Successfully `
                              on $($HostName)" }             
    • Was getting an error but finally figured out you need to import this generically as a module then it is a custom CMDlet- Runs very nicely just recycled DNS cache on a bunch of servers in a text file. For anyone who needs this info just copy the code to a file (name it Run-RemoteCMD.ps1) and throw it in your "My Documents" Powershell Modules folder (in a subfolder name it Run-RemoteCMD). The import it by running: Import-Module Run-RemoteCMD.ps1
      Then you can run it as a single line as in the examples (I used the get-content one to pull from a text file one server per line)
  • I am getting an issue whicle running the function. Can you please help me in resolving the issue.
    1 Posts | Last post November 12, 2013
    • PS C:\Windows\system32> function Run-RemoteCMD 
              $command = Read-Host " Enter command to run" 
              [string]$cmd = "CMD.EXE /C " +$command 
                  $newproc = Invoke-WmiMethod -class Win32_process -name Create -ArgumentList ($cmd) -ComputerName $compname 
                  if ($newproc.ReturnValue -eq 0 ) 
                          { Write-Output " Command $($command) invoked Sucessfully on $($compname)"}  
                  Write-Output "Script ...END"
      PS C:\Windows\system32> Run-RemoteCMD 
      cmdlet Run-RemoteCMD at command pipeline position 1
      Supply values for the following parameters:
      compname: KOTTIs
       Enter command to run: notepad.exe
      Invoke-WmiMethod : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
      At line:14 char:24
      +             $newproc = Invoke-WmiMethod -class Win32_process -name Create -Argum ...
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : InvalidOperation: (:) [Invoke-WmiMethod], COMException
          + FullyQualifiedErrorId : InvokeWMICOMException,Microsoft.PowerShell.Commands.InvokeWmiMethod
      Script ...END
  • How to run?
    1 Posts | Last post August 22, 2013
    • Hello Mohamed, I'm a beginner. I've downloaded your script, but don't know how to run and at which part i need to make changes. I've created file c:/server.txt with all the ip addresses of server and tried to run your script in command line. but i'm getting an error.
      could you please let me know, where to make changes and how to run?
      Many thanks in advance.
  • User & pwd
    2 Posts | Last post December 15, 2011
    • How can I enter user & password to get access to the remote computer?
    • Never mind, I already did it. Thanks
  • Script only works on Powershell v2.
    4 Posts | Last post April 12, 2011
    • Make sure you include that info somewhere.
    • sure 
      thank you
    • Just as an FYI in the comments if you choose to uncomment those bottom lines, there is a typo in the script the function is Run-RemoteCMD in the comments you have Run-Remotecommand.  Once you change those to the correct spelling of the function it works.  
      My issue with this script is, if I want to run an ipconfig on a specific computer I get no display on the remote machine, it shows that it was successfully run but the user won't see the display.  We have intermittent DNS issues, so it would be nice if we could target a computer and run an IP config and just have the user read us the information so we can remote in by IP when DNS is not cooperating.  Is there a way to modify the script to allow the window to open on the users screen?
    • What about using : ipconfig /all > \\yourPC\c$\temp\ip-output.txt ?
  • Script is not working
    12 Posts | Last post August 20, 2010
    • I created rem.ps1 script, using code in your article, then I am trying to start it in PowerShell and nothing happens. No metter if I mention remote server or not, no any messages or mistakes. 
      Could you please help me with it, because this script could be very useful in daily routine.
      Thank you!
    • Ok, i just added procedure call at the end of the code (Run-RemoteCMD) and now it works. 
    • where is the code for the function get-connect?
    • misant 
      welcome , glad i could help :)
    • sotodenty 
      i meant get-content ... it was typo error , i have fixed it now
      thanks :)
    • Friend,
      Is it possible to see the exact result. Ex:- if i am running a script from one server to execute the command "IPconfig /all" on remote system. i need to see the result here. is it possible? Bcoz i required to run a script for powerpath, where i can implement this there.
    • misant, what do you mean, you added a procdure call at the end of the code? I have the same problem as you had, there is simply nothing happening when I start the script.
    • what is meant by the procedure call is calling the function Run-RemoteCommand 
      you can do that by commenting out the parts at the end like Run-RemoteCommand server1
    • ganapathy , about extracting the result i don't know any easy way to do that , what i have in mind but haven't tried yet is intercepting the output of the process cmd.exe
      i will try to try that and feed you back
    • hi, Great script.... This will solve/Save us time! one thing though, how do you run this in a workgroup environment using get-credential parameter?
    • I'm sorry. I can get this to work at all. Whats the process for calling a powershell script? I saved it as a .ps1 file, put my "server.txt" in the same directory and in cmd navigated to that directory. What should I do next? Telling you what I did wrong, Im guessing, would be moot.
    • Hi Mohamed Garrana 
      Have you tried it, any luck? In VMWARE, invoke-vmscript we were able to see the output but in this command its little difficult...
      Please let me know if its possible?
      Also i need to join a remote machine to domain is it possible, i think this script should work am i correct?