Solved

Run executable output over multiple servers

Posted on 2013-01-29
16
398 Views
Last Modified: 2013-02-28
Hi,

I’d need to run an executable on 30 servers. This executable generates output to C:\WINDOWS\report\report.ZIP.
I’d need to collect this data centrally.

Please advise howto do this, preferably via powershell (remote servers are windows 2008/2003).

J.
0
Comment
Question by:janhoedt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +1
16 Comments
 
LVL 42

Assisted Solution

by:sedgwick
sedgwick earned 167 total points
ID: 38831316
0
 

Author Comment

by:janhoedt
ID: 38831339
Thanks!
The problem is that some servers are 64 BIT, some are 32 BIT.
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 38831381
it works on Windows Server 2008 R2,Windows Server 2008 and Windows Server 2003.
which servers you use?
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

Author Comment

by:janhoedt
ID: 38832396
I use those os-es but I have 2 executables: 1 X64 and 1 x32.
0
 

Author Comment

by:janhoedt
ID: 38835178
I guess I could use x86 for both of them.

Please advise however on howto configure. I understand I have to add the executable between the brackets "c:\temp\myexecutable", but how do I set the target servers and how do I collect the output (c:\... on the target-servers?

function Run-RemoteCMD { 
 
    param( 
    [Parameter(Mandatory=$true,valuefrompipeline=$true)] 
    [string]$compname) 
    begin { 
        $command = Read-Host "c:\TEMP\myexecutable.exe" 
        [string]$cmd = "CMD.EXE /C " +$command 
                        } 
    process { 
        $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)" } 
                # if command is sucessfully invoked it doesn't mean that it did what its supposed to do 
                #it means that the command only sucessfully ran on the cmd.exe of the server 
                #syntax errors can occur due to user input  
     
     
     
     
    } 
    End{Write-Output "Script ...END"} 
                 } 

Open in new window

0
 

Author Comment

by:janhoedt
ID: 38835290
And then I'd need to do also a check if system is 64 bit or 32 bit and run correct executable.
0
 
LVL 37

Accepted Solution

by:
Neil Russell earned 167 total points
ID: 38836958
Try the following.

function Run-RemoteCMD { 
 
    param( 
    [Parameter(Mandatory=$true,valuefrompipeline=$true)] 
    [string]$compname) 
    begin { 
        $command = "\\mydomain.local\share\scripttorun.exe" 
        [string]$cmd = "CMD.EXE /C " +$command 
                        } 
    process { f
        $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)" } 
                # if command is sucessfully invoked it doesn't mean that it did what its supposed to do 
                #it means that the command only sucessfully ran on the cmd.exe of the server 
                #syntax errors can occur due to user input  
     
     
     
     
    } 
    End{Write-Output "Script ...END"} 
                 } 
     
	 Run-RemoteCMD myserver

Open in new window

0
 
LVL 70

Assisted Solution

by:Qlemo
Qlemo earned 166 total points
ID: 38848544
Were you able to run the script now for a single machine, so you got he basics of how to use the sample script?
To adapt it to your needs, and make it more generic, I recommend following modifications:
function Run-ReportCMD { 
param( 
  [Parameter(Mandatory=$true, valuefrompipeline=$true)] 
    [string] $computername
)
begin {
  $command = "C:\Program Files\Manufacturer\GenReportZIP"
}
process {
  $newproc = Invoke-WmiMethod -ComputerName $computername -class Win32_process -name Create -ArgumentList $command
  if ($newproc.ReturnValue -eq 0 ) 
  {
    Write-Host " Command invoked Sucessfully on $computername, getting report file"
    Move-Item "\\$computername\admin`$\report\report.zip" "C:\Windows\Temp\report.$computername.zip"
  }
} 
End {
  Write-Host "Script ...END"
} 
} 
     
"Server1", "Server2" | Run-ReportCMD
# or, with a list of servers stored in a file "C:\Serverlist.txt":
get-content "C:\Serverlist.txt" | Run-ReportCMD

Open in new window

This should also take care of x64/x86, as "Program Files" is the folder containing the appropriate exe.
0
 

Author Comment

by:janhoedt
ID: 38849184
Thanks a lot, but sorry, apperently I have a lot to catch up on scripting. So this is Powershell, right?

Could you comment the lines what it is actually doing?

So this script runs an executable from a central share (or do I need to copy it locally), depending on the CPU architecture (86 or 64 the 86 or 64 exectuable) to a list of servers (windows 2003 and 2008) then collects its output on those servers c:\window\outputfolder to a central share?

So I need to save it as script.ps1 and then run? Or better I should run it from a powershelleditor like powergui?
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 38849517
Yes, we are talking 'bout PowerShell here. The code needs to go into a *.ps1 file hence, and called by typing the filename (including absolute or relative path) at the PS prompt.
Or pasted into a script tab of PowerGUI, and run there. That is basically the same, but if you did not save your script, it will be saved with an temporary name.

I assumed the tool to run is installed/located in the local "Program Files" folder of each machine. Using a network share introduces additional pitfalls - the WMI service needs to have access to the network, and some commands including cmd.exe batch files require a mapped drive. Best to just try it out, if you need to run the executable from a network share. But then you would call the x86 one, as determining whether x64 would have to be used is not worth the effort if the tool runs as x86 without issues.
The command to run is in line 7.

Lines 22 and 24 show how to run the command against more than one machine. The result file will be located in C:\Windows\Temp, with the computer name as part of the file name.
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 38870900
janhoedt,

Please explain why you accepted http:#a38836958 (only).

The basic script was brought in by sedgwick in http:#a38831316.
Neilsr showed how you call the function defined in the script in http:#a38836958.
I've improved the script to do what you requested, including moving the generated file, and running it against more than one machine, in http:#a38848544.

All those posts should receive points, not only one (which isn't even the final solution). Remember that you can split points when closing a question.
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question