Reputation: 165
I am getting an error "Access is denied" when I try to run a Powershell script:
Set-Service : Service 'Windows Installer (msiserver)' cannot be configured due to the following error: Access is denied
At C:\Dnload\9xAddons\Wordperfect.ps1:4 char:12
+ $service | Set-Service -StartupType Manual
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (System.ServiceProcess.ServiceController:ServiceController) [Set-Servi
ce], ServiceCommandException
+ FullyQualifiedErrorId : CouldNotSetService,Microsoft.PowerShell.Commands.SetServiceCommand
Press Enter to continue...:
Press Enter to continue...:
PS C:\Dnload\9xAddons> Get-ExecutionPolicy -List
Scope ExecutionPolicy
----- ---------------
MachinePolicy Unrestricted
UserPolicy Undefined
Process Undefined
CurrentUser Unrestricted
LocalMachine Unrestricted
PS C:\Dnload\9xAddons> Get-ExecutionPolicy
Unrestricted
I'm running a .bat file to start the Powershell script:
@ECHO Off
setlocal EnableDelayedExpansion
set "params=%*"
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 )
%windir%\system32\msiexec.exe /unregister
%windir%\system32\msiexec.exe /regserver
%windir%\syswow64\msiexec.exe /unregister
%windir%\syswow64\msiexec.exe /regserver
(SET _KEY=SOFTWARE\Policies\Microsoft\Windows\PowerShell)
reg add "HKLM\%_KEY%" /v "ExecutionPolicy" /t REG_SZ /d "Unrestricted" /f >NUL
reg add "HKLM\%_KEY%" /v "EnableScripts" /t REG_DWORD /d 00000001 /F >NUL
:: I have rebooted after adding registry entries
powershell.exe -noprofile -NoExit -command "&{start-process powershell -ArgumentList '-NoExit -noprofile -file \"%~dpn0.ps1\"' -verb RunAs}"
I also tried:
PowerShell.exe -ExecutionPolicy Bypass -File Wordperfect.ps1
reg add "HKLM\%_KEY%" /v "ExecutionPolicy" /t REG_SZ /d "AllSigned" /f
reg add "HKLM\%_KEY%" /v "EnableScripts" /t REG_DWORD /d 00000000 /F >NUL
The Powershell script I am trying to run is:
Start-Transcript -Path "C:\Dnload\9xAddons\Wordperfect.txt" -Force
$compname = $(hostname)
$service = Get-Service msiserver -Computername $compname
cmd /c 'reg add "HKLM\System\CurrentControlSet\Services\msiserver" /v "Start" /t REG_DWORD /d "3" /f' | Out-File WordPerfectStartWinInstall.txt
$service | Start-Service
pause
do{
Start-Sleep -seconds 3
$service.Refresh()
}
until($service.Status -eq 'Running')
pause
The Powershell script path is:
C:\Dnload\9xAddons\WordPerfect.PS1
I'm running this in Windows 10 22H2
Currently I'm running Powershell version 5.1.19041.2673 Should I update it to 7.4?
Windows PowerShell transcript start
Start time: 20230418173314
Username: DOCFXITLT10\Gary
RunAs User: DOCFXITLT10\Gary
Configuration Name:
Machine: DOCFXITLT10 (Microsoft Windows NT 10.0.19045.0)
Host Application: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoExit -noprofile -file C:\Dnload\9xAddons\Wordperfect.ps1
Process ID: 16988
PSVersion: 5.1.19041.2673
PSEdition: Desktop
PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.19041.2673
BuildVersion: 10.0.19041.2673
CLRVersion: 4.0.30319.42000
WSManStackVersion: 3.0
PSRemotingProtocolVersion: 2.3
SerializationVersion: 1.1.0.1
**********************
Transcript started, output file is C:\Dnload\9xAddons\Wordperfect.txt
Set-Service : Service 'Windows Installer (msiserver)' cannot be configured due to the following error: Access is denied
At C:\Dnload\9xAddons\Wordperfect.ps1:10 char:12
+ $service | Set-Service -StartupType Manual
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (System.ServiceProcess.ServiceController:ServiceController)
[Set-Service], ServiceCommandException
+ FullyQualifiedErrorId : CouldNotSetService,Microsoft.PowerShell.Commands.SetServiceCommand
Set-Service : Service 'Windows Installer (msiserver)' cannot be configured due to the following error: Access is denied
At C:\Dnload\9xAddons\Wordperfect.ps1:10 char:12
+ $service | Set-Service -StartupType Manual
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (System.ServiceProcess.ServiceController:ServiceController) [Set-Servi
ce], ServiceCommandException
+ FullyQualifiedErrorId : CouldNotSetService,Microsoft.PowerShell.Commands.SetServiceCommand
Upvotes: 2
Views: 2138
Reputation: 165
I made the following changes to solve the Access Denied error message:
The batch file:
@echo off
setlocal EnableDelayedExpansion
set "params=%*"
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%"
The .PS1 file:
param($ThisScriptsPath)
$Extention = "Transcript.txt"
$Path = $ThisScriptsPath + "" + $Extention
Start-Transcript -Path "$Path" -Force
$compname = $(hostname)
$service = Get-Service msiserver -Computername $compname
$FileName = "StartWinInstall.txt"
$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')
Upvotes: 1