4

I wrote a PowerShell script to give run access to run without admin privilege. So I need to run that script from batch file. Here I attached my PowerShell script and my batch file. I am not able to run the PowerShell script from my batch file.

Access.ps1

powershell -File "%~dpn0.ps1" %*
Start-Process PowerShell -ArgumentList "Set-ExecutionPolicy Unrestricted -Force" -Verb RunAs
$key = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey("SOFTWARE\Wow6432Node\Unicorn",[Microsoft.Win32.RegistryKeyPermissionCheck]::ReadWriteSubTree,[System.Security.AccessControl.RegistryRights]::ChangePermissions)
$acl = $key.GetAccessControl()
$rule = New-Object System.Security.AccessControl.RegistryAccessRule (".\USERS","FullControl",@("ObjectInherit","ContainerInherit"),"None","Allow")
$acl.SetAccessRule($rule)
$key.SetAccessControl($acl)

Write-Host "Successfully set permission to PM Registry!"

Access.bat

@ECHO OFF
PowerShell.exe -Command "& '%~dpn0.ps1'"
PAUSE

This is the error

enter image description here

stackprotector
  • 10,498
  • 4
  • 35
  • 64
Lasal Senarath
  • 149
  • 1
  • 13
  • Does `Access.ps1` really start with `powershell -File "%~dpn0.ps1" %*`, or is that a copy-paste error? Please [edit] the question if it's an error. Also, what happens? Is there an error message? Nothing? Wrong output? – vonPryz Dec 17 '20 at 07:16
  • @vonPryz updated error – Lasal Senarath Dec 17 '20 at 07:24

1 Answers1

2

On your system, the execution of PowerShell scripts is not allowed. Either allow it, by executing (with administrative privileges):

Set-ExecutionPolicy RemoteSigned

Or bypass it (in your .bat):

PowerShell.exe -ExecutionPolicy Bypass -File .\Access.ps1

Inside Access.ps1, the following line is pretty much useless:

Start-Process PowerShell -ArgumentList "Set-ExecutionPolicy Unrestricted -Force" -Verb RunAs

as you already need permissions to execute scripts to run this script.

stackprotector
  • 10,498
  • 4
  • 35
  • 64
  • I followed your second option then I have the below output The argument '.\Access.ps1' to the -File parameter does not exist. Provide the path to an existing '.ps1' file as an argument to the -File parameter. @Thomas – Lasal Senarath Dec 17 '20 at 08:12
  • PSVersion 5.1.18362.145 – Lasal Senarath Dec 17 '20 at 08:18
  • @LasalSenarath Well, you have to provide the correct path to your script, of course. `.\Access.ps1` is a relative path and means, that this file is expected to be inside your working directory. – stackprotector Dec 17 '20 at 08:18
  • Both files(.bat and .ps1) are in same directory as other dll files. The last line of .ps can be seen in output. Can you give me an example to put an absolute path inside bat file @Thomas – Lasal Senarath Dec 17 '20 at 08:33
  • This directory should then also be your working directory. An absolute path looks like this: `C:\Users\LasalSenarath\Desktop\scripts\Access.ps1`. – stackprotector Dec 17 '20 at 08:59
  • It is not working below output happened @Thomas The argument '%~dpn0.ps1' to the -File parameter does not exist. Provide the path to an existing '.ps1' file as an argument to the -File parameter. – Lasal Senarath Dec 17 '20 at 09:07
  • 1
    It is not a problem I put "PowerShell -File "%~dpn0.ps1" %*" inside .ps1 file so both relative and absolute path is working this solution. Thank you @Thomas – Lasal Senarath Dec 17 '20 at 09:26