Question

I have a batch file that I put together to automate installations we do in house, but I have headers made in ASCII art that I print into the script with type. When I run as admin though, it will not type the text files.

Is there something I'm missing?

:::::::::::::::::::::::::::::::::::::::::::
:: Automatically check & get admin rights::
:::::::::::::::::::::::::::::::::::::::::::
@echo off
CLS 
ECHO.
ECHO =============================
ECHO Running Admin shell
ECHO =============================

:checkPrivileges 
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges ) 

:getPrivileges 
if '%1'=='ELEV' (shift & goto gotPrivileges)  
ECHO. 
ECHO **************************************
ECHO Invoking UAC for Privilege Escalation 
ECHO **************************************

setlocal DisableDelayedExpansion
set "batchPath=%~0"
setlocal EnableDelayedExpansion
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\OEgetPrivileges.vbs" 
ECHO UAC.ShellExecute "!batchPath!", "ELEV", "", "runas", 1 >> "%temp%\OEgetPrivileges.vbs" 
"%temp%\OEgetPrivileges.vbs" 
exit /B 

:gotPrivileges 
:::::::
:START:
:::::::
setlocal & pushd .
@echo off

:: Get ADMIN Privs
:-------------------------------------
mkdir "%windir%\BatchGotAdmin"
if '%errorlevel%' == '0' (
  rmdir "%windir%\BatchGotAdmin" & goto gotAdmin 
) else ( goto UACPrompt )

:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    echo UAC.ShellExecute %0, "", "", "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"
:-------------------------------------
:: End Get ADMIN Privs
@echo off
Title  Baseline Out of Box - Server Config
Color F
type RequiredFiles\header.txt
pause
cls
echo 3
ping localhost -n 2 > nul
cls
echo 3.
ping localhost -n 2 > nul
cls
echo 3..
ping localhost -n 2 > nul
cls
echo 3..2
ping localhost -n 2 > nul
cls
echo 3..2.
ping localhost -n 2 > nul
cls
echo 3..2..
ping localhost -n 2 > nul
cls
echo 3..2..1
ping localhost -n 2 > nul
cls
echo Liftoff
ping localhost -n 2 > nul
cls
echo Creating Users - Admin - Adding New Users to
echo Administrators and Remote Desktop User Groups
ping localhost -n 4 > nul
net user Admin  /ADD
net user  /ADD
net localgroup "Remote Desktop Users" Admin /add
net localgroup "Remote Desktop Users" /add
net localgroup "Administrators" Admin /add
net localgroup "Administrators"  /add
pause
cls
type RequiredFiles\text1.txt
ping localhost -n 2 > nul
cls
type RequiredFiles\text2.txt
ping localhost -n 2 > nul
cls
type RequiredFiles\text3.txt
ping localhost -n 2 > nul
pause
netsh interface set interface name = "Local Area Connection" newname = "Outside"
echo Local Area Connection has been changed to "Outside"
echo Now, minion, configure it's IP
echo.
echo.
echo Choose:
echo [Static] Set Static IP
echo [Dynamic] Set DHCP
echo.
:choice
SET /P C=[Static,Dynamic]?
for %%? in (Static) do if /I "%C%"=="%%?" goto Static
for %%? in (Dynamic) do if /I "%C%"=="%%?" goto Dynamic
goto choice
:Static
@echo off
echo "Please enter Static IP Address Information"
echo "Static IP Address:"
set /p IP_Addr=
echo.
echo "Default Gateway:"
set /p D_Gate=
echo.
echo "Subnet Mask:"
set /p Sub_Mask=
echo.
echo "DNS being configured to Google Public"
netsh interface ip set dns name="Outside" source=static addr=8.8.8.8

echo "Setting Static IP Information"
netsh interface ip set address "Outside" static %IP_Addr% %Sub_Mask% %D_Gate% 1
ping localhost -n 4 > nul
Echo Your New IP Settings
echo.
netsh int ip show config
pause
cls
goto interface2

:Dynamic
@ECHO OFF
ECHO Resetting IP Address and Subnet Mask For DHCP
netsh int ip set address name = "Outside" source = dhcp

ipconfig /renew

ECHO Here are the new settings for %computername%:
netsh int ip show config

pause
cls
goto interface2
:interface2

netsh interface set interface name = "Local Area Connection 2" newname = "Storage"
echo Local Area Connection 2 has been changed to "Storage"
echo Now, minion, configure it's IP....again
echo.
echo.
echo Choose:
echo [Static] Set Static IP
echo [Dynamic] Set DHCP
echo.
:choice
SET /P C=[Static,Dynamic]?
for %%? in (Static) do if /I "%C%"=="%%?" goto Static
for %%? in (Dynamic) do if /I "%C%"=="%%?" goto Dynamic
goto choice
:Static
@echo off
echo "Please enter Static IP Address Information"
echo "Static IP Address:"
set /p IP_Addr=
echo.
echo "Default Gateway:"
set /p D_Gate=
echo.
echo "Subnet Mask:"
set /p Sub_Mask=

echo "Setting Static IP Information"
netsh interface ip set address "Storage" static %IP_Addr% %Sub_Mask% %D_Gate% 1
ping localhost -n 4 > nul
cls
Echo Your New IP Settings
echo.
netsh int ip show config
pause
goto next

:Dynamic
@ECHO OFF
ECHO Resetting IP Address and Subnet Mask For DHCP
netsh int ip set address name = "LAN" source = dhcp

ipconfig /renew

Echo Your New IP Settings
echo.
netsh int ip show config
pause
goto next

:next
cls
echo Disabling Firewall
netsh advfirewall set allprofiles state off
msg * "WARNING - All Firewall Profiles Have Been Disabled"
ping localhost -n 4 > nul
pause
goto changename

:changename
cls
@echo off
SET /P PCNAME=Please enter desired computer name: 
REG ADD HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName /v ComputerName /t REG_SZ /d %PCNAME% /f
ping localhost -n 4 > nul
pause
goto enableremote

:enableremote
cls
Echo Enabling Remote Desktop
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server" /v fDenyTSConnections /t REG_DWORD /d 0 /f
ping localhost -n 4 > nul
pause
goto logmein

:logmein
cls
echo Installing LogMeIn - Please Follow Installer
msiexec /i RequiredFiles\LogMeIn.msi
echo LogMeIn Installer Has Finished
pause
goto genetec

:genetec
cls
echo Installing Genetec Security Center - Please Follow Installer
start /wait RequiredFiles\Genetec\Full\setup.exe
echo Genetec Installer Has Finished
pause
goto finished

:finished
cls
echo Out of Box Configuration Complete
msg * "Out of Box Configuration Complete"
ping localhost -n 2 > nul
cls
type RequiredFiles\end.txt
ping localhost -n 2 > nul
exit
Was it helpful?

Solution

When run as administrator the working directory changes.

Use the full path to the text files or a variable that uses %~dp0 to get the path of the batch file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top