0

I have written a small Power Shell script which is installing certificate and appxupload file into the system but the problem is I want whenever user run the script it should run as admin mode.

my script is:

$CertLocation =  Get-ChildItem -Recurse | Where-Object{$_.Name -like "*.cer"}
Set-Location $CertLocation.Directory 
certutil.exe -addstore TrustedPeople $CertLocation.Name
$AppPackage =  Get-ChildItem -Recurse | Where-Object{$_.Name -like "*.appxbundle"}
Add-AppxPackage -Path $AppPackage.Name

Please help me with the query.

VIVEK
  • 257
  • 1
  • 5
  • 18
  • Not a direct answer, but you could have a look at the tool called `elevate` :http://code.kliu.org/misc/elevate/ Worked for me in the past for several things. – Jan_V Feb 19 '18 at 09:48

1 Answers1

0

Add the following lines at the beginning of a script.

If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))

{   
$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
Break
}

Here is the SO link for the same.

Vivek Kumar Singh
  • 3,223
  • 1
  • 14
  • 27