mirror of
https://github.com/zoicware/RemoveWindowsAI.git
synced 2025-12-17 07:06:29 +01:00
fix error when executionpolicy is set via settings page fix #54
This commit is contained in:
@@ -961,10 +961,20 @@ foreach ($choice in $aipackages) {
|
||||
Set-ExecutionPolicy Unrestricted -Force -ErrorAction Stop
|
||||
}
|
||||
catch {
|
||||
#user has set powershell execution policy via group policy, to change it we need to update the registry
|
||||
$Global:ogExecutionPolicy = Get-ItemPropertyValue -Path 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\PowerShell' -Name 'ExecutionPolicy' -ErrorAction SilentlyContinue
|
||||
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
|
||||
#user has set powershell execution policy via group policy or via settings, to change it we need to update the registry
|
||||
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 '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,7 +2238,12 @@ else {
|
||||
|
||||
#set executionpolicy back to what it was
|
||||
if ($ogExecutionPolicy) {
|
||||
Reg.exe add 'HKLM\SOFTWARE\Policies\Microsoft\Windows\PowerShell' /v 'ExecutionPolicy' /t REG_SZ /d $ogExecutionPolicy /f >$null
|
||||
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
|
||||
}
|
||||
}
|
||||
Restart-Computer -Force
|
||||
}
|
||||
@@ -2272,7 +2287,12 @@ catch {}
|
||||
|
||||
#set executionpolicy back to what it was
|
||||
if ($ogExecutionPolicy) {
|
||||
Reg.exe add 'HKLM\SOFTWARE\Policies\Microsoft\Windows\PowerShell' /v 'ExecutionPolicy' /t REG_SZ /d $ogExecutionPolicy /f >$null
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
if (!$nonInteractive) {
|
||||
|
||||
Reference in New Issue
Block a user