【问题标题】:Psexec runs batch file with no errors, but has no effectPsexec 运行批处理文件没有错误,但没有效果
【发布时间】:2014-09-03 22:37:53
【问题描述】:

我有一个批处理文件,它运行一个 powershell script_1,它使用提升的凭据调用另一个 powershell script_2。

当我登录到 remote_server(Win Server 2012 R2,在 WORKGROUP 中)并执行这个批处理脚本时,它运行良好。

当我登录到 host_server(Win Server 2012 R2,在 WORKGROUP 中)并使用 psexec 执行此批处理脚本时,它返回错误代码 0,但是当我测试这是否有效时,它就像 powershell script_1 或 script_2从未执行过

在 host_server 上,我将运行一个 cmd shell,Shift-右键单击并选择“以不同用户身份运行”

然后对于用户名,我输入“管理员”,然后输入密码。

然后我执行以下操作

D:\pstools\psexec.exe \\IP_of_remote_server -u username -p password -accepteula C:\share\enable.bat

它执行没有错误,并返回代码0

enable.bat

@echo off
powershell.exe C:\share\eLEVATE.ps1

eLEVATE.ps1

$startInfo = $NULL
$process = $NULL


<#Previously created password file in C:\share\cred.txt, read-host -assecurestring | convertfrom-securestring | out-file C:\share\cred.txt#>
$password = get-content C:\share\cred.txt | convertto-securestring

$startInfo = New-Object System.Diagnostics.ProcessStartInfo
$startInfo.FileName = "powershell.exe"
$startInfo.Arguments = "-noninteractive -windowstyle hidden -noprofile C:\share\remote_elevate.ps1 "

$startInfo.RedirectStandardOutput = $true
$startInfo.UseShellExecute = $false
$startInfo.CreateNoWindow = $false
$startInfo.Username = "Administrator"
$startInfo.Domain = "WORKGROUP"
$startInfo.Password = $password 

$process = New-Object System.Diagnostics.Process
$process.StartInfo = $startInfo
$process.Start() | Out-Null
$userId = $process.StandardOutput.ReadToEnd() 
$process.WaitForExit()

remote_elevate.ps1

enable-psremoting -force

我整个上午都在想办法解决这个问题。谢谢!

编辑

我正在发布我输入时遇到的错误的确切屏幕截图

d:\pstools\psexec.exe \\remote_IP -u Administrator -p password -accepteula cmd

然后执行

powershell.exe -executionpolicy bypass c:\share\eLEVATE.ps1

【问题讨论】:

  • PowerShell ExecutionPolicy 设置为什么?
  • @aphoria - 它是RemoteSigned
  • 您是否尝试过使用 PSExec 运行 CMD.exe 并查看当您尝试以这种方式运行 powershell.exe C:\share\eLEVATE.ps1 时显示的内容?也许您需要powershell.exe -executionpolicy bypass C:\share\eLEVATE.ps1 来绕过任何阻塞的执行策略。
  • @TheMadTechnician 我试过d:\pstools\psexec.exe \\remote_IP -u username -p password -accepteula cmd /c powershell.exe -executionpolicy bypass c:\share\eLEVATE.ps1 还是不行
  • 试试d:\pstools\psexec.exe \\remote_IP -u username -p password -accepteula cmd。这将在远程系统上打开一个 CMD 提示符。然后自己输入powershell.exe -executionpolicy bypass c:\share\eLEVATE.ps1 命令并按回车键,看看它是否给出错误或任何东西。完成后键入 Exit 以关闭远程 cmd 提示符并返回到本地提示符。

标签: windows powershell batch-file cmd


【解决方案1】:

我写这个作为答案,因为 cmets 没有传达有限格式所需的内容。

在 Host_server 系统上,按照您的描述打开命令提示符。

输入以下命令:

d:\pstools\psexec.exe \\remote_IP -u username -p password -accepteula cmd

屏幕应显示以下内容(Windows 版本会有所不同,因为我在 Windows 7 Enterprise 上运行此程序,而您将在 Server 2012 上执行此操作):

PsExec v1.98 - Execute processes remotely
Copyright (C) 2001-2010 Mark Russinovich
Sysinternals - www.sysinternals.com


Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Windows\system32>

这现在是 Remote_server 上的命令提示符。从此提示执行的任何操作都不会发生在本地 (host_server),但会发生在 remote_server 计算机上。在这个提示符下输入命令:

powershell.exe -executionpolicy bypass c:\share\eLEVATE.ps1

执行是否没有错误?它会做它应该做的事吗?

完成测试并准备关闭 remote_server 系统上的命令提示符后,使用以下命令:

exit

这将关闭远程命令提示符并将您返回到本地计算机 (host_server) 上的命令提示符。

PSExec 将声明返回码 0,因为 CMD.exe 正常关闭。这并不反映 PowerShell 返回的任何内容,仅反映远程系统上的 cmd.exe 正常执行和关闭。

【讨论】:

  • 我在 host_server 上遵循了这些步骤。当我执行 powershell.exe ...没有错误。当我输入 exit 并进入时,它显示 cmd exited on remote_server with error code 0. 但是,远程服务器上仍然禁用 PSRemoting
  • 那么您用来执行 PSExec 的用户是 Remote_Server 计算机上的管理员吗?
  • 用户名在 host_server 和 remote_server 上都具有管理员权限。并且管理员在 host_server 和 remote_server 上具有相同的密码。但是,当我在host_server上执行psexec.exe \\remote_IP -u Administrator -p password -accepteula cmd然后执行powershell.exe -executionpolicy bypass c:\share\eLEVATE.ps1时,出现了几个错误,即convertto-securestring : Key not valid for use in specified state.Exception calling "Start" with "0" argument(s): "The user name or password is incorrect" ...但是加密的密码文件是管理员的密码
  • 好的,所以你确实得到了错误。您之前说过您没有收到任何错误,除了帐户权限问题外,没有太多工作要做。好的,所以问题是它无法将密码字符串转换为安全字符串。不幸的是,我不熟悉这方面的基本过程,所以我现在可能无法提供进一步的帮助。
  • 查看Get-Help ConvertTo-SecureString -full,当您为密码加密字符串时,您可能需要设置一个密钥。文件中是否已经加密,还是纯文本格式?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-19
相关资源
最近更新 更多