我有一个运行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并右键单击并选择“以不同用户身份运行”
然后,对于用户名,我输入“Administrator”,然后输入密码。
然后我执行以下
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
答案 0 :(得分:1)
我写这个作为答案,因为评论没有传达有限格式化所需的内容。
在Host_server系统上显示您所描述的命令提示符。
输入以下命令:
d:\pstools\psexec.exe \\remote_IP -u username -p password -accepteula cmd
屏幕应显示以下内容(因为我在Windows 7 Enterprise上运行此版本,Windows版本会有所不同,您将在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正常执行和关闭。