无忧启动论坛

标题: 批处理关闭功能 [打印本页]

作者: wenbieba    时间: 2023-7-12 10:33
标题: 批处理关闭功能

利用命令关闭一些Windows功能,如:TFTP、Microsoft-Hyper-V用此以TFTP为例
Dism /online /Get-FeatureInfo /FeatureName:TFTP  均运行正常,可关闭,
为了方便,把需要禁用的功能名写入一个TXT记事本本內
用以下命令执行:

for /F %%i in (' Type "%~dp0Windows\Remove.TXT" 2^>nul')
for /F "tokens=2 delims=: " %%a in ('dism /online /get-features ^| Findstr /I /C:"FeatureName : %%i"') do if "%%a" NEQ "" Call Dism /online /Disable-Feature /FeatureName:%%a >nul
   )
运行之后无效果。麻烦大佬指点

作者: fulibo    时间: 2023-7-12 11:01
照着这个来!
  1. @echo off
  2. pushd "%~dp0"

  3. set MT=H:\1

  4. reg load HKLM\SOFT "%MT%\Windows\System32\config\software"

  5. for /f %%i in ('findstr /i . Remove.txt 2^>nul') do ( call :SETVALUE %%i )

  6. reg unload HKLM\SOFT

  7. for /f %%i in ('findstr /i . Remove.txt 2^>nul') do dism /NoRestart /image:%MT% /Remove-package /Packagename:%%i

  8. pause
  9. exit
复制代码



作者: china.gaofeng    时间: 2023-7-12 12:22
调用powershell更方便些


  1. powershell "(Get-Content .\remove.txt).ForEach({Get-WindowsOptionalFeature $_ -online|Disable-WindowsOptionalFeature -online})"
复制代码


作者: bjay2008xmy    时间: 2023-7-12 12:26
提示: 作者被禁止或删除 内容自动屏蔽
作者: system110    时间: 2023-7-12 12:38

多谢分享
作者: wenbieba    时间: 2023-7-12 14:00
bjay2008xmy 发表于 2023-7-12 12:26
有了现成的,就不想再费脑筋想了~

并非如此,折腾好久没有效果,才来求助。从大佬的诊断中学习
作者: wenbieba    时间: 2023-7-12 14:29
china.gaofeng 发表于 2023-7-12 12:22
调用powershell更方便些

谢谢大佬,有时间要学习学习powershell
作者: yc2428    时间: 2023-7-12 15:13
学习学习
作者: wenbieba    时间: 2023-7-12 17:54
china.gaofeng 发表于 2023-7-12 12:22
调用powershell更方便些

Get-Content .\remove.txt此处路径该如何指定,.txt文件未在当前根目录,在当前文件夹下的子目录里边,Get-Content %Lists%\FeaturesList.txt,这种写法提示Get-WindowsOptionalFeature : 找不到接受实际参数“IIS-HttpErrors”的位置形式参数。
所在位置 行:1 字符: 78
+ ... turesList.txt).ForEach({Get-WindowsOptionalFeature $_ -online|Disable ...
+                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-WindowsOptionalFeature],ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.Dism.Commands.GetWindowsOptionalFeatureCommand

作者: wenbieba    时间: 2023-7-12 17:59
china.gaofeng 发表于 2023-7-12 12:22
调用powershell更方便些

把Remove.TXT和批处理放同一文件夹也报同样错
作者: yangnaohai    时间: 2023-7-12 19:22
感谢分享
作者: seeimpact153    时间: 2023-7-13 02:23
感谢分享
作者: china.gaofeng    时间: 2023-7-13 13:45
wenbieba 发表于 2023-7-12 17:59
把Remove.TXT和批处理放同一文件夹也报同样错

你remove.txt文件里的内容是什么格式?
作者: wenbieba    时间: 2023-7-13 14:54
china.gaofeng 发表于 2023-7-13 13:45
你remove.txt文件里的内容是什么格式?

文本格式,remove.TXT,文本下一行一列:

TFTP
Microsoft-Hyper-V

作者: china.gaofeng    时间: 2023-7-13 15:09
wenbieba 发表于 2023-7-13 14:54
文本格式,remove.TXT,文本下一行一列:

TFTP

那你试试
  1. powershell "(Get-Content .\remove.txt).ForEach({Get-WindowsOptionalFeature -FeatureName $_ -online|Disable-WindowsOptionalFeature -online})"
复制代码

作者: wenbieba    时间: 2023-7-13 15:35
china.gaofeng 发表于 2023-7-13 15:09
那你试试

Get-Content %Lists%\remove.txt
这种路径写法对吗
作者: china.gaofeng    时间: 2023-7-13 16:10
wenbieba 发表于 2023-7-13 15:35
Get-Content %Lists%\remove.txt
这种路径写法对吗

powershell里的环境变量不是用%%引用

1. 自定义变量:$list = "完整路径",然后用$list引用
2. 环境变量:$env:TEMP
作者: conlin888    时间: 2023-7-13 17:01
@echo off
for /F "delims=" %%I in ('Type "%~dp0Windows\Remove.TXT" 2^>nul') do (
   for /F "tokens=2 delims=:" %%a in ('dism /online /get-features^|Find /i ": %%I"') do (
   if /i " %%I"=="%%a" Dism /online /Disable-Feature /FeatureName:"%%I"
   )
)
echo.&echo.&pause
作者: xcfgw8    时间: 2023-7-13 17:22
多谢分享!
作者: conlin888    时间: 2023-7-13 17:26
本帖最后由 conlin888 于 2023-7-13 17:35 编辑

或者:
@echo off
for /F "tokens=2 delims=:" %%a in ('dism /online /get-features^|find /i " : "') do (
    for /f "delims=" %%I in ('Type "%~dp0Windows\Remove.TXT"') do (
    if /i " %%I"=="%%a" Dism /online /Disable-Feature /FeatureName:"%%I"
    )
)
echo.&echo.&pause
作者: nttwqz    时间: 2023-7-13 21:25
本帖最后由 nttwqz 于 2023-7-13 21:28 编辑
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. pause
  4. pushd %~dp0
  5. for /f "delims=" %%a in (remove.txt) do (
  6.     if not defined feature_list (
  7.         set "feature_list=/featurename:%%a"
  8.     ) else (
  9.         set "feature_list=!feature_list! /featurename:%%a"
  10.     )
  11. )

  12. if defined feature_list (
  13.     dism /online /disable-feature !feature_list! /norestart
  14.     if !errorlevel!==0 (
  15.         color 2f
  16.     ) else (
  17.         color 4f
  18.     )
  19. ) else (
  20.     color 4f
  21.     echo;错误!需要禁用的功能列表为空...
  22. )
  23. echo;
  24. pause
复制代码

功能每行一个,如果功能名称有空格,请用英文""


作者: wfg1555    时间: 2023-7-17 20:27
这个要学些知识,
作者: wjggo6210    时间: 2023-10-11 13:36
感谢分享
作者: rbwrbw    时间: 2023-10-12 08:57
慢慢调试
作者: yyz2191958    时间: 2023-10-18 10:29
进来学习




欢迎光临 无忧启动论坛 (http://wuyou.net/) Powered by Discuz! X3.3