窄口牛 发表于 2017-7-4 16:18:38

wmic的环境在批处理里怎么退出?

调用它以后,怎么退出它的环境,却不影响批处理继续工作?

窄口牛 发表于 2017-7-4 16:41:24

echo.quit|wmic这种格式呢,有群友提示,我回去试试。

nttwqz 发表于 2017-7-4 17:43:12

这玩意儿在批处理里面还用单独退出?不退出有影响?

窄口牛 发表于 2017-7-4 18:18:20

嗯,后续的命令就会执行不了,比如字符串对比,就会提示获取字符串失败。

窄口牛 发表于 2017-7-5 09:16:37

有办法吗?

dos时代菜鸟 发表于 2017-7-8 17:36:31

批处理中,调用wmic 都是单一命令调用,自然退出。

dos时代菜鸟 发表于 2017-7-8 17:36:33

批处理中,调用wmic 都是单一命令调用,自然退出。

窄口牛 发表于 2017-7-8 18:45:40

但是后面的命令都执行失败了。

窄口牛 发表于 2017-7-8 18:54:59

@echo off
del list.log /q
dir *-kb*.msu /b /o:n >a.txt
wmic qfe get hotfixid >>1.tx
tfor /f "delims=^- tokens=1,*" %%a in (1.txt) do (echo %%a>>2.txt)
del 1.txt /q
for /f "delims=^_ tokens=1,*" %%a in (2.txt) do (echo %%a>>b.txt)
del 2.txt /q
findstr /V /I /G:b.txt a.txt > c.txt && move /y c.txt list.log
del *.txt
for /f "eol=o eol=O " %%j in (list.log) do echo 正在安装补丁%%j && start /wait wusa %%j /quiet /norestart

比如这个

nttwqz 发表于 2017-7-15 20:18:53

窄口牛 发表于 2017-7-8 18:54
比如这个

最近右臂摔了一下,骨裂了,回帖不便。

原因已经找到,WMIC输出的文本默认为unicode格式,而for默认是无法读取这种文本的,可以通过type 1.txt实现,或者将wmic结果用findstr处理一下再输出(建议只保留KB开头的行),这样输出的文本默认ansi格式,for可以正常读取。

窄口牛 发表于 2017-7-15 22:11:13

谢谢,好好养伤,愿早日康复。

窄口牛 发表于 2017-7-17 10:07:13

本帖最后由 窄口牛 于 2017-7-17 10:25 编辑

@echo off
del list.log /q
dir *-kb*.exe /b /o:n >7.txt
For %%a in (7.txt) Do Type %%a >a.txt
del 7.txt /q
wmic qfe get hotfixid >>8.txt
For %%a in (8.txt) Do Type %%a >6.txt
del 8.txt /q
findstr /i "kb" 6.txt>>1.txt
del 6.txt
::for /f "tokens=2" %%a in ('systeminfo^|findstr /i "kb"') do echo %%a>>1.txt
for /f "delims=^- tokens=1,*" %%a in (1.txt) do (echo %%a>>2.txt)
del 1.txt /q
for /f "delims=^_ tokens=1,*" %%a in (2.txt) do (echo %%a>>b.txt)
del 2.txt /q
findstr /V /I /G:b.txt a.txt > c.txt && move /y c.txt list.log
del *.txt
又凑了几句进去前部分没问题了
筛选(findstr /V /I /G:b.txt a.txt > c.txt && move /y c.txt list.log)又失效了

nttwqz 发表于 2017-7-17 14:01:59

