Author Posts

August 28, 2018 at 9:16 pm

This is my PS script (intended to be run remotely on Computer2 from Computer1)

Start-Transcript -path \SomeFiler\Apps\Merrick\logs\max_allocation_output\$(get-date -Format yyyymmdd_hhmmtt).txt
$statistic = (Get-Process -Name alloc -ComputerName computer2).Count
Write-Host "Allocation Count is $statistic"
if ($statistic -gt 1){
(Taskkill /S computer2 /im alloc.exe /f)
Start-Process C:\ProgramData\Merrick\Allocations.bat -ComputerName computer2
}

With the above, everything works remotely (such as killing the "Alloc" processes remotely on Computer2) BUT – except for the very last line – which is to start the ".bat" file where I get the following error: "A Parameter cannot be found that matches parameter name 'ComputerName' (This tells me that Start-Process does not work remotely so I went with an Invoke-Command to see if that works better So I replaced the very last line with an Invoke-Command instead:
Invoke-Command -ComputerName "Computer2" -ScriptBlock {Invoke-Expression -Command:"cmd.exe /c 'C:\ProgramData\Merrick\Allocations.bat'"}
Then I get the error that "Input Redirection is not supported." and I see the batch file trying to execute but with each break, it just fails.
Below is the content of the actual batch file I have to run remotely on Computer2 from Computer1 within the powershell file above:

c: cd \ProgramData\Merrick\ timeout /t 30 /nobreak start /D "D:\ProCount\Allocations" alloc.exe auto2 timeout /t 30 /nobreak start /D "D:\ProCount\Allocations" alloc.exe pumper timeout /t 30 /nobreak start /D "D:\ProCount\Allocations" alloc.exe request1 timeout /t 30 /nobreak start /D "D:\ProCount\Allocations" alloc.exe request2 timeout /t 30 /nobreak start /D "D:\ProCount\Allocations" alloc.exe request3 timeout /t 30 /nobreak start /D "D:\ProCount\Allocations" alloc.exe request4 timeout /t 30 /nobreak start /D "D:\ProCount\Allocations" alloc.exe request5 timeout /t 30 /nobreak start /D "D:\ProCount\Allocations" alloc.exe request6 timeout /t 30 /nobreak start /D "D:\ProCount\Allocations" alloc.exe request7 timeout /t 30 /nobreak start /D "D:\ProCount\Allocations" alloc.exe request8 exit

so the problem is not with the powerhsell file, or the batch file, the problem is trying to figure out how to run a PS file on Computer1 which contains a batch file to run remotely on Computer2 with all these timeout redirections.
Note: I can run a simple PS script (Start-Process C:\ProgramData\Merrick\Allocations.bat) locally on the actual server Computer 2 – and CMD starts up and everything is gravy! But I need to be able to run this remotely from a tool like OpenBatch or a staging server's task scheduler..etc.
Please advise, thanks

August 28, 2018 at 11:53 pm

You are using Powershell, command line tools, and a DOS batch file...yikes. I would say that you could make your life much easier using a full Powershell solution versus messing with a batch file. My .02

August 29, 2018 at 5:59 pm

Hi Rob, can you please elaborate on using a full Powershell solution versus messing with a batch file? Do you mean, writing the vendors batch file as a PS script instead or *.bat?

August 29, 2018 at 6:15 pm

Yes and a big example here would be,

Invoke-Command -ComputerName "Computer2" -FilePath c:\YourNewScript.ps1

Where you can have the script(c:\YourNewScript.ps1) in the local machine itself and PowerShell will take care of executing the script in remote machine.

August 29, 2018 at 7:09 pm

Well, first we'd have to see what is the batch file. My assumption is the batch is just starting an exe with some params,so you can just skip the batch and do that directly with Start-Process. But even what it posted is doing a Get-Process and the using taskkill.exe to stop the process versus Stop-Process. It can be as simple as:

Get-Process -Name Notepad -ComputerName Computer2 | Stop-Process

August 29, 2018 at 11:02 pm

Thanks, that was my question there as well, I posted in in both powershell and technet forums.

August 30, 2018 at 1:54 pm

