Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Background

I have the following components:

  • A windows server 2008 R2 box
  • An administrator account (mine)
  • A service account on the domain that is an Administrator of the box
  • An installation of Putty (including plink for command line and Pageant as the ssh public key generation)
  • A text file for commands to be used during an SSH session
  • A powershell script that runs plink which uses Start-Process to invoke plink with command lines that tell it to use Ageant for the PuTTy connection and commands.txt for the command.
  • A scheduled task that runs as my user account

The Goal

Eventually, to get the scheduled task to run as a service account, which calls powershell as the service account, which runs the process and carries out the ssh commands.

The Problem

  • When I run this script from a powershell command prompt as my user account, it runs.
  • When I run this script from a scheduled task as my user account, with the highest permissions, and tell it to save my credentials, it "doesn't run".
  • By "doesn't run", it mean it appears to spawn an instance of plink.exe, which is where I think the problem is occurring (something hanging that I can't see on the console).

Questions

  • Anybody have an overall-solution that's a little more elegant but uses the same or similar tools?
  • Should my powershell script instead invoke cmd.exe so that I can capture the stdout into my log file at least?

The Script

In case it helps:

#Starts the vcenter2 server.
#if the server is already started, it will tell you so.

#Stop an error from occurring when a transcript is already stopped
$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null

#Reset the error level before starting the transcript
$ErrorActionPreference="Continue"
Start-Transcript -path C:\Scripts\logs\StartTheVCenter2Server.log -append

#Run plink and reference the commands file to start up the server
Start-Process "C:\Program Files (x86)\PuTTY\plink.exe" -Argumentlist "-agent -m C:\Scripts\idrac_powerup_commands.txt root@[servernameredacted]" -wait -RedirectStandardOutput "C:\Scripts\logs\plinklog.log"

#Clean-up
Stop-Transcript

Thoughts? I'll likely play around with Invoke-Command to see if I can get it redirect the output into my log at least.

Thanks!

share|improve this question

3 Answers 3

up vote 4 down vote accepted

The general cause for plink.exe "hanging" that I've seen are prompts to add the host key to the registry. I'd think the host key already be in your registry, though. I'd add the -batch argument to plink and see if you get an out-right failure versus a hang. That ought to start you down the right path, at least.

share|improve this answer
    
Ahh, a GREAT point. I added the host key as my user account but it wouldn't have been added as the server account. I'll use PsExec to run the command prompt as my service account and add the host key that way to ensure it's not the issue. I'll report back; thanks for the tip –  SeanKilleen Feb 7 '13 at 21:00
    
Interestingly enough, adding the -batch argument appeared to do better than tell me why it failed -- it actually appears to have made it start working! I see the output I expect to see in the log file. After some more testing I'm going to confirm this as the answer. –  SeanKilleen Feb 8 '13 at 13:21
    
It must have been hanging at a prompt, then. Hopefully everything checks out w/ it. Glad I could help. –  Evan Anderson Feb 8 '13 at 13:22
    
I believe you're correct. the -batch command also later allowed me to prove that my service account hadn't cached the key yet (which I did fix with PsExec). Thank you! –  SeanKilleen Feb 8 '13 at 13:46

Your Plink call is using -agent (pageant) for authentication, but you haven't explicitly started the service, so that may be the issue, export your private key to a password less key file and reference it directly with a -i (identity) argument, to see if it's the authentication that's at fault. Also temporarily add a -v (verbose) option, and check the event log after.

share|improve this answer
    
Hi arober, thanks for responding. using -v -i -noageant and a passwordless private key produced same results -- works perfectly from the console, works perfectly from command prompt calling powershell.exe C:\Path\To\Script.ps1 -- and hangs from a scheduled task. I don't see any output in the event log even with the -v flag. –  SeanKilleen Feb 8 '13 at 13:16

For what its worth, I had a very similar issue - psftp wouldn't work in a script that was scheduled in windows 7 to run whether user was logged in or not. In my case, I was using a pageant saved session referenced on the command line; and since the pageant is not running in the same context as the scheduled task, it would hang. I solved the problem by using the -i flag in psftp and referencing the location of the private key in the script itself.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.