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.