gpt4 book ai didi

powershell - 重新启动后,以管理员模式运行批处理文件

转载 作者:行者123 更新时间:2023-12-03 00:13:17 25 4
gpt4 key购买 nike

我正在尝试执行以下操作:

  • 运行脚本
  • 重新启动
  • 自动运行某些脚本块
  • 重新启动
  • 等等。

  • 我发现了以下代码片段,可以帮助我实现它。
    # 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
    }
    }

    最佳答案

    您需要将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
    }

    要同时以Admin身份运行批处理文件,您需要像这样调用它
    start-Process $bootbatch -Verb runas

    甚至没有变量
    start-Process "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\dropper.bat" -Verb runas

    关于powershell - 重新启动后,以管理员模式运行批处理文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43775895/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com