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!
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 toplink
and see if you get an out-right failure versus a hang. That ought to start you down the right path, at least.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.
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.