窄口牛 发表于 2017-7-17 10:07
又凑了几句进去前部分没问题了
筛选(findstr /V /I /G:b.txt a.txt > c.txt && move /y c.txt list.log ...

@echo off
pushd %~dp0\
del list.log /q 2>nul
dir *-kb*.exe /b /o:n >a.txt
for /f "tokens=1 delims=-_" %%a in ('wmic qfe get hotfixid ^| findstr /i "^KB"') do echo %%a>>b.txt
findstr /V /I /G:b.txt a.txt > c.txt && move /y c.txt list.log
del *.txt

窄口牛 发表于 2017-7-17 15:25:42

最终测试这样可以顺利筛选
@echo off
pushd %~dp0\
del list.log /q 2>nul
dir *-kb*.exe /b /o:n >a.txt
for /f %%a in ('wmic qfe get hotfixid ^| findstr /i "^KB"') do echo %%a>>1.txt
::for /f "tokens=2" %%a in ('systeminfo^|findstr /i "kb"') do echo %%a>>1.txt
for /f "tokens=1 delims=-_" %%i in (1.txt) do echo %%i>>b.txt
findstr /V /I /G:b.txt a.txt > c.txt && move /y c.txt list.log
pause
del *.txt
for /f "eol=o eol=O " %%j in (list.log) do echo 正在安装补丁%%j && %%j /quiet /norestart
不知道为什么简化后和我之前的就筛选失败

nttwqz 发表于 2017-7-17 18:07:10

测试之前的筛选没有问题

窄口牛 发表于 2017-7-17 19:05:24

原因不明,我这确实不行,无奈。

nttwqz 发表于 2017-7-17 21:56:09

窄口牛 发表于 2017-7-17 19:05
原因不明,我这确实不行,无奈。

回显开,在cmd中执行一下批处理排除一下错误

窄口牛 发表于 2017-7-18 07:48:40



无错误提示

nttwqz 发表于 2017-7-18 15:45:57

怎么,没有生成list.log?

窄口牛 发表于 2017-7-18 16:08:06

我测试的共13个补丁,正确应该是筛留2个补丁需要打,错误的筛留多与两个,因具体数字没记住,所以只用多于两个来描述。

nttwqz 发表于 2017-7-18 16:19:04

窄口牛 发表于 2017-7-18 16:08
我测试的共13个补丁,正确应该是筛留2个补丁需要打,错误的筛留多与两个,因具体数字没记住,所以只用多于 ...

人工对比下a.txt b.txt,看多出来的有哪里不一样……

窄口牛 发表于 2017-7-18 16:51:37

本帖最后由 窄口牛 于 2017-7-18 16:58 编辑

我测试的正确的就是两个。
a.txt
IE8-WindowsXP-KB2964358-x86-custom-CHS.exe
ie8-windowsxp-kb4018271-x86-custom-chs_4e41e3f3c9af0222e158d86aecf64fbfb0c9b48d.exe
WindowsXP-KB2483618-x86-CHS.exe
WindowsXP-KB2813345-x86-CHS.exe
WindowsXP-KB2859537-x86-CHS.exe
WindowsXP-KB4012583-x86-Custom-CHS.exe
windowsxp-kb4012598-x86-custom-chs_dca9b5adddad778cfd4b7349ff54b51677f36775.exe
WindowsXP-KB4018466-x86-Custom-CHS.exe
WindowsXP-KB4019204-x86-Custom-CHS.exe
WindowsXP-KB4022747-x86-Custom-CHS.exe
WindowsXP-KB4024323-x86-Custom-CHS.exe
WindowsXP-KB4024402-x86-Custom-CHS.exe
WindowsXP-KB4025218-x86-Custom-CHS.exe
正确筛选list.og
WindowsXP-KB2483618-x86-CHS.exe
WindowsXP-KB2813345-x86-CHS.exe
错误筛选list.log
WindowsXP-KB2483618-x86-CHS.exe
WindowsXP-KB2813345-x86-CHS.exe
WindowsXP-KB2859537-x86-CHS.exe
WindowsXP-KB4012583-x86-Custom-CHS.exe
windowsxp-kb4012598-x86-custom-chs_dca9b5adddad778cfd4b7349ff54b51677f36775.exe
WindowsXP-KB4018466-x86-Custom-CHS.exe
WindowsXP-KB4019204-x86-Custom-CHS.exe
WindowsXP-KB4022747-x86-Custom-CHS.exe
WindowsXP-KB4024323-x86-Custom-CHS.exe
WindowsXP-KB4024402-x86-Custom-CHS.exe
WindowsXP-KB4025218-x86-Custom-CHS.exe

只筛掉了两个ie8的补丁

nttwqz 发表于 2017-7-18 17:33:24

b.txt??

窄口牛 发表于 2017-7-18 18:05:01

b.txt

nttwqz 发表于 2017-7-18 18:32:55

窄口牛 发表于 2017-7-18 18:05
b.txt

原因已经找到,wmic输出每个补丁名称后面有很多空格(除了有-_),文件名没有空格,所以只需将分隔符由“-_”改为“-_ ”即可,就是加了个空格

窄口牛 发表于 2017-7-18 18:47:56

完美!nt6不存在这个问题。大师好认真,硬硬的找到了问题所在,佩服!

zhiwen1977 发表于 2017-7-29 15:02:27

我自己专为WIN系统写的批处理,自动侦测WIN系统版本,并根据系统已有的补丁来判断还有多少补丁没有安装,最终自动安装系统没有的补丁程序。另外将此批处理与补丁放在同一目录内就可运行了。横线下边的就是自动安装系统补丁批处理了。
=================================================================================================================================================


@echo off
title 系统补丁批量自动安装

color 1f

cls

PUSHD %~dp0
del /q /f *.txt

rem //判断WIN7、WIN8、WIN10系统并获取最高权限
ver|find /i "10.0." > NUL &&goto UACwin10
ver|find /i "6.3." > NUL &&goto UACwin8
ver|find /i "6.2." > NUL &&goto UACwin8
ver|find /i "6.1." > NUL &&goto UACwin7
ver|find /i "6.0." > NUL &&goto UACwin7
ver|find /i "5.00"> NUL &&goto fixpatch
ver|find /i "5.1"> NUL &&goto fixpatch
ver|find /i "5.2"> NUL &&goto fixpatch
IF EXIST %SystemRoot%\System32\PECMD.INI goto ErrSystem


:ErrSystem
cls
Echo.
Echo.
Echo注意:经测试,本软件只适用于WINXP及其以上的Windows操作系统。
Echo很遗憾,你的系统不符合本程序的要求。按回车键退出安装程序!!!
Pause >nul
Exit

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:fixpatch

IF /i not "%OS%."=="Windows_NT." goto ErrSystem

::判断系统版本并设置TheOS
ver|find /i "10.">nul &&SET Winver=nt6&& SET TheOS=Win10
ver|find /i "6.3">nul &&SET Winver=nt6&& SET TheOS=Win8.1
ver|find /i "6.2">nul &&SET Winver=nt6&& SET TheOS=Win8
ver|find /i "6.1">nul &&SET Winver=nt6&& SET TheOS=Win7
ver|find /i "6.0">nul &&SET Winver=nt6&& SET TheOS=Vista
ver|find /i "5.0">nul &&SET Winver=nt5&& SET TheOS=2000
ver|find /i "5.1">nul &&SET Winver=nt5&& SET TheOS=XP
ver|find /i "5.2">nul &&SET Winver=nt5&& SET TheOS=2003


cls

SET TP=%~dp0
SET TP=%TP:~0,-1%
cd /d %TP%

Echo.
@Echo**************************************************
@Echo ║ 运行全自动安装补丁(全部补丁都安装)   ║
@Echo ║                                            ║
@Echo ║ 运行全自动安装补丁(系统已有的省略安装)  ║
@Echo ║                                            ║
@Echo ║ 删除全部补丁                             ║
@Echo ║                                            ║
@Echo ║ 查看目录内补丁                            ║
@Echo ║                                            ║
@Echo ║ 退出本程序                                ║
@Echo**************************************************
Echo.
Echo   您现在使用的是 %TheOS% 操作系统,请确保您拥有管理员权限
IF "%Winver%"=="nt6" Echo  并关闭了UAC和360等保护软件,否则安装可能失败!
Echo.

SET Choice=
SET /P Choice=   请选择要进行的操作 (1/2/3/4/5=退出) ,然后按回车:
Echo.
IF "%Choice%"=="" Goto fixpatch
IF Not "%Choice%"=="" SET Choice=%Choice:~0,1%
IF /I "%Choice%"=="1" Goto Start0
IF /I "%Choice%"=="2" Goto Start1
IF /I "%Choice%"=="3" Goto Del
IF /I "%Choice%"=="4" Goto Exp
IF /I "%Choice%"=="5" Goto Exit


rem //运行全自动安装补丁(全部补丁都安装)
:Start0
echo 请将此批处理与补丁放在同一目录内运行才能自动安装系统补丁
echo.

ver|find /i "5.00"> NUL &&goto win_xp
ver|find /i "5.1"> NUL &&goto win_xp
ver|find /i "5.2"> NUL &&goto win_xp

rem //目录内补丁具体内容
dir /a /b *.msu > hotfix.txt

rem //显示目录内补丁总数
for /f "tokens=2 delims=:" %%a in ('find /c /v "" hotfix.txt')do set/a patch1=%%a+0

echo.
echo.
@echo 总共要自动安装的系统补丁数:%patch1%
echo.


rem //生成安装批处理程序
echo.@echo off >install.cmd
echo.title win系统补丁自动安装 >>install.cmd


echo echo.总共要安装 %patch1% 个补丁 >>install.cmd
echo echo. >>install.cmd

for /l %%i in (1,1,%patch1%) do echo echo.Windows有%patch1%个补丁要安装,正在安装第%%i个补丁,请稍等......>>hotfixA.txt
echo echo. >>install.cmd
ver|find /i "6.2." > NUL &&goto W8
ver|find /i "6.3." > NUL &&goto W8


for /f "tokens=1" %%i in (hotfix.txt) do echo.start /wait wusa %%i /quiet /norestart>>hotfixB.txt


GOTO W7


:W8
for /f "tokens=1" %%i in (hotfix.txt) do echo.start /wait %%i /quiet /norestart>>hotfixB.txt



:W7
rem //合并成一个文件并生成批处理命令
setlocal enabledelayedexpansion
echo.
set "间隔行数=1"
echo.
for /f "delims=" %%a in (hotfixB.txt) do (
      set /a LineA+=1
      set "_!LineA!=%%a"
)

(for /f "delims=" %%a in (hotfixA.txt) do (
      echo,%%a
      set /a "m=(LineB+=1)%%间隔行数, x+=^!m, y=(x-1)%%LineA+1"
      if "!m!" == "0" for %%b in (!y!) do echo,!_%%b!
))>>install.cmd
echo.
echo echo.系统安装完成,将在40秒后关闭电脑。 >>install.cmd
echo.
echo Shutdown.exe -s -t 40 >>install.cmd
echo exit>>install.cmd

rem //删除hotfixA.txt
if exist hotfixA.txt del /q /f hotfixA.txt

rem //删除hotfixB.txt
if exist hotfixB.txt del /q /f hotfixB.txt

rem //删除
del /q /f hotfix*.txt

rem //准备运行install.cmd
if exist install.cmd start /wait install.cmd

rem //删除install.cmd
if exist install.cmd del /q /f install.cmd
echo.
echo 39秒后自动关闭电脑!
Shutdown.exe -s -t 39
exit






rem //运行全自动安装补丁(系统已有的省略安装)
:Start1
echo.
echo 请将此批处理与补丁放在同一目录内运行才能自动安装系统补丁...
echo.
ver|find /i "5.00"> NUL &&goto win_xp1
ver|find /i "5.1"> NUL &&goto win_xp1
ver|find /i "5.2"> NUL &&goto win_xp1

color 0A
cls
rem //已经安装在系统上的补丁

for /f "tokens=2" %%a in ('systeminfo^|findstr /i "KB"') do (
echo %%a>>info.txt
)


rem //目录内所有补丁中还没安装上系统的补丁patch

rem //目录内补丁具体内容
dir /a /b *.msu > hotfix.txt

rem //显示目录内补丁总数
for /f "tokens=2 delims=:" %%a in ('find /c /v "" hotfix.txt')do set/a patch1=%%a+0


copy /y hotfix.txt patch.txt >nul
for /f "delims=" %%a in (info.txt) do (
type patch.txt|find /i "%%a">nul&&(type patch.txt|find /v /i "%%a")>temp.txt
move /y temp.txt patch.txt >nul 2>nul
)
echo.
echo 目录内补丁总共有%patch1%个
Echo.

rem //显示目录内所有补丁中还没安装上系统的补丁的个数
for /f "tokens=2 delims=:" %%a in ('find /c /v "" patch.txt')do set/a patch2=%%a+0

echo 目录内所有补丁中还没安装上系统的补丁有%patch2%个

if %patch2%==0 goto qqq

rem //生成安装批处理程序
echo.@echo off >install.cmd
echo.title win系统补丁自动安装 >>install.cmd
echo echo. >>install.cmd
echo echo.目录内总共有 %patch1% 个补丁,其中有 %patch2% 个补丁未被安装 >>install.cmd
echo echo. >>install.cmd
for /l %%i in (1,1,%patch2%) do echo echo.Windows有%patch2%个补丁将要安装,正在安装第%%i个补丁,请稍等......>>hotfixA.txt

ver|find /i "6.2." > NUL &&goto W81
ver|find /i "6.3." > NUL &&goto W81

for /f "tokens=1" %%i in (hotfix.txt) do echo.start /wait wusa %%i /quiet /norestart>>hotfixB.txt

GOTO W71


rem //win8和win8.1补丁安装方法
:W81
for /f "tokens=1" %%i in (hotfix.txt) do echo.start /wait %%i /quiet /norestart>>hotfixB.txt



:W71

rem //合并成一个文件并生成批处理命令
setlocal enabledelayedexpansion
echo.
set "间隔行数=1"
echo.
for /f "delims=" %%a in (hotfixB.txt) do (
      set /a LineA+=1
      set "_!LineA!=%%a"
)

(for /f "delims=" %%a in (hotfixA.txt) do (
      echo,%%a
      set /a "m=(LineB+=1)%%间隔行数, x+=^!m, y=(x-1)%%LineA+1"
      if "!m!" == "0" for %%b in (!y!) do echo,!_%%b!
))>>install.cmd
echo echo. >>install.cmd
echo echo.系统安装完成,将在40秒后关闭电脑。 >>install.cmd
echo.
echo Shutdown.exe -s -t 40 >>install.cmd
echo exit>>install.cmd
Echo.
Echo.
Pause

if exist info.txt del /q /f info*.txt
if exist hotfix.txt del /q /f hotfix*.txt
if exist patch.txt del /q /f *.txt


rem //准备运行install.cmd
if exist install.cmd start /wait install.cmd

rem //删除install.cmd
if exist install.cmd del /q /f install.cmd

exit



:qqq
echo.
echo.你的系统已经装过了目录内的补丁了!!!按任意键退出!
pause
exit




:win_xp
rem //winxp补丁(全部补丁都安装)
cls
echo.
echo.

rem //目录内补丁具体内容
dir /a /b *.exe > hotfix.txt


rem //显示目录内补丁总数
for /f "tokens=2 delims=:" %%a in ('find /c /v "" hotfix.txt')do set/a patch1=%%a+0

echo.
echo.
@echo 总共要自动安装的系统补丁数:%patch1%
echo.
rem //生成安装批处理程序
echo.@echo off >install.cmd
echo.title win系统补丁自动安装 >>install.cmd


echo echo.总共要安装 %patch1% 个补丁 >>install.cmd
echo echo. >>install.cmd
for /l %%i in (1,1,%patch1%) do echo echo.Windows有%patch1%个补丁要安装,正在安装第%%i个补丁,请稍等......>>hotfixA.txt
echo echo. >>install.cmd
for /f "tokens=1" %%i in (hotfix.txt) do echo.start /wait %%i /passive /norestart /nobackup>>hotfixB.txt

rem //合并成一个文件并生成批处理命令
setlocal enabledelayedexpansion
echo.
set "间隔行数=1"
echo.
for /f "delims=" %%a in (hotfixB.txt) do (
      set /a LineA+=1
      set "_!LineA!=%%a"
)

(for /f "delims=" %%a in (hotfixA.txt) do (
      echo,%%a
      set /a "m=(LineB+=1)%%间隔行数, x+=^!m, y=(x-1)%%LineA+1"
      if "!m!" == "0" for %%b in (!y!) do echo,!_%%b!
))>>install.cmd

echo exit>>install.cmd


rem //删除hotfixA.txt
if exist hotfixA.txt del /q /f hotfixA.txt

rem //删除hotfixB.txt
if exist hotfixB.txt del /q /f hotfixB.txt

rem //删除
del /q /f hotfix*.txt

rem //准备运行install.cmd
if exist install.cmd start /wait install.cmd

rem //删除install.cmd
if exist install.cmd del /q /f install.cmd
echo.
echo 39秒后自动关闭电脑!
Shutdown.exe -s -t 39
exit




:win_xp1
rem //winxp补丁(系统未安装的补丁才装)
echo.
echo 请将此批处理与Windows补丁放在同一目录内运行才能自动安装系统补丁
echo.

rem ========目录内所有补丁中还没安装上系统的补丁==================

rem //目录内补丁具体内容
dir /a /b *.exe > hotfix.txt

rem //显示目录内补丁总数
for /f "tokens=2 delims=:" %%a in ('find /c /v "" hotfix.txt')do set/a patch1=%%a+0

ver|find /i "5.00"> NUL &&goto win_2k1
ver|find /i "5.1"> NUL &&goto win_2k1

rem //已安装在系统上的补丁情况
for /f "tokens=2" %%a in ('systeminfo^|findstr /i "KB"') do (
echo %%a>>info.txt
)

copy /y hotfix.txt patch.txt >nul
for /f "delims=" %%a in (info.txt) do (
type patch.txt|find /i "%%a">nul&&(type patch.txt|find /v /i "%%a")>temp.txt
move /y temp.txt patch.txt >nul 2>nul
)

goto xp2k

:win_2k1
echo.
echo 正在检测已经安装的补丁
rem //从文件中取出一个补丁的名字,截取中间的补丁标号(如KB873339),到注册表查询这个是否已经安装了,没有就将其存放到patch.txt文件里
for /f "delims=- tokens=2" %%i in ('dir *-kb*.exe /b /on') do REG QUERY "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\HotFix\%%i" >nul || dir /b *%%i*.exe >>patch.txt && cls && echo 正在检测已经安装的补丁

goto xp2k
exit


:xp2k
echo.
echo 目录内补丁总共有%patch1%个
Echo.

rem //显示目录内所有补丁中还没安装上系统的补丁的个数
for /f "tokens=2 delims=:" %%a in ('find /c /v "" patch.txt')do set/a patch2=%%a+0
echo.
echo 目录内所有补丁中还没安装上系统的补丁有%patch2%个
echo.
if %patch2%==0 goto qqq

rem //生成安装批处理程序
echo.@echo off >install.cmd
echo.title win系统补丁自动安装 >>install.cmd
echo.
echo echo.目录内总共有 %patch1% 个补丁,其中有 %patch2% 个补丁未被安装 >>install.cmd
echo echo. >>install.cmd
for /l %%i in (1,1,%patch2%) do echo echo.Windows有%patch2%个补丁将要安装,正在安装第%%i个补丁,请稍等......>>hotfixA.txt

for /f "tokens=1" %%i in (patch.txt) do echo.start /wait %%i /passive /norestart /nobackup>>hotfixB.txt

rem //合并成一个文件并生成批处理命令
setlocal enabledelayedexpansion
echo.
set "间隔行数=1"
echo.
for /f "delims=" %%a in (hotfixB.txt) do (
      set /a LineA+=1
      set "_!LineA!=%%a"
)

(for /f "delims=" %%a in (hotfixA.txt) do (
      echo,%%a
      set /a "m=(LineB+=1)%%间隔行数, x+=^!m, y=(x-1)%%LineA+1"
      if "!m!" == "0" for %%b in (!y!) do echo,!_%%b!
))>>install.cmd
echo.
echo echo.系统安装完成,将在40秒后关闭电脑。 >>install.cmd
echo.
echo Shutdown.exe -s -t 40 >>install.cmd
echo exit>>install.cmd
pause
Echo.
rem //准备运行install.cmd
if exist install.cmd start /wait install.cmd

rem //删除install.cmd
if exist install.cmd del /q /f install.cmd
Echo.
if exist info.txt del /q /f info*.txt
if exist hotfix.txt del /q /f hotfix*.txt
if exist patch.txt del /q /f *.txt
exit




:Exp
explorer %TP%
goto fixpatch
exit




:Del
echo 按任意键将删除目录内全部补丁文件,并退出程序!!!
pause
del /q /f *.exe
del /q /f *.msu
exit




:UACwin7
CLS
ECHO.
ECHO ================================
ECHOWIN7获取批处理文件管理员权限
ECHO ================================

:: BatchGotAdmin
:-------------------------------------
REM --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges(请求管理员权限)...
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
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
pushd "%CD%"
CD /D "%~dp0"
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::   以下为需要运行的批处理文件代码   ::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
rem 本行以下可修改为你需要的批处理命令

rem //去除WIN7-UAC小盾牌(重启电脑生效)
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" /f /v ConsentPromptBehaviorAdmin /t REG_DWORD /d 0
reg add "HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\System" /f /v EnableLUA /t REG_DWORD /d 0

taskkill /f /im explorer.exe
attrib -s -r -h "%userprofile%\AppData\Local\iconcache.db"
del "%userprofile%\AppData\Local\iconcache.db" /f /q
start explorer
goto fixpatch
exit



:UACwin8
CLS
ECHO.
ECHO ================================
ECHOWIN8获取批处理文件管理员权限
ECHO ================================

:: BatchGotAdmin
:-------------------------------------
REM --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges(请求管理员权限)...
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
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
pushd "%CD%"
CD /D "%~dp0"
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::   以下为需要运行的批处理文件代码   ::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
rem 本行以下可修改为你需要的批处理命令

rem //去除WIN8.1-UAC小盾牌
taskkill /f /im explorer.exe
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Icons" /v 77 /d "%systemroot%\system32\imageres.dll,197" /t reg_sz /f
del "%userprofile%\AppData\Local\iconcache.db" /f /q
taskkill /f /im explorer.exe
start %systemroot%\explorer

goto fixpatch
exit




:UACwin10

CLS
ECHO.
ECHO ================================
ECHOWIN10获取批处理文件管理员权限
ECHO ================================
:init
setlocal DisableDelayedExpansion
set "batchPath=%~0"
for %%k in (%0) do set batchName=%%~nk
set "vbsGetPrivileges=%temp%\OEgetPriv_%batchName%.vbs"
setlocal EnableDelayedExpansion
:checkPrivileges
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges )
:getPrivileges
if '%1'=='ELEV' (echo ELEV & shift /1 & goto gotPrivileges)
ECHO.
ECHO ********************************
ECHO 请求 UAC 权限批准……
ECHO ********************************
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%vbsGetPrivileges%"
ECHO args = "ELEV " >> "%vbsGetPrivileges%"
ECHO For Each strArg in WScript.Arguments >> "%vbsGetPrivileges%"
ECHO args = args ^& strArg ^& " ">> "%vbsGetPrivileges%"
ECHO Next >> "%vbsGetPrivileges%"
ECHO UAC.ShellExecute "!batchPath!", args, "", "runas", 1 >> "%vbsGetPrivileges%"
"%SystemRoot%\System32\WScript.exe" "%vbsGetPrivileges%" %*
exit /B

:gotPrivileges
setlocal & pushd .
cd /d %~dp0
if '%1'=='ELEV' (del "%vbsGetPrivileges%" 1>nul 2>nul&shift /1)

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::   以下为需要运行的批处理文件代码   ::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
rem 本行以下可修改为你需要的批处理命令

REM //关闭WIN10用户账户控制(UAC)
rem reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" /v "EnableLUA" /d 0 /t REG_DWORD /f
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" /v "ConsentPromptBehaviorAdmin" /d 0 /t REG_DWORD /f


goto fixpatch

exit

窄口牛 发表于 2017-7-29 15:16:03

不错,谢谢分享!
页: [1]
查看完整版本: wmic的环境在批处理里怎么退出?