bat cmd批处理命令获取管理员权限&重启、停用windows服务
获取管理员权限
//获取管理员权限 @echo off >nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system" if '%errorlevel%' NEQ '0' ( echo run as administrator... goto UACPrompt ) else ( goto gotAdmin ) :UACPrompt echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs" echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs" "%temp%\getadmin.vbs" exit /B :gotAdmin
重启服务(停止、启动Windows服务)
@title 重启服务 echo 停止、启动服务 net stop 服务名 net start 服务名 exit
停止服务并禁用服务
@title 停止并禁用服务 echo 停止并禁用服务 net stop "服务名" SC config "服务名" start= disabled echo (五秒后自动退出) ping /n 5 localhost>nul exit
BAT批处理一键禁用所有任务计划
@echo off & setlocal EnableDelayedExpansion set "cmdhead=schtasks /change /disable /tn " set "filelist=%TMP%\filelist.txt" set "filebat=%TMP%\myschtasks.bat" set "dir_tasks=C:\Windows\System32\Tasks" dir /a-d /b /s %dir_tasks% > %filelist% echo @echo off > %filebat% set j=0 for /f "delims=" %%i in (%filelist%) do ( set /a j+=1 set line!j!=%%i call set a=%%line!j!%% echo %cmdhead%"!a:~25!" >> %filebat% ) call %filebat% del /q /f %filelist% del /q /f %filebat% pause
实例: 使用 PowerShell 禁用更新:
打开 Windows PowerShell 直接复制以下脚本运行一下更加方便:
或者将脚本保存为 disable-edge-auto-update.ps1 文件,右键点击 “使用 PowerShell 运行” 即可快速完成。
if ([Environment]::Is64BitOperatingSystem -eq "True") { #Write-Host "64-bit OS" $PF=${env:ProgramFiles(x86)} } else { #Write-Host "32-bit OS" $PF=$env:ProgramFiles } if ($(Test-Path "$PF\Microsoft\Edge\Application\msedge.exe") -eq "True") { # 结束进程 taskkill /im MicrosoftEdgeUpdate.exe /f taskkill /im msedge.exe /f # Microsoft Edge 更新服务 (sysin) #这里也可以使用 sc.exe stop "service name" Stop-Service -Name "edgeupdate" Stop-Service -Name "edgeupdatem" Stop-Service -Name "MicrosoftEdgeElevationService" # Windows 10 默认 PS 版本 5.1 没有 Remove-Service 命令 # This cmdlet was added in PS v6. See https://docs.microsoft.com/en-us/powershell/scripting/whats-new/what-s-new-in-powershell-core-60?view=powershell-6#cmdlet-updates. #Remove-Service -Name "edgeupdate" #Remove-Service -Name "edgeupdatem" #Remove-Service -Name "MicrosoftEdgeElevationService" # sc 在 PowerShell 中是 Set-Content 别名,所以要使用 sc.exe 否则执行后无任何效果 sc.exe delete "edgeupdate" sc.exe delete "edgeupdatem" sc.exe delete "MicrosoftEdgeElevationService" # 任务计划企业版 #schtasks.exe /Delete /TN \MicrosoftEdgeUpdateBrowserReplacementTask /F #schtasks.exe /Delete /TN \MicrosoftEdgeUpdateTaskMachineCore /F #schtasks.exe /Delete /TN \MicrosoftEdgeUpdateTaskMachineUA /F Get-ScheduledTask -taskname MicrosoftEdgeUpdate* | Unregister-ScheduledTask -Confirm: $false # 移除更新程序 Remove-Item "$PF\Microsoft\EdgeUpdate" -Recurse -Force Write-Output "Disable Microsoft Edge Enterprise Auto Update Successful!" } elseif ($(Test-Path "$env:USERPROFILE\AppData\Local\Microsoft\Edge\Application\msedge.exe") -eq "True") { # 结束进程 taskkill /im MicrosoftEdgeUpdate.exe /f taskkill /im msedge.exe /f # 用户版没有创建服务 # 获取SID方法 function Get-CurrentUserSID { [CmdletBinding()] param( ) Add-Type -AssemblyName System.DirectoryServices.AccountManagement return ([System.DirectoryServices.AccountManagement.UserPrincipal]::Current).SID.Value } # 用户版任务计划 schtasks.exe /Delete /TN \MicrosoftEdgeUpdateTaskUser$(Get-CurrentUserSID)Core /F schtasks.exe /Delete /TN \MicrosoftEdgeUpdateTaskUser$(Get-CurrentUserSID)UA /F #Get-ScheduledTask -taskname MicrosoftEdgeUpdate* | Unregister-ScheduledTask -Confirm: $false # 移除更新程序 Remove-Item "$env:USERPROFILE\AppData\Local\Microsoft\EdgeUpdate" -Recurse -Force Write-Output "Disable Microsoft Edge Users Setup Auto Update Successful!" } else { Write-Output "No Microsoft Edge Installation Detected!" }
··
人生只若初见................