Page 1 of 1

Run Powershell from bat as Administrator

Posted: 14 Apr 2023 23:33
by Docfxit
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?

Re: Run Powershell from bat as Administrator

Posted: 16 Apr 2023 12:17
by bxx
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.

Re: Run Powershell from bat as Administrator

Posted: 17 Apr 2023 04:46
by einstein1969
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

Re: Run Powershell from bat as Administrator

Posted: 17 Apr 2023 11:37
by Docfxit
I can't get the Powershell to work.

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

Re: Run Powershell from bat as Administrator

Posted: 17 Apr 2023 16:45
by Docfxit
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.

Re: Run Powershell from bat as Administrator

Posted: 19 Apr 2023 15:44
by einstein1969
On my pc with windows 10 it works.

Do you get the UAC prompt?

Re: Run Powershell from bat as Administrator

Posted: 19 Apr 2023 18:12
by Docfxit
No. I have it turned off.

On my Windows 10 22h2
In Services, Windows Installer, Properties
The Startup Type is greyed out.

Re: Run Powershell from bat as Administrator

Posted: 19 Apr 2023 18:17
by Docfxit
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')