我有一个PowerShell脚本,如下所示:
# Define time for report (default is 1 day)
$startDate = (get-date).AddDays(-10)
# Store successful logon events from security logs with the specified dates and workstation/IP in an array
# foreach ($DC in $DCs){
# $slogonevents = Get-Eventlog -LogName Security -ComputerName $DC.Hostname -after $startDate | where {$_.eventID -eq 4624 }
# }
$slogonevents = Get-Eventlog -LogName Security -after $startDate | where {$_.eventID -eq 4624 }
# Crawl through events; print all logon history with type, date/time, status, account name, computer and IP address if user logged on remotely
$(foreach ($e in $slogonevents){
# Logon Successful Events
# Local (Logon Type 2)
if (($e.EventID -eq 4624 ) -and ($e.ReplacementStrings[8] -eq 2)){
write-host "Type: Local Logon`tDate: "$e.TimeGenerated "`tStatus: Success`tUser: "$e.ReplacementStrings[5] "`tWorkstation: "$e.ReplacementStrings[11]
}
# Remote (Logon Type 10)
if (($e.EventID -eq 4624 ) -and ($e.ReplacementStrings[8] -eq 10)){
write-host "Type: Remote Logon`tDate: "$e.TimeGenerated "`tStatus: Success`tUser: "$e.ReplacementStrings[5] "`tWorkstation: "$e.ReplacementStrings[11] "`tIP Address: "$e.ReplacementStrings[18]
}
}) *>&1 > D:\Cyber_security\Python\test.txt
我想从python运行这个脚本。这个脚本保存在我的D drive.My python脚本中:
import subprocess, sys
p = subprocess.Popen(["powershell.exe",
"D:\Cyber_security\Python\login.ps1"],
stdout=sys.stdout)
p.communicate()
但不起作用。我需要作为管理员运行powershell,但我不知道如何运行。
发布于 2022-07-28 14:58:54
您需要嵌套powershell.exe
调用:
Start-Process
调用PowerShell的-Verb RunAs
cmdlet的外部调用,它允许运行任何具有海拔的可执行文件。.ps1
脚本,所以必须通过powershell.exe
( Windows PowerShell CLI )调用它,就像在您自己的尝试中一样,但您需要显式地合并一个Set-Location
调用,以确保脚本在相同的工作dir中运行。作为调用方(C:\Windows\System32
是PowerShell中使用Start-Process -Verb RunAs
时的缺省值)。- If you don't need this, or if you're using `pwsh.exe`, the CLI of the cross-platform [_PowerShell (Core) 7+_](https://github.com/PowerShell/PowerShell/blob/master/README.md) edition (which now retains the working dir. by default), the inner call can be simplified to:
- `powershell.exe -Args '-noprofile -file D:\Cyber_security\Python\login.ps1'`
# See bottom section if you also want to get the exit code of
# the elevated process.
p = subprocess.Popen(
[
"powershell.exe",
"-noprofile", "-c",
r"""
Start-Process -Verb RunAs -Wait powershell.exe -Args "
-noprofile -c Set-Location \`"$PWD\`"; & D:\Cyber_security\Python\login.ps1
"
"""
],
stdout=sys.stdout
)
p.communicate()
注意:
- involves an interactive UAC confirmation / credentials prompt that cannot be bypassed (unless UAC is turned off, which would be ill-advised)
- invariably runs in a _new window_.
- prevents _direct_ capture of the elevated process' output streams; you'll have to redirect to (temporary) _files_ instead, which you can do with `Start-Process`'
-RedirectStandardOutput
/ -RedirectStandardError
参数
-noprofile
和-c
:-noprofile
禁止加载PowerShell的配置文件,-c
(-Command
)显式地指示要执行的PowerShell命令。-Wait
被添加到上面的Start-Process
调用中,以便使外部powershell.exe
调用在继续之前等待提升的进程退出。powershell.exe
+ Start-Process -Verb RunAs
,但这是最方便的选择。- A Python-based solution is possible, but involves fairly complex use of the WinAPI - see [this blog post](https://yeahexp.com/how-to-run-a-subprocess-with-admin-permission/)
- Note that while you can technically use `runas.exe /user:Administrator` utility to create an elevated session, doing so (a) only works with precisely that account, i.e. the built-in account named `Adminstrator`, and that account is often disabled in practice (it is disabled by default).
.ps1
文件,以便按需自举(或使用帮助器.ps1
文件进行此操作)-请参阅这个答案。变体,它还获取提升进程的退出代码:
如果您的.ps1
脚本使用exit
语句来故意报告一个(进程)退出代码,该退出代码标志着成功还是失败,并且您希望查询退出代码,则需要做更多的工作:
Start-Process
-PassThru
开关输出一个表示新启动进程的进程信息对象,其.ExitCode
属性报告进程退出代码(终止后)。-c
/ -Command
CLI参数的工作方式,内部powershell.exe
调用必须显式地使用exit $LASTEXITCODE
将脚本的退出代码中继为提升的进程退出代码。p = subprocess.Popen(
[
"powershell.exe",
"-noprofile", "-c",
r"""
exit (
Start-Process -Verb RunAs -PassThru -Wait powershell.exe -Args "
-noprofile -c Set-Location \`"$PWD\`"; & C:\Users\jdoe\Desktop\pg\pg.ps1; exit `$LASTEXITCODE
"
).ExitCode
"""
],
stdout=sys.stdout
)
p.communicate()
print('Terminated with exit code ' + str(p.returncode))
https://stackoverflow.com/questions/73153170
复制相似问题