Trying to run Powershell

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Posts: 66
Joined: 12 Nov 2015 12:42

Trying to run Powershell

#1 Post by Docfxit » 20 Oct 2019 20:05

I have a batch script to create an entry in Task Schedule, that seems to work except nothing gets written to the $Logfile. I am running this with the Administrator user. (Not a user that has administrator privileges)

Code: Select all

:: This script name is Reset-AppX.cmd
::Create for log file in Fix-Sysprep.ps1
If not exist C:\Temp md C:\Temp
Set $Logfile = "C:\Temp\Fix-Sysprep.log"
REM Fail if script is run as ANYONE other than Administrator
IF NOT '%username%' == 'Administrator' GOTO AdminError
REM Create a Scheduled Task that fires when the computer starts. The script called by the
REM Scheduled Task handles deleting the Scheduled Task if it is successful.
schtasks /create /RU "SYSTEM" /NP /SC ONSTART /TN Reset-AppX /TR "PowerShell.exe -File "C:\Dnload\Fix-Sysprep.ps1"" /F 2>$Logfile
ECHO SYSPREP FIX FAILED: Not logged in as Administrator
ECHO --------------------------------------------------
ECHO %username% is not the same as Administrator.
cmd /k
The Powershell file Fix-Sysprep.ps1 is now running. After I run the above batch file I reboot into the Administrator user. It does now write out the log file. 1. It's getting an error Access is denied when it tries to disable the StateRepository service. I have disabled the service manually for now.
2. I'm getting Access is denied when it tries to rename the files. The files are owned by Administrator. The files have full control for Administrator. I am signed on as Administrator. The script runs during re-boot when the files are not in use.
3. Very little is being written to the log file.

Code: Select all

# Fix-Sysprep.ps1 Script to rename the AppX database that can prevent Sysprep from running
# so that Windows will rebuild the AppX databse from scratch the the correct
# information and thus allowing Sysprep to complete successfully.
# Stop the service

