fix error when executionpolicy is set via settings page fix #54

This commit is contained in:
zoicware
2025-11-30 01:41:34 -05:00
committed by GitHub
parent d40cd34349
commit 2430605a23

View File

@@ -961,10 +961,20 @@ foreach ($choice in $aipackages) {
Set-ExecutionPolicy Unrestricted -Force -ErrorAction Stop Set-ExecutionPolicy Unrestricted -Force -ErrorAction Stop
} }
catch { catch {
#user has set powershell execution policy via group policy, to change it we need to update the registry #user has set powershell execution policy via group policy or via settings, to change it we need to update the registry
$Global:ogExecutionPolicy = Get-ItemPropertyValue -Path 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\PowerShell' -Name 'ExecutionPolicy' -ErrorAction SilentlyContinue try {
$Global:ogExecutionPolicy = Get-ItemPropertyValue -Path 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\PowerShell' -Name 'ExecutionPolicy' -ErrorAction Stop
Reg.exe add 'HKLM\SOFTWARE\Policies\Microsoft\Windows\PowerShell' /v 'EnableScripts' /t REG_DWORD /d '1' /f >$null Reg.exe add 'HKLM\SOFTWARE\Policies\Microsoft\Windows\PowerShell' /v 'EnableScripts' /t REG_DWORD /d '1' /f >$null
Reg.exe add 'HKLM\SOFTWARE\Policies\Microsoft\Windows\PowerShell' /v 'ExecutionPolicy' /t REG_SZ /d 'Unrestricted' /f >$null Reg.exe add 'HKLM\SOFTWARE\Policies\Microsoft\Windows\PowerShell' /v 'ExecutionPolicy' /t REG_SZ /d 'Unrestricted' /f >$null
$Global:executionPolicyUser = $false
}
catch {
$Global:executionPolicyUser = $true
$Global:ogExecutionPolicy = Get-ItemPropertyValue -Path 'HKCU:\Software\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' -Name 'ExecutionPolicy'
Reg.exe add 'HKCU\Software\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' /v 'ExecutionPolicy' /t REG_SZ /d 'Unrestricted' /f >$null
}
} }
@@ -2228,8 +2238,13 @@ else {
#set executionpolicy back to what it was #set executionpolicy back to what it was
if ($ogExecutionPolicy) { if ($ogExecutionPolicy) {
if ($Global:executionPolicyUser) {
Reg.exe add 'HKCU\Software\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' /v 'ExecutionPolicy' /t REG_SZ /d $ogExecutionPolicy /f >$null
}
else {
Reg.exe add 'HKLM\SOFTWARE\Policies\Microsoft\Windows\PowerShell' /v 'ExecutionPolicy' /t REG_SZ /d $ogExecutionPolicy /f >$null Reg.exe add 'HKLM\SOFTWARE\Policies\Microsoft\Windows\PowerShell' /v 'ExecutionPolicy' /t REG_SZ /d $ogExecutionPolicy /f >$null
} }
}
Restart-Computer -Force Restart-Computer -Force
} }
@@ -2272,8 +2287,13 @@ catch {}
#set executionpolicy back to what it was #set executionpolicy back to what it was
if ($ogExecutionPolicy) { if ($ogExecutionPolicy) {
if ($Global:executionPolicyUser) {
Reg.exe add 'HKCU\Software\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' /v 'ExecutionPolicy' /t REG_SZ /d $ogExecutionPolicy /f >$null
}
else {
Reg.exe add 'HKLM\SOFTWARE\Policies\Microsoft\Windows\PowerShell' /v 'ExecutionPolicy' /t REG_SZ /d $ogExecutionPolicy /f >$null Reg.exe add 'HKLM\SOFTWARE\Policies\Microsoft\Windows\PowerShell' /v 'ExecutionPolicy' /t REG_SZ /d $ogExecutionPolicy /f >$null
} }
}
if (!$nonInteractive) { if (!$nonInteractive) {
Write-Host 'Done! Press Any Key to Exit...' -ForegroundColor Green Write-Host 'Done! Press Any Key to Exit...' -ForegroundColor Green