Unable to remotely execute a batch file containing redirection/timeouts using PS

Welcome Forums General PowerShell Q&A Unable to remotely execute a batch file containing redirection/timeouts using PS

This topic contains 9 replies, has 5 voices, and was last updated by

 
Participant
2 months, 3 weeks ago.

  • Author
    Posts
  • #110189

    Participant
    Points: 0
    Rank: Member

    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

  • #110201

    Participant
    Points: 165
    Helping Hand
    Rank: Participant
    • #110429

      Participant
      Points: 0
      Rank: Member

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

  • #110210

    Participant
    Points: 166
    Helping Hand
    Rank: Participant

    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

    • #110291

      Participant
      Points: 0
      Rank: Member

      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?

    • #110500

      Participant
      Points: 0
      Rank: Member

      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

  • #110294

    Participant
    Points: 606
    Helping Hand
    Rank: Major Contributor

    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.

  • #110308

    Participant
    Points: 166
    Helping Hand
    Rank: Participant

    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
    
  • #110534

    Participant
    Points: 0
    Rank: Member

    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"
    
    }
    
    
  • #110551

    Participant
    Points: 166
    Helping Hand
    Rank: Participant

    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
    

You must be logged in to reply to this topic.