0

I'm trying to develop a VB.NET software that runs the following code in PowerShell:

Get-AppXPackage -AllUsers | Foreach {Add-AppxPackage -DisableDevelopmentMode -Register "$($_.InstallLocation)\AppXManifest.xml"} 

I've already created a .ps1 file, inserting this code and calling it in VisualStudio:

Shell("powershell -noexit Start-Process powershell -Verb runAs ""C:\Users\netov\Desktop\teste.ps1""", AppWinStyle.NormalFocus, True)

It runs, calls the PowerShell as Administrator, but I got the erro showing that The execution scripts was disabled in this system and said to check about about_Execution_Policies

I googled and I've found that if I run Set-ExecutionPolicy Unrestricted in PowerShell it allows me (after press Y to yes) runs the scripts.

The issue is I cannot run that code above into VS. I tried to add this Set-ExecutionPolicy Unrestricted in my file Teste.ps1, but didn't work out.

Someone already had this problem? Or if has another way to make that.

I'm trying to make it, because the Start Button in some computers with Windows 10 stops working. So I've thought in create a software to help some users.

netovgs
  • 51
  • 1
  • 7

1 Answers1

0

Pass -ExecutionPolicy into your Shell() call to powershell:

Shell("powershell -NoExit -ExecutionPolicy Unrestricted Start-Process powershell -Verb runAs ""ps1"", AppWinStyle.NormalFocus, True)

The alternative is actually using Set-ExecutionPolicy on the PC(s) you're trying to run the script as admin, or signing your scripts. Bypass or Unrestricted will work

Maximilian Burszley
  • 18,243
  • 4
  • 34
  • 63
  • I've already tried it as well. But with no success. Have you tried this code in your PC, it worked out ok? I've ran this code: "Shell("powershell -NoExit -ExecutionPolicy Unrestricted Start-Process powershell -Verb runAs ""C:\Users\netov\Desktop\teste.ps1""", AppWinStyle.NormalFocus, True)" . In teste.ps1 has just the code I've already sent. – netovgs Aug 11 '17 at 01:02