Solved

Run executable output over multiple servers

Posted on 2013-01-29
16
388 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
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
 

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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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 68

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 68

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 68

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

3 Use Cases for Connected Systems

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

Question has a verified solution.

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

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

920 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

13 Experts available now in Live!

Get 1:1 Help Now