Run Powershell from bat as Administrator

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Docfxit
Posts: 130
Joined: 12 Nov 2015 12:42

Run Powershell from bat as Administrator

#1 Post by Docfxit » 14 Apr 2023 23:33

I'm trying to run a Powershell script to change a service in windows 10 to manual and started.

I created a .bat file called Wordperfect.bat to call the .ps1 file.

Code: Select all

powershell.exe -noprofile -NoExit -command "&{start-process powershell -ArgumentList '-NoExit -noprofile -file \"%~dpn0.ps1\"' -verb RunAs}"
I created a .ps1 file called Wordperfect.ps1

Code: Select all

$compname = $(hostname)
$service = Get-Service msiserver -Computername $compname 
$service | Set-Service -StartupType Manual
$service | Start-Service
pause
do{
    Start-Sleep -seconds 3
    $service.Refresh()
}
until($service.Status -eq 'Running')
pause
I'm getting a powershell window saying Access is denied

Code: Select all

Set-Service : Service 'Windows Installer (msiserver)' cannot be configured due to the following error: Access is denied
At C:\Dnload\9xAddons\Wordperfect.ps1:3 char:12
+ $service | Set-Service -StartupType Manual
+            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (System.ServiceProcess.ServiceController:ServiceController) [Set-Servi
   ce], ServiceCommandException
    + FullyQualifiedErrorId : CouldNotSetService,Microsoft.PowerShell.Commands.SetServiceCommand
The user I'm running it with has Administrator rights.
I think I'm running the .ps1 with Run as Administrator

How can I change it to make sure it's in Manual mode and make sure it's started?

bxx
Posts: 5
Joined: 09 Mar 2023 03:49

Re: Run Powershell from bat as Administrator

#2 Post by bxx » 16 Apr 2023 12:17

try removing the -noexit maybe? From what i can tell it's starting your script from the *first* powershell, the one you invoke from cmd as non-admin.

einstein1969
Expert
Posts: 941
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: Run Powershell from bat as Administrator

#3 Post by einstein1969 » 17 Apr 2023 04:46

for start as Administrator try this:

Code: Select all

@echo off
Rem Check for Run as administrator.
call :Restart_as_Administrator %1

rem put here your code


goto :eof
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::   SUBROUTINE   ::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:Restart_as_Administrator
(Fsutil Dirty Query %SystemDrive%>Nul)&&(
        if "%1" neq "admin" (
		start %~f0 admin
		Exit
	)
)||( 
	mode con cols=90 lines=20
	echo( & echo 	It is necessary to start the script with administrative rights.
	echo( & echo 	Please wait ... I am restarting the script with administrative rights. 
	echo( & echo 	Answer "YES" to the next User Account Control UAC request to continue 
	echo( 	running this script with administrative permissions. & echo(
	timeout /t 4
	powershell.exe -c "Start -Verb RunAs cmd /c, ("^""%~f0"^"" -replace '[;,=() &^]', '^$0'), "admin" " & Exit
)
goto :eof

Docfxit
Posts: 130
Joined: 12 Nov 2015 12:42

Re: Run Powershell from bat as Administrator

#4 Post by Docfxit » 17 Apr 2023 11:37

I can't get the Powershell to work.

I'm getting "The syntax of the command is incorrect."

Docfxit
Posts: 130
Joined: 12 Nov 2015 12:42

Re: Run Powershell from bat as Administrator

#5 Post by Docfxit » 17 Apr 2023 16:45

I found the syntax error:

This line gives a syntax error:
call :Restart_as_Administrator
This line doesn't give a syntax error:
call :Restart_as_Admin

The code isn't putting it into Administrative mode.

einstein1969
Expert
Posts: 941
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: Run Powershell from bat as Administrator

#6 Post by einstein1969 » 19 Apr 2023 15:44

On my pc with windows 10 it works.

Do you get the UAC prompt?

Docfxit
Posts: 130
Joined: 12 Nov 2015 12:42

Re: Run Powershell from bat as Administrator

#7 Post by Docfxit » 19 Apr 2023 18:12

No. I have it turned off.

On my Windows 10 22h2
In Services, Windows Installer, Properties
The Startup Type is greyed out.
Last edited by Docfxit on 19 Apr 2023 18:23, edited 2 times in total.

Docfxit
Posts: 130
Joined: 12 Nov 2015 12:42

Re: Run Powershell from bat as Administrator

#8 Post by Docfxit » 19 Apr 2023 18:17

I figured out a solution.

This is the bat file I created:

Code: Select all

cd /d "%~dp0" && ( if exist "%Temp%\getadmin.vbs" del "%Temp%\getadmin.vbs") && fsutil dirty query %systemdrive% 1>nul 2>nul || ( echo Set UAC = CreateObject^("Shell.Application"^) : UAC.ShellExecute "cmd.exe", "/k cd ""%~sdp0"" && %~s0 %params%", "", "runas", 1 >> "%Temp%\getadmin.vbs" && "%Temp%\getadmin.vbs" && Exit /b)
%windir%\system32\reg.exe query "HKU\S-1-5-19" 1>nul 2>nul || ( echo. & echo  ERROR: This Batch file MUST be run in an ELEVATED cmd prompt [ Administrator ] & echo. & echo         Right-click the Batch file and click ^<Run as Administrator^>. & echo. & echo ^>Press ANY key to EXIT . . . & pause >nul & Exit )

SET ThisScriptsPath=%~dpn0

PowerShell.exe -ExecutionPolicy UnRestricted -File "%~dpn0.ps1" "%ThisScriptsPath%" 
This is the Powershell file:

Code: Select all

param($ThisScriptsPath)
commands from there and see what is different.
$Extention = "Transcript.txt"
$Path = $ThisScriptsPath + "" + $Extention
Start-Transcript -Path "$Path" -Force
$compname = $(hostname)
$service = Get-Service msiserver -Computername $compname
$Extention2 = "StartWinInstall.txt"
$Path = $ThisScriptsPath + "" + $Extention2
cmd /c 'reg add "HKLM\System\CurrentControlSet\Services\msiserver" /v "Start" /t REG_DWORD /d "3" /f' | Out-File $Path 
$service | Start-Service

do{
    Start-Sleep -seconds 3
    $service.Refresh()
}
until($service.Status -eq 'Running')

Post Reply