Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Run executable output over multiple servers

Posted on 2013-01-29
16
Medium Priority
?
413 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 668 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
Looking for the Wi-Fi vendor that's right for you?

We know how difficult it can be to evaluate Wi-Fi vendors, so we created this helpful Wi-Fi Buyer's Guide to help you find the Wi-Fi vendor that's right for your business! Download the guide and get started on our checklist today!

 

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 668 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 71

Assisted Solution

by:Qlemo
Qlemo earned 664 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 71

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 71

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

730 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