标题: [文件操作] 求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 运行- function Get-FileMD5($file){
- $stream = [System.IO.File]::Open($file, [IO.FileMode]::Open, [IO.FileAccess]::Read);
- $objMD5 = [System.Security.Cryptography.MD5]::Create('MD5');
- $strMD5 = [BitConverter]::ToString($objMD5.ComputeHash($stream));
- $stream.Close();
- return $strMD5.Replace('-', '');
- }
-
- if( $args[0] -eq $null ){
- start PowerShell -Args ('-Exec Bypass -file "' + $MyInvocation.MyCommand.Path + '" ARG') -Verb RunAs;
- } else {
- gwmi Win32_LogicalDisk -Filter "DriveType='5'" | forEach {
- $file1 = $_.Name + '\setup.exe';
- $file2 = $_.Name + '\configuration.xml';
- if( [IO.File]::Exists($file1) -and [IO.File]::Exists($file2) ){
- $MD5 = Get-FileMD5 $file1;
- if( $MD5 -eq 'C1A8D182A940E751F1C9A506ADA22FE8' ){
- start $file1 -Args ('/configure "' + $file2 + '"') -Wait;
- }
- }
- }
- echo 'Done';
- [Console]::ReadKey();
- }
复制代码
作者: WHY 时间: 2020-11-8 17:21
本帖最后由 WHY 于 2020-11-8 20:03 编辑
- @echo off
- setlocal enabledelayedexpansion
- copy "%~f0" "%winDir%\" >nul 2>nul
- if "!errorLevel!" NEQ "0" (echo;bat需以管理员身份运行&pause&goto :EOF)
- del /f "%WinDir%\%~nx0" >nul 2>nul
-
- set "file1=setup.exe"
- set "file2=configuration.xml"
-
- for /f %%i in ('wmic LogicalDisk where "DriveType='5'" Get Name /format:Table ^| find ":"') do (
- if exist "%%~di\!file1!" if exist "%%~di\!file2!" (
- set "md5="
- for /f "skip=1" %%j in ('certutil -hashfile "%%~di\!file1!" MD5') do (
- if not defined md5 set "md5=%%j"
- )
- if /i "!md5!" == "C1A8D182A940E751F1C9A506ADA22FE8" (
- start "" /wait "%%~di\!file1!" /configure "%%~di\!file2!"
- )
- )
- )
- pause
复制代码
作者: gshwlj 时间: 2020-11-9 08:42
回复 3# WHY
感激涕零,顶礼膜拜。。。
多谢多谢啦。。。
祝好,再祝好!!!!
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |