Board logo

标题: [文件操作] 求bat检测光驱盘符内文件的MD5值并以管理员身份运行 [打印本页]

作者: gshwlj    时间: 2020-11-8 12:05     标题: 求bat检测光驱盘符内文件的MD5值并以管理员身份运行

求大神们给写一个  bat ...

功能:
1、检测当前电脑中的所有光驱,如果该光驱盘符内包括  setup.exe  且 setup.exe 的 md5 等于 C1A8D182A940E751F1C9A506ADA22FE8   则运行   
    该光驱盘符:\setup.exe /configure    该光驱盘符:\configuration.xml  

2、如果 bat 没有以管理员身份运行,则提示 “请推出重新以管理员身份运行”

不知可否实现。。。。。。。。。。。。。。。。。。。。

作者: WHY    时间: 2020-11-8 17:11

本帖最后由 WHY 于 2020-11-8 20:02 编辑

PowerShell 脚本,保存为 Test.ps1
执行方法:
1.在 cmd 命令行窗口输入:PowerShell -exec Bypass -File "脚本路径"
2.或者,右键单击脚本,选择使用 PowerShell 运行
  1. function Get-FileMD5($file){
  2.     $stream = [System.IO.File]::Open($file, [IO.FileMode]::Open, [IO.FileAccess]::Read);
  3.     $objMD5 = [System.Security.Cryptography.MD5]::Create('MD5');
  4.     $strMD5 = [BitConverter]::ToString($objMD5.ComputeHash($stream));
  5.     $stream.Close();
  6.     return $strMD5.Replace('-', '');
  7. }
  8. if( $args[0] -eq $null ){
  9.     start PowerShell -Args ('-Exec Bypass -file "' + $MyInvocation.MyCommand.Path + '" ARG') -Verb RunAs;
  10. } else {
  11.     gwmi Win32_LogicalDisk -Filter "DriveType='5'" | forEach {
  12.         $file1 = $_.Name + '\setup.exe';
  13.         $file2 = $_.Name + '\configuration.xml';
  14.         if( [IO.File]::Exists($file1) -and [IO.File]::Exists($file2) ){
  15.             $MD5 = Get-FileMD5 $file1;
  16.             if( $MD5 -eq 'C1A8D182A940E751F1C9A506ADA22FE8' ){
  17.                 start $file1 -Args ('/configure "' + $file2 + '"') -Wait;
  18.             }
  19.         }
  20.     }
  21.     echo 'Done';
  22.     [Console]::ReadKey();
  23. }
复制代码

作者: WHY    时间: 2020-11-8 17:21

本帖最后由 WHY 于 2020-11-8 20:03 编辑
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. copy "%~f0" "%winDir%\" >nul 2>nul
  4. if "!errorLevel!" NEQ "0" (echo;bat需以管理员身份运行&pause&goto :EOF)
  5. del /f "%WinDir%\%~nx0" >nul 2>nul
  6. set "file1=setup.exe"
  7. set "file2=configuration.xml"
  8. for /f %%i in ('wmic LogicalDisk where "DriveType='5'" Get Name /format:Table ^| find ":"') do (
  9.     if exist "%%~di\!file1!" if exist "%%~di\!file2!" (
  10.         set "md5="
  11.         for /f "skip=1" %%j in ('certutil -hashfile "%%~di\!file1!" MD5') do (
  12.             if not defined md5 set "md5=%%j"
  13.         )
  14.         if /i "!md5!" == "C1A8D182A940E751F1C9A506ADA22FE8" (
  15.             start "" /wait "%%~di\!file1!" /configure "%%~di\!file2!"
  16.         )
  17.     )
  18. )
  19. pause
复制代码

作者: gshwlj    时间: 2020-11-9 08:42

回复 3# WHY

感激涕零,顶礼膜拜。。。

多谢多谢啦。。。

祝好,再祝好!!!!




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2