$Logfile = "C:\Temp\Fix-Sysprep.log"
If (Test-Path $Logfile)
Remove-item -path $Logfile
Stop-Service -Name "StateRepository" -Force | Out-File -FilePath $Logfile -Append -Encoding ascii
# Disable the State Repository Service so that the files can be renamed
#Set-Service -Name "StateRepository" -StartupType Disabled | Out-File -FilePath $Logfile -Append -Encoding ascii
# Export the existing permissions
icacls C:\ProgramData\Microsoft\Windows\AppRepository /save AclFile /T | Out-File -FilePath $Logfile -Append -Encoding ascii
# Take ownership of specific files
takeown /F C:\ProgramData\Microsoft\Windows\AppRepository\StateRepository-Deployment.srd | Out-File -FilePath $Logfile -Append -Encoding ascii
takeown /F C:\ProgramData\Microsoft\Windows\AppRepository\StateRepository-Deployment.srd-shm | Out-File -FilePath $Logfile -Append -Encoding ascii
takeown /F C:\ProgramData\Microsoft\Windows\AppRepository\StateRepository-Deployment.srd-wal | Out-File -FilePath $Logfile -Append -Encoding ascii
takeown /F C:\ProgramData\Microsoft\Windows\AppRepository\StateRepository-Machine.srd | Out-File -FilePath $Logfile -Append -Encoding ascii
takeown /F C:\ProgramData\Microsoft\Windows\AppRepository\StateRepository-Machine.srd-shm | Out-File -FilePath $Logfile -Append -Encoding ascii
takeown /F C:\ProgramData\Microsoft\Windows\AppRepository\StateRepository-Machine.srd-wal | Out-File -FilePath $Logfile -Append -Encoding ascii
icacls "C:\ProgramData\Microsoft\Windows\AppRepository\StateRepository-Deployment.srd" /inheritance:d 
icacls "C:\ProgramData\Microsoft\Windows\AppRepository\StateRepository-Deployment.srd-shm" /inheritance:d 
icacls "C:\ProgramData\Microsoft\Windows\AppRepository\StateRepository-Deployment.srd-wal" /inheritance:d 
icacls "C:\ProgramData\Microsoft\Windows\AppRepository\StateRepository-Machine.srd" /inheritance:d 
icacls "C:\ProgramData\Microsoft\Windows\AppRepository\StateRepository-Machine.srd-shm" /inheritance:d 
icacls "C:\ProgramData\Microsoft\Windows\AppRepository\StateRepository-Machine.srd-wal" /inheritance:d
icacls "C:\ProgramData\Microsoft\Windows\AppRepository\StateRepository-Deployment.srd"  /grant Administrator:"F" 
icacls "C:\ProgramData\Microsoft\Windows\AppRepository\StateRepository-Deployment.srd-shm"  /grant Administrator:"F"
icacls "C:\ProgramData\Microsoft\Windows\AppRepository\StateRepository-Deployment.srd-wal"  /grant Administrator:"F"
icacls "C:\ProgramData\Microsoft\Windows\AppRepository\StateRepository-Machine.srd"  /grant Administrator:"F"
icacls "C:\ProgramData\Microsoft\Windows\AppRepository\StateRepository-Machine.srd-shm"  /grant Administrator:"F"
icacls "C:\ProgramData\Microsoft\Windows\AppRepository\StateRepository-Deployment.srd-wal"  /grant Administrator:"F"
# Rename the files
Rename-Item C:\ProgramData\Microsoft\Windows\AppRepository\StateRepository-Deployment.srd C:\ProgramData\Microsoft\Windows\AppRepository\StateRepository-Deployment_corrupted.srd -Force | Out-File -FilePath $Logfile -Append -Encoding ascii
Rename-Item C:\ProgramData\Microsoft\Windows\AppRepository\StateRepository-Deployment.srd-shm C:\ProgramData\Microsoft\Windows\AppRepository\StateRepository-Deployment_corrupted.srd-shm -Force | Out-File -FilePath $Logfile -Append -Encoding ascii
Rename-Item C:\ProgramData\Microsoft\Windows\AppRepository\StateRepository-Deployment.srd-wal C:\ProgramData\Microsoft\Windows\AppRepository\StateRepository-Deployment_corrupted.srd-wal -Force | Out-File -FilePath $Logfile -Append -Encoding ascii
Rename-Item C:\ProgramData\Microsoft\Windows\AppRepository\StateRepository-Machine.srd C:\ProgramData\Microsoft\Windows\AppRepository\StateRepository-Machine_corrupted.srd -Force | Out-File -FilePath $Logfile -Append -Encoding ascii
Rename-Item C:\ProgramData\Microsoft\Windows\AppRepository\StateRepository-Machine.srd-shm C:\ProgramData\Microsoft\Windows\AppRepository\StateRepository-Machine_corrupted.srd-shm -Force | Out-File -FilePath $Logfile -Append -Encoding ascii
Rename-Item C:\ProgramData\Microsoft\Windows\AppRepository\StateRepository-Machine.srd-wal C:\ProgramData\Microsoft\Windows\AppRepository\StateRepository-Machine_corrupted.srd-wal -Force | Out-File -FilePath $Logfile -Append -Encoding ascii
# Import the permissions back
icacls C:\ProgramData\Microsoft\Windows\AppRepository /restore AclFile | Out-File -FilePath $Logfile -Append -Encoding ascii
# Re-Enable the service
Set-Service -Name "StateRepository" -StartupType Manual | Out-File -FilePath $Logfile -Append -Encoding ascii
# Re-Start the State Repository Service
Start-Service -Name "StateRepository" | Out-File -FilePath $Logfile -Append -Encoding ascii
# Try and verify that everything is really working
if (Test-Path C:\ProgramData\Microsoft\Windows\AppRepository\StateRepository-Deployment_corrupted.srd)
if (Test-Path C:\ProgramData\Microsoft\Windows\AppRepository\StateRepository-Machine_corrupted.srd)
# If the two database files have been renamed and the service is running again then delete the scheduled task
if ((Get-Service -Name "StateRepository").Status -eq "Running")
# Delete the scheduled task
schtasks /delete /TN Reset-AppX /F | Out-File -FilePath $Logfile -Append -Encoding ascii

Does anyone have any idea why the service StateRepository is getting Access Denied when the script tries to disable it?


I'm running this in Windows 10 1903 Pro

Post Reply