我正在尝试执行以下操作: -
我发现了以下代码片段,它可以帮助我实现它。
# Temp Folder
if (!(Get-Item d:\temp -ea ignore)) { mkdir d:\temp }
$dropperscript = 'C:\temp\dropper.ps1'
$dropper = @'
#############################################
### Configuration Variables ###
#
# Put any variables you'll use here
#
### ###
#############################################
# Static Variables
$countfile = 'd:\temp\bootcount.txt'
$bootbatch = 'C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\dropper.bat'
$dropperscript = 'd:\temp\dropper.ps1'
#################
##### Setup #####
# Bootstrap Batch
if (!(Get-Item $bootbatch -ea ignore)) {
"powershell -c $dropperscript`npause" | Out-File $bootbatch -Encoding 'OEM'
}
# Boot Count
if (Get-Item $countfile -ea ignore) {
[int]$bootcount = Get-Content $countfile
if ($bootcount -match "^\d{1,2}$") { ([int]$bootcount) ++ }
else { $bootcount = 1 }
}
else { $bootcount = 1 }
$bootcount | Out-File $countfile
switch ($bootcount) {
1 {
Get-Process | Out-File log1.txt
$x=Read-Host "Press Enter"
Restart-Computer -Force
##################################################
############### --REBOOT-- ###############
}
2 {
# Fill in anything needed on second reboot; remove if unneeded
Get-Process | Out-File log2.txt
$x=Read-Host "Press Enter"
Restart-Computer -Force
##################################################
############### --REBOOT-- ###############
}
3 {
# Fill in anything needed on third reboot; remove if unneeded
# Create more reboots as needed
$x=Read-Host "Press Enter"
Get-Process | Out-File log3.txt
Restart-Computer -Force
##################################################
############### --END-- ################
}
default {
# Dropper is complete; clean up
rm $countfile
rm $bootbatch
rm $dropperscript
}
}
'@
# Drop and run Dropper
$dropper | Out-File $dropperscript -Encoding 'OEM'
Invoke-Expression $dropperscript
但重新启动后,批处理文件以正常模式运行(而不是以管理员模式运行),并按如下方式抛出拒绝访问错误
请在重启后帮我在管理员中运行批处理。
dropper.bat
powershell -c d:\temp\dropper.ps1 pause
在temp中创建的Dropper.ps1如下
#############################################
### Configuration Variables ###
#
# Put any variables you'll use here
#
### ###
#############################################
# Static Variables
$countfile = 'd:\temp\bootcount.txt'
$bootbatch = 'C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\dropper.bat'
$dropperscript = 'd:\temp\dropper.ps1'
#################
##### Setup #####
# Bootstrap Batch
if (!(Get-Item $bootbatch -ea ignore)) {
"powershell -c $dropperscript`npause" | Out-File $bootbatch -Encoding 'OEM'
}
# Boot Count
if (Get-Item $countfile -ea ignore) {
[int]$bootcount = Get-Content $countfile
if ($bootcount -match "^\d{1,2}$") { ([int]$bootcount) ++ }
else { $bootcount = 1 }
}
else { $bootcount = 1 }
$bootcount | Out-File $countfile
switch ($bootcount) {
1 {
Get-Process | Out-File log1.txt
$x=Read-Host "Press Enter"
Restart-Computer -Force
##################################################
############### --REBOOT-- ###############
}
2 {
# Fill in anything needed on second reboot; remove if unneeded
Get-Process | Out-File log2.txt
$x=Read-Host "Press Enter"
Restart-Computer -Force
##################################################
############### --REBOOT-- ###############
}
3 {
# Fill in anything needed on third reboot; remove if unneeded
# Create more reboots as needed
$x=Read-Host "Press Enter"
Get-Process | Out-File log3.txt
Restart-Computer -Force
##################################################
############### --END-- ################
}
default {
# Dropper is complete; clean up
rm $countfile
rm $bootbatch
rm $dropperscript
}
}
答案 0 :(得分:0)
您需要将powershell脚本提升为"以管理员身份运行"
Start-Process powershell -Verb runAs
但是,您需要禁用提示以确认以管理员身份运行的请求。您可以找到该部分here
您只需在脚本的开头复制并粘贴以下内容即可。
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
Break
}
然后以管理员身份运行批处理文件,您需要像这样调用它
start-Process $bootbatch -Verb runas
甚至没有变量
start-Process "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\dropper.bat" -Verb runas