权限问题?Putty + Plink + Pageant + Powershell + 计划任务

权限问题?Putty + Plink + Pageant + Powershell + 计划任务

背景

我有以下组件:

  • Windows Server 2008 R2 盒子
  • 管理员帐户(我的)
  • 域中的服务帐户是该框的管理员
  • Putty 的安装(包括用于命令行的 plink 和用于 ssh 公钥生成的 Pageant)
  • SSH 会话期间要使用的命令的文本文件
  • 运行 plink 的 powershell 脚本,该脚本使用 Start-Process 通过命令行调用 plink,该命令行告诉它使用 Ageant 进行 PuTTy 连接并使用 command.txt 执行命令。
  • 以我的用户帐户运行的计划任务

目标

最终,让计划任务以服务账户运行,以服务账户调用powershell,运行进程并执行ssh命令。

问题

  • 当我以我的用户帐户从 powershell 命令提示符运行此脚本时,它就会运行。
  • 当我以我的用户帐户的身份从计划任务中以最高权限运行此脚本并告诉它保存我的凭据时,它“无法运行”。
  • “无法运行”的意思是它似乎产生了 plink.exe 的一个实例,我认为问题就发生在这里(某些挂起的东西我在控制台上看不到)。

问题

  • 有人有一个更优雅但使用相同或类似工具的整体解决方案吗?
  • 我的 powershell 脚本是否应该调用 cmd.exe 以便我至少可以将 stdout 捕获到我的日志文件中?

剧本

如果有帮助的话:

#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

有什么想法吗?我可能会尝试使用 Invoke-Command,看看是否至少可以将输出重定向到我的日志中。

谢谢!

答案1

我见过的“挂起”的一般原因plink.exe是提示将主机密钥添加到注册表中。不过,我认为主机密钥已经在您的注册表中了。我会添加参数-batchplink看看您是否会遇到彻底失败而不是挂起。这至少应该能让您走上正确的道路。

答案2

你的普林克呼叫正在使用-代理人(pageant)进行身份验证,但您尚未明确启动该服务,因此这可能是问题所在,请将您的私钥导出到无密码密钥文件,然后直接使用-我(身份)参数,看看是否是身份验证出了问题。还可以临时添加一个-v(详细)选项,然后检查事件日志。

答案3

不管怎么说,我遇到了一个非常类似的问题 - psftp 无法在 Windows 7 中计划运行的脚本中工作,无论用户是否登录。就我而言,我使用的是命令行上引用的 Pageant 保存的会话;由于 Pageant 不在与计划任务相同的上下文中运行,因此它会挂起。我通过在 psftp 中使用 -i 标志并在脚本本身中引用私钥的位置解决了该问题。

相关内容