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.

1
  • Not a direct answer, but you could have a look at the tool called elevate :code.kliu.org/misc/elevate Worked for me in the past for several things. Commented Feb 19, 2018 at 9:48

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.