2
我努力做到以下幾點: -重啓後運行管理員聯絡模式下的批處理文件
- 運行腳本
- 重啓
- 運行一些腳本塊自動
- 重啓
- 而等等。
我發現了下面的代碼片段,它可以幫助我實現它。
# 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
而且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
}
}
究竟哪裏這個變化是進行,我基本上是新的這個PowerShell的世界,所以請幫助我!>''powershell -c $ dropperscript'npause「|出文件$ bootbatch -Encoding'OEM「 是要被替換爲」開始處理PowerShell的-Verb的runAs(緊接着做什麼) –
它拋出一個管理員的提示,然後開始爲powershell.But該批次的管理實體在啓動時運行的文件沒有提升權限 –
因此我說你需要禁用管理提示。 –