Hi Rob, I did include the content in my original question, but here it is again

 
Below is the content of the actual batch file I have to run remotely on Computer2 from Computer1 within the powershell file above:
c: cd \ProgramData\Merrick\ timeout /t 30 /nobreak start /D "D:\ProCount\Allocations" alloc.exe auto2 timeout /t 30 /nobreak start /D "D:\ProCount\Allocations" alloc.exe pumper timeout /t 30 /nobreak start /D "D:\ProCount\Allocations" alloc.exe request1 timeout /t 30 /nobreak start /D "D:\ProCount\Allocations" alloc.exe request2 timeout /t 30 /nobreak start /D "D:\ProCount\Allocations" alloc.exe request3 timeout /t 30 /nobreak start /D "D:\ProCount\Allocations" alloc.exe request4 timeout /t 30 /nobreak start /D "D:\ProCount\Allocations" alloc.exe request5 timeout /t 30 /nobreak start /D "D:\ProCount\Allocations" alloc.exe request6 timeout /t 30 /nobreak start /D "D:\ProCount\Allocations" alloc.exe request7 timeout /t 30 /nobreak start /D "D:\ProCount\Allocations" alloc.exe request8 exit

August 30, 2018 at 4:58 pm

you can download PSEXec tool, here is what I have done in the past.
1. Create a Batch folder in the Temp C:\temp\Batch
2. Copy PSExec.exe and the batch file to c:\temp\Batch
3. Copy this path to all computers :

foreach ($Computer in $ComputerName){

if (!(Test-Path "\\$Computer\c$\Temp\Batch")){
New-Item -path "\\$($Computer)\c$\Temp\Batch" -ItemType "directory" -Force
}
Copy-Item "\\YourPC\Temp\Batch\*.*" -Recurse -Destination "\\$Computer\c$\Temp\Batch" -Force
Start-Sleep -s 2

C:\Temp\Batch\PsExec.exe -i -s -accepteula \\$computer "C:\Temp\Batch\batch.cmd"

}

August 30, 2018 at 6:36 pm

The batch formatted should look like this:

c: 
cd \ProgramData\Merrick\ 
timeout /t 30 /nobreak 
start /D "D:\ProCount\Allocations" alloc.exe auto2 
timeout /t 30 /nobreak 
start /D "D:\ProCount\Allocations" alloc.exe pumper 
timeout /t 30 /nobreak
start /D "D:\ProCount\Allocations" alloc.exe request1 
timeout /t 30 /nobreak
start /D "D:\ProCount\Allocations" alloc.exe request2 
timeout /t 30 /nobreak 
start /D "D:\ProCount\Allocations" alloc.exe request3 
timeout /t 30 /nobreak 
start /D "D:\ProCount\Allocations" alloc.exe request4 
timeout /t 30 /nobreak 
start /D "D:\ProCount\Allocations" alloc.exe request5 
timeout /t 30 /nobreak 
start /D "D:\ProCount\Allocations" alloc.exe request6 
timeout /t 30 /nobreak 
start /D "D:\ProCount\Allocations" alloc.exe request7 
timeout /t 30 /nobreak 
start /D "D:\ProCount\Allocations" alloc.exe request8 
exit

This is basically the batch file written in Powershell.

$switches = 'auto2','pumper'
$switches += 1..8 | foreach{ 'request{0}' -f $_}

Set-Location 'C:\ProgramData\Merrick'

foreach ($switch in $switches) {
    'D:\ProCount\Allocations\alloc.exe {0}' -f $switch #example of command being called
    Start-Process -FilePath 'D:\ProCount\Allocations\alloc.exe' -ArgumentList $switch -Wait
    Start-Sleep -Seconds 30
}

Basically it will set the working directory and the execute each command with 30 second timeouts:

D:\ProCount\Allocations\alloc.exe auto2
D:\ProCount\Allocations\alloc.exe pumper
D:\ProCount\Allocations\alloc.exe request1
D:\ProCount\Allocations\alloc.exe request2
D:\ProCount\Allocations\alloc.exe request3
D:\ProCount\Allocations\alloc.exe request4
D:\ProCount\Allocations\alloc.exe request5
D:\ProCount\Allocations\alloc.exe request6
D:\ProCount\Allocations\alloc.exe request7
D:\ProCount\Allocations\alloc.exe request8