2017-05-04 80 views
2

我努力做到以下幾點: -重啓後運行管理員聯絡模式下的批處理文件

  1. 運行腳本
  2. 重啓
  3. 運行一些腳本塊自動
  4. 重啓
  5. 而等等。

我發現了下面的代碼片段,它可以幫助我實現它。

# 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 

但重新啓動後,批處理文件在正常模式下運行(而不是在管理員模式),並引發拒絕訪問錯誤如下

Error Message

請幫我運行批處理的管理員重啓後。

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 
    } 
} 

回答

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 
+0

究竟哪裏這個變化是進行,我基本上是新的這個PowerShell的世界,所以請幫助我!>''powershell -c $ dropperscript'npause「|出文件$ bootbatch -Encoding'OEM「 是要被替換爲」開始處理PowerShell的-Verb的runAs(緊接着做什麼) –

+0

它拋出一個管理員的提示,然後開始爲powershell.But該批次的管理實體在啓動時運行的文件沒有提升權限 –

+0

因此我說你需要禁用管理提示。 –

相關問題