Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Run executable output over multiple servers

Posted on 2013-01-29
16
Medium Priority
?
422 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
11 Comments
 
LVL 42

Assisted Solution

by:Meir Rivkin
Meir Rivkin 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:Meir Rivkin
ID: 38831381
it works on Windows Server 2008 R2,Windows Server 2008 and Windows Server 2003.
which servers you use?
0
Lessons on Wi-Fi & Recommendations on KRACK

Simplicity and security can be a difficult  balance for any business to tackle. Join us on December 6th for a look at your company's biggest security gap. We will also address the most recent attack, "KRACK" and provide recommendations on how to secure your Wi-Fi network 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
The viewer will learn how to dynamically set the form action using jQuery.
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 …
Suggested Courses

876 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