3

I'm trying to run a batch file on a remote server via a drive mapping as follows, but the process hangs...

Enable-WSManCredSSP -Role Client -DelegateComputer someserver -Force

$credential = Get-Credential -Credential domain\user (then I supply the password in the popup)

$RemoteSession = New-PSSession -ComputerName someserver -Credential $credential -Authentication Credssp

Invoke-Command -Session $RemoteSession -ScriptBlock { Enable-WSManCredSSP -Role Server -Force }

Invoke-Command -Session $RemoteSession -ScriptBlock { New-PSDrive -Name I -PSProvider FileSystem -Root \\server\share$ }

Everything seems fine up to this point and I can 'dir' the I drive and see the expected content.

When I execute the following, the process hangs -

Invoke-Command -Session $RemoteSession -ScriptBlock { Start-Process I:\temp.bat }

The temp.bat file executes the following command and I've verified manually that it works

echo Scott was here > C:\temp.txt

However, the command runs for over 5 minutes without any response.

Can anyone help? What have I done wrong?

  • Since Start-Process does not wait for the process to finish I don't see why it would. Have you tried replacing the batch file with powershell commands? – Lars Truijens Dec 30 '13 at 15:34
  • Hi Lars, I havn't, no. The actual file I need to run is a third party batch file, so I'm not sure there's much point. I initially assumed it was this file that was at fault, which is why I changed the call to an redirected echo statement (as above). – scottwilson_uk Dec 30 '13 at 22:01
  • Lars, have you seen anything in the past that could explain this? Help much appreciated... – scottwilson_uk Dec 30 '13 at 22:10
  • Might be related to your redirection. Can you try removing the `> C:\temp.txt` in your batch, and appending `-RedirectStandardOutput C:\temp.txt` as a parameter to `Start-Process`? – Anthony Neace Dec 30 '13 at 22:13
  • Hi Anthony, I'll try that next week. The only thing is without the redirection, I don't know if it's worked! Any other ways of proving it? Thanks – scottwilson_uk Dec 31 '13 at 18:09
  • You don´t have to create output to see if your command worked. You can use `$lastexitcode` for this. You can find an example in this thread: http://stackoverflow.com/questions/8549184/how-to-capture-the-return-value-of-a-scriptblock-invoked-with-powershells-invok – Paul Jan 03 '14 at 15:18

4 Answers4

0

Although this is not a direct solution to your question, my suggestion from many weeks of banging my head against my keyboard is to just take a different path.

$taskName = whocares
$command = I:\Temp.bat
$serverName = theServer
$userName = domain\user
$pass = pl4inT3xtPassword
$date = (Get-Date).addMinutes(1)
$formats = $date.getDateTimeFormats()
$startTime = $formats[113]
schtasks.exe /create /sc ONCE /tn $taskName /tr $command /s $serverName /u $userName /p $pass /ru $userName /rp $pass /st $startTime

This will create a task, start the process in the next minute, and then remove itself from the scheduler. There is also a /z flag in there to remove after it runs, but if your like me and on XP then this probably won't be available to you. If you notice your task isnt being removed after it runs you can always wait a minute and then kill it... or you can get a little bit more fancy with it:

while(($timer -lt 60) -and (!$running)) {
    schtasks.exe /query /S $serverName /FO CSV /v /u $userName /p $pass | ConvertFrom-CSV | `
    % {if($_.Status -eq "Running") {
        Write-Host "IT WORKED!"
        Write-host $_
        $running = $true
      }
    }
    if(!$running) {
        $timer++
    }
    sleep 1
}
schtasks.exe /delete /s $serverName /u $userName /p $pass /tn $taskName /f

This bit just queries the task every second to check if its running... once it is it will delete the old scheduled task and your process will continue to run (until its finished).

The task scheduler is the only clean way I have found to start processes remotely with PowerShell. Instead of being a child process of your PSremote.exe, it will be under System. Also, unlike remote-session, if you X out of your powershell session, the process will persist on the remote computer.

I know it is a lot more than just your one liner, but I hope it is useful, and if you parameterize it or even build a windows forms application on top it will save you a lot of time and headache.

more on schtasks Here

Cole9350
  • 4,928
  • 2
  • 31
  • 49
0

Thanks for your suggestions.

Due to other issues (with the process that we're ultimately trying to run), we've put this down for the minute, without finding a solution

Thanks for your help and suggestions. Will hopefully pick this back up in a few weeks Scott

0

Try putting an -AsJob at the end of your Invoke-Command lines?

Michael Beck
  • 2,598
  • 3
  • 18
  • 21
0

As an FYI if Invoke-Command always hangs it may be the service or firewall:

  1. Try a simple command to system : Invoke-Command -ComputerName XXXXX -ScriptBlock { Get-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion }

  2. Start the Windows Remote Management Service (on that system)

  3. Check for the listening port:

     netstat -aon | findstr "5985"
          TCP    0.0.0.0:5985           0.0.0.0:0              LISTENING       4
          TCP    [::]:5985              [::]:0                 LISTENING       4
    
Mike Q
  • 5,006
  • 2
  • 41
  • 53