私はpowershellスクリプト(およびプログラミング/ジェネラルのスクリプト)には新しく、これは初めてのPowerShellスクリプトです。私はPCにLinux Subsystemをインストールするスクリプトを書こうとしています。ここで私は、スクリプトが管理privliges(再びそれを見つけるように見えることはできません)で実行されているかどうかを確認するif文を取らなければならなかったコードPowershellスクリプトは、 "実行中のスクリプトがこのシステムでは無効になっている"ため、自己を呼び出すことはできません。
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
です。また、これをFedoraホストのVM内で実行しています。私は、 "スクリプトを実行することはこのシステムでは無効になっている"ということはとても奇妙に思えるので、それは自分自身だとは思えません。私はif文を取り出し、すべて正常に動作し、WSLをインストールします。