Psexec运行的批处理文件没有错误,但没有任何影响

问题描述:

我有一个批处理文件,该文件运行powershell script_1,该文件使用提升的凭据调用另一个powershell script_2。Psexec运行的批处理文件没有错误,但没有任何影响

当我登录到远程服务器(Win Server 2012 R2,在WORKGROUP中)并执行此批处理脚本时,它可以很好地工作。

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

在host_server,我将运行CMD外壳,按住Shift键并单击鼠标右键,并选择用户名我在“管理员”输入“运行方式不同的用户”

enter image description here

然后,然后输入密码。

然后我执行以下

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

enter image description here

+0

设置为“ExecutionPolicy”的PowerShell是什么? – aphoria 2014-09-02 15:36:54

+0

@aphoria - 它是'RemoteSigned' – Glowie 2014-09-02 16:35:51

+0

您是否尝试过使用PSExec运行CMD.exe,并在尝试以这种方式运行'powershell.exe C:\ share \ eLEVATE.ps1'时看到它说什么?也许你需要'powershell.exe -executionpolicy绕过C:\ share \ eLEVATE.ps1'来绕过阻塞的任何执行策略。 – TheMadTechnician 2014-09-02 16:55:29

我正在写这个作为答案,因为评论并没有表达有限的格式需要什么。

在Host_server系统上调出您的命令提示符,如您所述。

输入以下命令:

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

的屏幕应该显示如下(Windows版本会有所不同,因为我跑这对Windows 7企业版,你会那样做服务器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执行并正常关闭。

+0

我在host_server上执行了这些步骤。当我执行powershell.exe ...没有错误。当我输入exit并输入时,它表示'cmd在remote_server上退出,错误代码为0.'但是,remote_server上的PSRemoting仍处于禁用状态 – Glowie 2014-09-02 19:01:57

+1

然后是您正在用于在Remote_Server计算机上执行PSExec管理员的用户吗? – TheMadTechnician 2014-09-02 19:03:52

+0

用户名在host_server和remote_server上都具有管理权限。管理员在host_server和remote_server上都有相同的密码。但是,当我在host_server上并执行'psexec。exe \\ remote_IP -u管理员-p密码-accepteula cmd'然后执行'powershell.exe -executionpolicy绕过c:\ share \ eLEVATE.ps1',我有几个错误,即'convertto-securestring:Key无效使用在指定的状态.'和'异常调用“开始”与“0”参数:“用户名或密码不正确”'...但加密的密码文件是管理员的密码 – Glowie 2014-09-02 19:31:51