多批文件无效

时间:2016-11-11 10:37:27

标签: batch-file

:: Hide Command and Set Scope
COLOR F0
@echo off
setlocal EnableExtensions

:: Customize Window
title - AnyConnect Project

:: Menu Options
:: Specify as many as you want, but they must be sequential from 1 with no gaps
:: Step 1. List the Application Names
set "App[1]=Set Drive to T"
set "App[2]=Create Dir AnyConnect"
set "App[3]=Copy AnyConnect to C"
set "App[4]=Map K Drive"
set "App[5]=Change Firewall"
set "App[6]=Amend Host File"
set "App[7]=Exit"

:: Display the Menu
set "Message="
:Menu
cls
echo.%Message%
echo.
echo.  Project AnyConnect
echo.
set "x=0"
:MenuLoop
set /a "x+=1"
if defined App[%x%] (
    call echo   %x%. %%App[%x%]%%
    goto MenuLoop
)
echo.

:: Prompt User for Choice
:Prompt
set "Input="
set /p "Input=Select what app:"

:: Validate Input [Remove Special Characters]
if not defined Input goto Prompt
set "Input=%Input:"=%"
set "Input=%Input:^=%"
set "Input=%Input:<=%"
set "Input=%Input:>=%"
set "Input=%Input:&=%"
set "Input=%Input:|=%"
set "Input=%Input:(=%"
set "Input=%Input:)=%"
:: Equals are not allowed in variable names
set "Input=%Input:^==%"
call :Validate %Input%

:: Process Input
call :Process %Input%
goto End


:Validate
set "Next=%2"
if not defined App[%1] (
    set "Message=Invalid Input: %1"
    goto Menu
)
if defined Next shift & goto Validate
goto :eof


:Process
set "Next=%2"
call set "App=%%App[%1]%%"

:: Run Installations
:: Specify all of the installations for each app.
:: Step 2. Match on the application names and perform the installation for each
if "%App%" EQU "Set Drive to T" subst T: %~d0\
if "%App%" EQU "Create Dir AnyConnect" mkdir c:\anyconnect
if "%App%" EQU "Copy AnyConnect to C" xcopy /s F:\AnyConnect\anyconnect-win-4.3.01095-pre-deploy-k9\anyconnect-win-4.3.01095-pre-deploy-k9.exe c:\anyconnect
if "%App%" EQU "Map K Drive" net use k: /d
net use k: \\xxxxxxx-exxxx.xxxx.xxx\Data
if "%App%" EQU "Change Firewall" rem Firewall Delete
netsh advfirewall firewall delete rule all
pause
if "%App%" EQU "Amend Host File" 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"
    set params = %*:"="
    echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"

    "%temp%\getadmin.vbs"
    del "%temp%\getadmin.vbs"
    exit /B

:gotAdmin
    pushd "%CD%"
    CD /D "%~dp0"
:--------------------------------------

:LOOP
SET Choice=
SET /P Choice="Do you want to modify HOSTS file ? (Y/N)"

IF NOT '%Choice%'=='' SET Choice=%Choice:~0,1%

ECHO.
IF /I '%Choice%'=='Y' GOTO ACCEPTED
IF /I '%Choice%'=='N' GOTO REJECTED
ECHO Please type Y (for Yes) or N (for No) to proceed!
ECHO.
GOTO Loop


:REJECTED
ECHO Your HOSTS file was left unchanged>>%systemroot%\Temp\hostFileUpdate.log
ECHO Finished.
GOTO END


:ACCEPTED
setlocal enabledelayedexpansion
::Create your list of host domains
set LIST=(xxxxxxx-exxxx.xxxx.xxx)
::Set the ip of the domains you set in the list above
set xxxxxxx-exxxx.xxxx.xxx=10.10.10.10
:: deletes the parentheses from LIST
set _list=%LIST:~1,-1%
::ECHO %WINDIR%\System32\drivers\etc\hosts > tmp.txt
for  %%G in (%_list%) do (
    set  _name=%%G
    set  _value=!%%G!
    SET NEWLINE=^& echo.
    ECHO Carrying out requested modifications to your HOSTS file
    ::strip out this specific line and store in tmp file
    type %WINDIR%\System32\drivers\etc\hosts | findstr /v !_name! > tmp.txt
    ::re-add the line to it
    ECHO %NEWLINE%^!_value! !_name!>>tmp.txt
    ::overwrite host file
    copy /b/v/y tmp.txt %WINDIR%\System32\drivers\etc\hosts
    del tmp.txt
)
ipconfig /flushdns
ECHO.
ECHO.
ECHO Finished, you may close this window now.
GOTO END
if "%App%" EQU "Exit" (
    exit
)

:: Prevent the command from being processed twice if listed twice.
set "App[%1]="
if defined Next shift & goto Process
goto :eof


:End
endlocal
pause >nul

我创建了一个bat文件来协助我们的项目,我需要将一个安全的USB设备设置为一个设置的驱动器号。我还希望它在运行一个项目时返回菜单。当我运行它接缝运行所有这些。我很难尝试让它起作用,需要一双新鲜的眼睛。

0 个答案:

没有答案