Solved

Run executable output over multiple servers

Posted on 2013-01-29
16
387 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
  • 5
  • 3
  • 2
  • +1
16 Comments
 
LVL 42

Assisted Solution

by:sedgwick
sedgwick earned 167 total points
Comment Utility
0
 

Author Comment

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

Expert Comment

by:sedgwick
Comment Utility
it works on Windows Server 2008 R2,Windows Server 2008 and Windows Server 2003.
which servers you use?
0
 

Author Comment

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

Author Comment

by:janhoedt
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:janhoedt
Comment Utility
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
Comment Utility
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 68

Assisted Solution

by:Qlemo
Qlemo earned 166 total points
Comment Utility
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
Comment Utility
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 68

Expert Comment

by:Qlemo
Comment Utility
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 68

Expert Comment

by:Qlemo
Comment Utility
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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Learn about cloud computing and its benefits for small business owners.
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
To efficiently enable the rotation of USB drives for backups, storage pools need to be created. This way no matter which USB drive is installed, the backups will successfully write without any administrative intervention. Multiple USB devices need t…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

744 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now