0

I'm trying to write a script that installs the Linux Subsystem onto the PC. Here is the code:

function Get-ScriptDirectory { # Gets scripts full path
    Split-Path -parent $PSCommandPath
}

If (-NOT ([Security.Principal.WindowsPrincipal]
  [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
  [Security.Principal.WindowsBuiltInRole] "Administrator")) #checks if script is being run as admin
{
    Set-Location -Path $PSScriptRoot
    PowerShell -NoProfile -ExecutionPolicy Unrestricted -Command "&
    {
        Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy
        Unrestricted -File ""Get-ScriptDirectory""' -Verb RunAs}"; # Starts
            #Powershell with admin permissions and executes this script.
    Break
}
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux #Installs WSL

I had to take the if statement that checks if the script is being run with system administrator privileges (can't seem to find it again). I'm also running this within a VM on a Fedora host. I think the fact that it can't call itself because "running scripts is disabled on this system" to be very strange. I took out the if statement and everything runs fine and it installs WSL.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sol33t303
  • 105
  • 1
  • 7

1 Answers1

0

"Running scripts is disabled..." is an indication - almost certain - that the PowerShell execution policy is set to "Restricted". See Get-Help about_Execution_Policies.

To enable running scripts, you need to run a PowerShell session as administrator, and within that session, Set-ExecutionPolicy to an execution policy that will (a) provide you with adequate security for your needs, and (b) allow you to execute scripts that you're comfortable are safe. See Get-Help Set-ExecutionPolicy.

Jeff Zeitlin
  • 9,773
  • 2
  • 21
  • 33