Run Powershell from bat as Administrator
Posted: 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.
I created a .ps1 file called Wordperfect.ps1
I'm getting a powershell window saying Access is denied
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?
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}"
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
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
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?