Fail at set batch with Admin Rights and put the computer at Domain

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Dasmius
Posts: 21
Joined: 02 Mar 2022 13:47

Fail at set batch with Admin Rights and put the computer at Domain

#1 Post by Dasmius » 02 Mar 2022 14:05

Sup guys?

I'v some problems cuz I'm trying to execute a batch file in user account in order to do some system configs. Itried to use this lines to give admin power to the executing batch:

:: BatchGotAdmin

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"
echo UAC.ShellExecute "%~s0", "", "", "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"

The major problem is that the I have to run the batch from a server, so it's start to open multiples windows and don't stop, I even tried this method:

net file 1>nul 2>nul && goto :run || powershell -ex unrestricted -Command "Start-Process -Verb RunAs -FilePath '%comspec%' -ArgumentList '/c %~fnx0 %*'"
goto :run

But, result was same as before...

My second problem is to put a computer at the Domain by Powershell (there iso no net command available). I was tryin this:

powershell.exe -command "& {Add-Computer -DomainName domainhere}"

But it happens to set WORKGROUP, not the Domain field


So, can anyone enlighten this problem?

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: Fail at set batch with Admin Rights and put the computer at Domain

#2 Post by elzooilogico » 02 Mar 2022 15:55

some thoughts

first, as you’ve stated, your script opens multiple windows, what is your script name? it’s trying to call/use some command with the same name?

also, you must add computers to trusted server list, in order to use remote admin using wmi or powershell (this last not sure, but the first indeed)

Dasmius
Posts: 21
Joined: 02 Mar 2022 13:47

Re: Fail at set batch with Admin Rights and put the computer at Domain

#3 Post by Dasmius » 03 Mar 2022 10:16

The main part of the script as follow:

Code: Select all

@ECHO OFF

MODE CON: COLS=150 LINES=50

CHCP 65001

Title Script de Configuração Home Office

CLS

    :: BatchGotAdmin
   
    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"
        echo UAC.ShellExecute "%~s0", "", "", "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"
	

CLS

ECHO.
ECHO                              [91m       _     _   _   _  _    [0m
ECHO                              [91m"│__│ │_ │  │_│ │ \ │_ <  │/"[0m
ECHO                              [91m"│  │ │_ │_ │   │_/ │_ _> │\"[0m
ECHO.
ECHO          [101;93m    0     0101010101 01010101 010    01 0101010101 0101010101[0m
ECHO          [101;93m   010        01     01       01 0   01     01     01      01[0m
ECHO          [101;93m  01 01       01     010101   01  0  01     01     01      01[0m
ECHO          [101;93m 01   01      01     01       01   0 01     01     01      01[0m
ECHO          [101;93m01     01     01     01010101 01    001     01     0101010101[0m
ECHO                                                               RØÐRIGØ
ECHO.
ECHO.
ECHO.
ECHO [104;93m╔═════════════════════════╗[0m
ECHO [104;93m║▒▒▒▒▒ MENU DE OPÇÕES ▒▒▒▒║[0m
ECHO [104;93m╠═════════════════════════╣[0m
ECHO [104;93m║ 1 » option              ║[0m
ECHO [104;93m║                         ║[0m
ECHO [104;93m║ 2 » option              ║[0m
ECHO [104;93m║                         ║[0m
ECHO [104;93m║ 3 » option              ║[0m
ECHO [104;93m║                         ║[0m
ECHO [104;93m║ 4 » SAIR                ║[0m
ECHO [104;93m╚═════════════════════════╝[0m
ECHO.

CHOICE /N /C:1234 /M "Escolha uma opção:"

if errorlevel ==4 goto sair
if errorlevel ==3 goto edp
if errorlevel ==2 goto cobranca
if errorlevel ==1 goto tdata

GOTO sair

When I run it, it start to open multiple windows
Last edited by aGerman on 03 Mar 2022 12:50, edited 1 time in total.
Reason: Please use code formatting!

Post Reply