【发布时间】: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