From 97ce57829d45ab82a6c34a6d915d5c6244e1bafd Mon Sep 17 00:00:00 2001 From: Dmitry Nefedov Date: Sat, 1 Apr 2023 18:10:51 +0300 Subject: [PATCH] Fixed a bug in Set-Association function --- sophia_script_versions.json | 4 +- .../Module/Sophia.psm1 | 90 ++++-------------- .../Functions.ps1 | 4 +- .../Manifest/Sophia.psd1 | 2 +- .../Module/Sophia.psm1 | 92 ++++--------------- .../Sophia.ps1 | 4 +- .../Module/Sophia.psm1 | 90 ++++-------------- .../Module/Sophia.psm1 | 90 ++++-------------- .../Module/Sophia.psm1 | 90 ++++-------------- 9 files changed, 98 insertions(+), 368 deletions(-) diff --git a/sophia_script_versions.json b/sophia_script_versions.json index 01b21565..42f55e06 100644 --- a/sophia_script_versions.json +++ b/sophia_script_versions.json @@ -1,8 +1,8 @@ { "Sophia_Script_Windows_10_PowerShell_5_1": "5.16.4", "Sophia_Script_Windows_10_PowerShell_7": "5.16.4", - "Sophia_Script_Windows_10_LTSC2019": "5.6.3", - "Sophia_Script_Windows_10_LTSC2021": "5.16.3", + "Sophia_Script_Windows_10_LTSC2019": "5.6.4", + "Sophia_Script_Windows_10_LTSC2021": "5.16.4", "Sophia_Script_Windows_11_PowerShell_5_1": "6.4.4", "Sophia_Script_Windows_11_PowerShell_7": "6.4.4", "Sophia_Script_Wrapper": "2.6.5" diff --git a/src/Sophia_Script_for_Windows_10/Module/Sophia.psm1 b/src/Sophia_Script_for_Windows_10/Module/Sophia.psm1 index 2e4453b7..2e542e24 100644 --- a/src/Sophia_Script_for_Windows_10/Module/Sophia.psm1 +++ b/src/Sophia_Script_for_Windows_10/Module/Sophia.psm1 @@ -8964,73 +8964,6 @@ public static int UnloadHive(RegistryHives hive, string subKey) Add-Type @Signature } - - function Set-Icon - { - Param - ( - [Parameter( - Mandatory = $true, - Position = 0 - - )] - [string] - $ProgId, - - [Parameter( - Mandatory = $true, - Position = 1 - )] - [string] - $Icon - ) - - if (-not (Test-Path -Path "HKCU:\Software\Classes\$ProgId\DefaultIcon")) - { - New-Item -Path "HKCU:\Software\Classes\$ProgId\DefaultIcon" -Force - } - New-ItemProperty -Path "HKCU:\Software\Classes\$ProgId\DefaultIcon" -Name "(default)" -PropertyType String -Value $Icon -Force - } - - function Remove-UserChoiceKey - { - Param - ( - [Parameter( - Mandatory = $true, - Position = 0 - )] - [string] - $SubKey - ) - - [WinAPI.Action]::DeleteKey([Microsoft.Win32.RegistryHive]::CurrentUser,$SubKey) - } - - function Set-UserAccessKey - { - Param - ( - [Parameter( - Mandatory = $true, - Position = 0 - )] - [string] - $SubKey - ) - - $OpenSubKey = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey($SubKey,'ReadWriteSubTree','TakeOwnership') - if ($OpenSubKey) - { - $Acl = [System.Security.AccessControl.RegistrySecurity]::new() - # Get current user SID - $UserSID = (Get-CimInstance -ClassName Win32_UserAccount | Where-Object -FilterScript {$_.Name -eq $env:USERNAME}).SID - $Acl.SetSecurityDescriptorSddlForm("O:$UserSID`G:$UserSID`D:AI(D;;DC;;;$UserSID)") - $OpenSubKey.SetAccessControl($Acl) - $OpenSubKey.Close() - } - } - function Write-ExtensionKeys { Param @@ -9070,7 +9003,7 @@ public static int UnloadHive(RegistryHives hive, string subKey) # If ProgId doesn't exist set the specified ProgId for the extensions # Due to "Set-StrictMode -Version Latest" we have to check everything - if (-not (Get-Variable -Name OrigProgID -ErrorAction Ignore)) + if (-not (Get-Variable -Name ProgId -ErrorAction Ignore)) { if (-not (Test-Path -Path "HKCU:\Software\Classes\$Extension")) { @@ -9104,7 +9037,7 @@ public static int UnloadHive(RegistryHives hive, string subKey) New-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\$Extension\OpenWithProgids" -Name $ProgID -PropertyType None -Value ([byte[]]@()) -Force # Removing the UserChoice key - Remove-UserChoiceKey -SubKey "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\$Extension\UserChoice" + [WinAPI.Action]::DeleteKey([Microsoft.Win32.RegistryHive]::CurrentUser, "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\$Extension\UserChoice") # Setting parameters in UserChoice. The key is being autocreated if (-not (Test-Path -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\$Extension\UserChoice")) @@ -9122,8 +9055,17 @@ public static int UnloadHive(RegistryHives hive, string subKey) } New-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\$Extension\UserChoice" -Name Hash -PropertyType String -Value $ProgHash -Force - # Setting a ban on changing the UserChoice section - Set-UserAccessKey -SubKey "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\$Extension\UserChoice" + # Setting a block on changing the UserChoice section + $OpenSubKey = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\$Extension\UserChoice", "ReadWriteSubTree", "TakeOwnership") + if ($OpenSubKey) + { + $Acl = [System.Security.AccessControl.RegistrySecurity]::new() + # Get current user SID + $UserSID = (Get-CimInstance -ClassName Win32_UserAccount | Where-Object -FilterScript {$_.Name -eq $env:USERNAME}).SID + $Acl.SetSecurityDescriptorSddlForm("O:$UserSID`G:$UserSID`D:AI(D;;DC;;;$UserSID)") + $OpenSubKey.SetAccessControl($Acl) + $OpenSubKey.Close() + } } function Write-AdditionalKeys @@ -9434,7 +9376,11 @@ public static long MakeLong(uint left, uint right) if ($Icon) { - Set-Icon -ProgId $ProgId -Icon $Icon + if (-not (Test-Path -Path "HKCU:\Software\Classes\$ProgId\DefaultIcon")) + { + New-Item -Path "HKCU:\Software\Classes\$ProgId\DefaultIcon" -Force + } + New-ItemProperty -Path "HKCU:\Software\Classes\$ProgId\DefaultIcon" -Name "(default)" -PropertyType String -Value $Icon -Force } Write-Information -MessageData "" -InformationAction Continue diff --git a/src/Sophia_Script_for_Windows_10_LTSC_2019/Functions.ps1 b/src/Sophia_Script_for_Windows_10_LTSC_2019/Functions.ps1 index 7c1942a9..f645126a 100644 --- a/src/Sophia_Script_for_Windows_10_LTSC_2019/Functions.ps1 +++ b/src/Sophia_Script_for_Windows_10_LTSC_2019/Functions.ps1 @@ -2,7 +2,7 @@ .SYNOPSIS The TAB completion for functions and their arguments - Version: v5.6.3 + Version: v5.6.4 Date: 01.04.2023 Copyright (c) 2014—2023 farag @@ -50,7 +50,7 @@ function Sophia Clear-Host -$Host.UI.RawUI.WindowTitle = "Sophia Script for Windows 10 LTSC 2019 v5.6.3 | Made with $([char]::ConvertFromUtf32(0x1F497)) of Windows 10 | $([char]0x00A9) farag & Inestic, 2014$([char]0x2013)2023" +$Host.UI.RawUI.WindowTitle = "Sophia Script for Windows 10 LTSC 2019 v5.6.4 | Made with $([char]::ConvertFromUtf32(0x1F497)) of Windows 10 | $([char]0x00A9) farag & Inestic, 2014$([char]0x2013)2023" Remove-Module -Name Sophia -Force -ErrorAction Ignore Import-Module -Name $PSScriptRoot\Manifest\Sophia.psd1 -PassThru -Force diff --git a/src/Sophia_Script_for_Windows_10_LTSC_2019/Manifest/Sophia.psd1 b/src/Sophia_Script_for_Windows_10_LTSC_2019/Manifest/Sophia.psd1 index 2cc648fe..05912847 100644 --- a/src/Sophia_Script_for_Windows_10_LTSC_2019/Manifest/Sophia.psd1 +++ b/src/Sophia_Script_for_Windows_10_LTSC_2019/Manifest/Sophia.psd1 @@ -1,6 +1,6 @@ @{ RootModule = '..\Module\Sophia.psm1' - ModuleVersion = '5.6.3' + ModuleVersion = '5.6.4' GUID = 'a36a65ca-70f9-43df-856c-3048fc5e7f01' Author = 'Dmitry "farag" Nefedov' Copyright = '(c) 2014—2023 farag & Inestic. All rights reserved' diff --git a/src/Sophia_Script_for_Windows_10_LTSC_2019/Module/Sophia.psm1 b/src/Sophia_Script_for_Windows_10_LTSC_2019/Module/Sophia.psm1 index 507535bb..f15c5477 100644 --- a/src/Sophia_Script_for_Windows_10_LTSC_2019/Module/Sophia.psm1 +++ b/src/Sophia_Script_for_Windows_10_LTSC_2019/Module/Sophia.psm1 @@ -2,7 +2,7 @@ .SYNOPSIS Sophia Script is a PowerShell module for Windows 10 & Windows 11 fine-tuning and automating the routine tasks - Version: v5.6.3 + Version: v5.6.4 Date: 01.04.2023 Copyright (c) 2014—2023 farag @@ -7296,73 +7296,6 @@ public static int UnloadHive(RegistryHives hive, string subKey) Add-Type @Signature } - - function Set-Icon - { - Param - ( - [Parameter( - Mandatory = $true, - Position = 0 - - )] - [string] - $ProgId, - - [Parameter( - Mandatory = $true, - Position = 1 - )] - [string] - $Icon - ) - - if (-not (Test-Path -Path "HKCU:\Software\Classes\$ProgId\DefaultIcon")) - { - New-Item -Path "HKCU:\Software\Classes\$ProgId\DefaultIcon" -Force - } - New-ItemProperty -Path "HKCU:\Software\Classes\$ProgId\DefaultIcon" -Name "(default)" -PropertyType String -Value $Icon -Force - } - - function Remove-UserChoiceKey - { - Param - ( - [Parameter( - Mandatory = $true, - Position = 0 - )] - [string] - $SubKey - ) - - [WinAPI.Action]::DeleteKey([Microsoft.Win32.RegistryHive]::CurrentUser,$SubKey) - } - - function Set-UserAccessKey - { - Param - ( - [Parameter( - Mandatory = $true, - Position = 0 - )] - [string] - $SubKey - ) - - $OpenSubKey = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey($SubKey,'ReadWriteSubTree','TakeOwnership') - if ($OpenSubKey) - { - $Acl = [System.Security.AccessControl.RegistrySecurity]::new() - # Get current user SID - $UserSID = (Get-CimInstance -ClassName Win32_UserAccount | Where-Object -FilterScript {$_.Name -eq $env:USERNAME}).SID - $Acl.SetSecurityDescriptorSddlForm("O:$UserSID`G:$UserSID`D:AI(D;;DC;;;$UserSID)") - $OpenSubKey.SetAccessControl($Acl) - $OpenSubKey.Close() - } - } - function Write-ExtensionKeys { Param @@ -7402,7 +7335,7 @@ public static int UnloadHive(RegistryHives hive, string subKey) # If ProgId doesn't exist set the specified ProgId for the extensions # Due to "Set-StrictMode -Version Latest" we have to check everything - if (-not (Get-Variable -Name OrigProgID -ErrorAction Ignore)) + if (-not (Get-Variable -Name ProgId -ErrorAction Ignore)) { if (-not (Test-Path -Path "HKCU:\Software\Classes\$Extension")) { @@ -7436,7 +7369,7 @@ public static int UnloadHive(RegistryHives hive, string subKey) New-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\$Extension\OpenWithProgids" -Name $ProgID -PropertyType None -Value ([byte[]]@()) -Force # Removing the UserChoice key - Remove-UserChoiceKey -SubKey "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\$Extension\UserChoice" + [WinAPI.Action]::DeleteKey([Microsoft.Win32.RegistryHive]::CurrentUser, "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\$Extension\UserChoice") # Setting parameters in UserChoice. The key is being autocreated if (-not (Test-Path -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\$Extension\UserChoice")) @@ -7454,8 +7387,17 @@ public static int UnloadHive(RegistryHives hive, string subKey) } New-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\$Extension\UserChoice" -Name Hash -PropertyType String -Value $ProgHash -Force - # Setting a ban on changing the UserChoice section - Set-UserAccessKey -SubKey "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\$Extension\UserChoice" + # Setting a block on changing the UserChoice section + $OpenSubKey = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\$Extension\UserChoice", "ReadWriteSubTree", "TakeOwnership") + if ($OpenSubKey) + { + $Acl = [System.Security.AccessControl.RegistrySecurity]::new() + # Get current user SID + $UserSID = (Get-CimInstance -ClassName Win32_UserAccount | Where-Object -FilterScript {$_.Name -eq $env:USERNAME}).SID + $Acl.SetSecurityDescriptorSddlForm("O:$UserSID`G:$UserSID`D:AI(D;;DC;;;$UserSID)") + $OpenSubKey.SetAccessControl($Acl) + $OpenSubKey.Close() + } } function Write-AdditionalKeys @@ -7766,7 +7708,11 @@ public static long MakeLong(uint left, uint right) if ($Icon) { - Set-Icon -ProgId $ProgId -Icon $Icon + if (-not (Test-Path -Path "HKCU:\Software\Classes\$ProgId\DefaultIcon")) + { + New-Item -Path "HKCU:\Software\Classes\$ProgId\DefaultIcon" -Force + } + New-ItemProperty -Path "HKCU:\Software\Classes\$ProgId\DefaultIcon" -Name "(default)" -PropertyType String -Value $Icon -Force } Write-Information -MessageData "" -InformationAction Continue diff --git a/src/Sophia_Script_for_Windows_10_LTSC_2019/Sophia.ps1 b/src/Sophia_Script_for_Windows_10_LTSC_2019/Sophia.ps1 index 1324eff7..ec9224e8 100644 --- a/src/Sophia_Script_for_Windows_10_LTSC_2019/Sophia.ps1 +++ b/src/Sophia_Script_for_Windows_10_LTSC_2019/Sophia.ps1 @@ -2,7 +2,7 @@ .SYNOPSIS Default preset file for "Sophia Script for Windows 10 LTSC 2019" - Version: v5.6.3 + Version: v5.6.4 Date: 01.04.2023 Copyright (c) 2014—2023 farag @@ -70,7 +70,7 @@ param Clear-Host -$Host.UI.RawUI.WindowTitle = "Sophia Script for Windows 10 LTSC 2019 v5.6.3 | Made with $([char]::ConvertFromUtf32(0x1F497)) of Windows | $([char]0x00A9) farag & Inestic, 2014$([char]0x2013)2023" +$Host.UI.RawUI.WindowTitle = "Sophia Script for Windows 10 LTSC 2019 v5.6.4 | Made with $([char]::ConvertFromUtf32(0x1F497)) of Windows | $([char]0x00A9) farag & Inestic, 2014$([char]0x2013)2023" Remove-Module -Name Sophia -Force -ErrorAction Ignore Import-Module -Name $PSScriptRoot\Manifest\Sophia.psd1 -PassThru -Force diff --git a/src/Sophia_Script_for_Windows_10_LTSC_2021/Module/Sophia.psm1 b/src/Sophia_Script_for_Windows_10_LTSC_2021/Module/Sophia.psm1 index 7f36b9dc..a90553b1 100644 --- a/src/Sophia_Script_for_Windows_10_LTSC_2021/Module/Sophia.psm1 +++ b/src/Sophia_Script_for_Windows_10_LTSC_2021/Module/Sophia.psm1 @@ -7921,73 +7921,6 @@ public static int UnloadHive(RegistryHives hive, string subKey) Add-Type @Signature } - - function Set-Icon - { - Param - ( - [Parameter( - Mandatory = $true, - Position = 0 - - )] - [string] - $ProgId, - - [Parameter( - Mandatory = $true, - Position = 1 - )] - [string] - $Icon - ) - - if (-not (Test-Path -Path "HKCU:\Software\Classes\$ProgId\DefaultIcon")) - { - New-Item -Path "HKCU:\Software\Classes\$ProgId\DefaultIcon" -Force - } - New-ItemProperty -Path "HKCU:\Software\Classes\$ProgId\DefaultIcon" -Name "(default)" -PropertyType String -Value $Icon -Force - } - - function Remove-UserChoiceKey - { - Param - ( - [Parameter( - Mandatory = $true, - Position = 0 - )] - [string] - $SubKey - ) - - [WinAPI.Action]::DeleteKey([Microsoft.Win32.RegistryHive]::CurrentUser,$SubKey) - } - - function Set-UserAccessKey - { - Param - ( - [Parameter( - Mandatory = $true, - Position = 0 - )] - [string] - $SubKey - ) - - $OpenSubKey = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey($SubKey,'ReadWriteSubTree','TakeOwnership') - if ($OpenSubKey) - { - $Acl = [System.Security.AccessControl.RegistrySecurity]::new() - # Get current user SID - $UserSID = (Get-CimInstance -ClassName Win32_UserAccount | Where-Object -FilterScript {$_.Name -eq $env:USERNAME}).SID - $Acl.SetSecurityDescriptorSddlForm("O:$UserSID`G:$UserSID`D:AI(D;;DC;;;$UserSID)") - $OpenSubKey.SetAccessControl($Acl) - $OpenSubKey.Close() - } - } - function Write-ExtensionKeys { Param @@ -8027,7 +7960,7 @@ public static int UnloadHive(RegistryHives hive, string subKey) # If ProgId doesn't exist set the specified ProgId for the extensions # Due to "Set-StrictMode -Version Latest" we have to check everything - if (-not (Get-Variable -Name OrigProgID -ErrorAction Ignore)) + if (-not (Get-Variable -Name ProgId -ErrorAction Ignore)) { if (-not (Test-Path -Path "HKCU:\Software\Classes\$Extension")) { @@ -8061,7 +7994,7 @@ public static int UnloadHive(RegistryHives hive, string subKey) New-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\$Extension\OpenWithProgids" -Name $ProgID -PropertyType None -Value ([byte[]]@()) -Force # Removing the UserChoice key - Remove-UserChoiceKey -SubKey "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\$Extension\UserChoice" + [WinAPI.Action]::DeleteKey([Microsoft.Win32.RegistryHive]::CurrentUser, "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\$Extension\UserChoice") # Setting parameters in UserChoice. The key is being autocreated if (-not (Test-Path -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\$Extension\UserChoice")) @@ -8079,8 +8012,17 @@ public static int UnloadHive(RegistryHives hive, string subKey) } New-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\$Extension\UserChoice" -Name Hash -PropertyType String -Value $ProgHash -Force - # Setting a ban on changing the UserChoice section - Set-UserAccessKey -SubKey "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\$Extension\UserChoice" + # Setting a block on changing the UserChoice section + $OpenSubKey = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\$Extension\UserChoice", "ReadWriteSubTree", "TakeOwnership") + if ($OpenSubKey) + { + $Acl = [System.Security.AccessControl.RegistrySecurity]::new() + # Get current user SID + $UserSID = (Get-CimInstance -ClassName Win32_UserAccount | Where-Object -FilterScript {$_.Name -eq $env:USERNAME}).SID + $Acl.SetSecurityDescriptorSddlForm("O:$UserSID`G:$UserSID`D:AI(D;;DC;;;$UserSID)") + $OpenSubKey.SetAccessControl($Acl) + $OpenSubKey.Close() + } } function Write-AdditionalKeys @@ -8391,7 +8333,11 @@ public static long MakeLong(uint left, uint right) if ($Icon) { - Set-Icon -ProgId $ProgId -Icon $Icon + if (-not (Test-Path -Path "HKCU:\Software\Classes\$ProgId\DefaultIcon")) + { + New-Item -Path "HKCU:\Software\Classes\$ProgId\DefaultIcon" -Force + } + New-ItemProperty -Path "HKCU:\Software\Classes\$ProgId\DefaultIcon" -Name "(default)" -PropertyType String -Value $Icon -Force } Write-Information -MessageData "" -InformationAction Continue diff --git a/src/Sophia_Script_for_Windows_11/Module/Sophia.psm1 b/src/Sophia_Script_for_Windows_11/Module/Sophia.psm1 index f2d0d735..7281dfa9 100644 --- a/src/Sophia_Script_for_Windows_11/Module/Sophia.psm1 +++ b/src/Sophia_Script_for_Windows_11/Module/Sophia.psm1 @@ -8481,73 +8481,6 @@ public static int UnloadHive(RegistryHives hive, string subKey) Add-Type @Signature } - - function Set-Icon - { - Param - ( - [Parameter( - Mandatory = $true, - Position = 0 - - )] - [string] - $ProgId, - - [Parameter( - Mandatory = $true, - Position = 1 - )] - [string] - $Icon - ) - - if (-not (Test-Path -Path "HKCU:\Software\Classes\$ProgId\DefaultIcon")) - { - New-Item -Path "HKCU:\Software\Classes\$ProgId\DefaultIcon" -Force - } - New-ItemProperty -Path "HKCU:\Software\Classes\$ProgId\DefaultIcon" -Name "(default)" -PropertyType String -Value $Icon -Force - } - - function Remove-UserChoiceKey - { - Param - ( - [Parameter( - Mandatory = $true, - Position = 0 - )] - [string] - $SubKey - ) - - [WinAPI.Action]::DeleteKey([Microsoft.Win32.RegistryHive]::CurrentUser,$SubKey) - } - - function Set-UserAccessKey - { - Param - ( - [Parameter( - Mandatory = $true, - Position = 0 - )] - [string] - $SubKey - ) - - $OpenSubKey = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey($SubKey,'ReadWriteSubTree','TakeOwnership') - if ($OpenSubKey) - { - $Acl = [System.Security.AccessControl.RegistrySecurity]::new() - # Get current user SID - $UserSID = (Get-CimInstance -ClassName Win32_UserAccount | Where-Object -FilterScript {$_.Name -eq $env:USERNAME}).SID - $Acl.SetSecurityDescriptorSddlForm("O:$UserSID`G:$UserSID`D:AI(D;;DC;;;$UserSID)") - $OpenSubKey.SetAccessControl($Acl) - $OpenSubKey.Close() - } - } - function Write-ExtensionKeys { Param @@ -8587,7 +8520,7 @@ public static int UnloadHive(RegistryHives hive, string subKey) # If ProgId doesn't exist set the specified ProgId for the extensions # Due to "Set-StrictMode -Version Latest" we have to check everything - if (-not (Get-Variable -Name OrigProgID -ErrorAction Ignore)) + if (-not (Get-Variable -Name ProgId -ErrorAction Ignore)) { if (-not (Test-Path -Path "HKCU:\Software\Classes\$Extension")) { @@ -8621,7 +8554,7 @@ public static int UnloadHive(RegistryHives hive, string subKey) New-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\$Extension\OpenWithProgids" -Name $ProgID -PropertyType None -Value ([byte[]]@()) -Force # Removing the UserChoice key - Remove-UserChoiceKey -SubKey "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\$Extension\UserChoice" + [WinAPI.Action]::DeleteKey([Microsoft.Win32.RegistryHive]::CurrentUser, "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\$Extension\UserChoice") # Setting parameters in UserChoice. The key is being autocreated if (-not (Test-Path -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\$Extension\UserChoice")) @@ -8639,8 +8572,17 @@ public static int UnloadHive(RegistryHives hive, string subKey) } New-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\$Extension\UserChoice" -Name Hash -PropertyType String -Value $ProgHash -Force - # Setting a ban on changing the UserChoice section - Set-UserAccessKey -SubKey "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\$Extension\UserChoice" + # Setting a block on changing the UserChoice section + $OpenSubKey = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\$Extension\UserChoice", "ReadWriteSubTree", "TakeOwnership") + if ($OpenSubKey) + { + $Acl = [System.Security.AccessControl.RegistrySecurity]::new() + # Get current user SID + $UserSID = (Get-CimInstance -ClassName Win32_UserAccount | Where-Object -FilterScript {$_.Name -eq $env:USERNAME}).SID + $Acl.SetSecurityDescriptorSddlForm("O:$UserSID`G:$UserSID`D:AI(D;;DC;;;$UserSID)") + $OpenSubKey.SetAccessControl($Acl) + $OpenSubKey.Close() + } } function Write-AdditionalKeys @@ -8951,7 +8893,11 @@ public static long MakeLong(uint left, uint right) if ($Icon) { - Set-Icon -ProgId $ProgId -Icon $Icon + if (-not (Test-Path -Path "HKCU:\Software\Classes\$ProgId\DefaultIcon")) + { + New-Item -Path "HKCU:\Software\Classes\$ProgId\DefaultIcon" -Force + } + New-ItemProperty -Path "HKCU:\Software\Classes\$ProgId\DefaultIcon" -Name "(default)" -PropertyType String -Value $Icon -Force } Write-Information -MessageData "" -InformationAction Continue diff --git a/src/Sophia_Script_for_Windows_11_PowerShell_7/Module/Sophia.psm1 b/src/Sophia_Script_for_Windows_11_PowerShell_7/Module/Sophia.psm1 index f6b6be28..6582c627 100644 --- a/src/Sophia_Script_for_Windows_11_PowerShell_7/Module/Sophia.psm1 +++ b/src/Sophia_Script_for_Windows_11_PowerShell_7/Module/Sophia.psm1 @@ -8489,73 +8489,6 @@ public static int UnloadHive(RegistryHives hive, string subKey) Add-Type @Signature } - - function Set-Icon - { - Param - ( - [Parameter( - Mandatory = $true, - Position = 0 - - )] - [string] - $ProgId, - - [Parameter( - Mandatory = $true, - Position = 1 - )] - [string] - $Icon - ) - - if (-not (Test-Path -Path "HKCU:\Software\Classes\$ProgId\DefaultIcon")) - { - New-Item -Path "HKCU:\Software\Classes\$ProgId\DefaultIcon" -Force - } - New-ItemProperty -Path "HKCU:\Software\Classes\$ProgId\DefaultIcon" -Name "(default)" -PropertyType String -Value $Icon -Force - } - - function Remove-UserChoiceKey - { - Param - ( - [Parameter( - Mandatory = $true, - Position = 0 - )] - [string] - $SubKey - ) - - [WinAPI.Action]::DeleteKey([Microsoft.Win32.RegistryHive]::CurrentUser,$SubKey) - } - - function Set-UserAccessKey - { - Param - ( - [Parameter( - Mandatory = $true, - Position = 0 - )] - [string] - $SubKey - ) - - $OpenSubKey = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey($SubKey,'ReadWriteSubTree','TakeOwnership') - if ($OpenSubKey) - { - $Acl = [System.Security.AccessControl.RegistrySecurity]::new() - # Get current user SID - $UserSID = (Get-CimInstance -ClassName Win32_UserAccount | Where-Object -FilterScript {$_.Name -eq $env:USERNAME}).SID - $Acl.SetSecurityDescriptorSddlForm("O:$UserSID`G:$UserSID`D:AI(D;;DC;;;$UserSID)") - $OpenSubKey.SetAccessControl($Acl) - $OpenSubKey.Close() - } - } - function Write-ExtensionKeys { Param @@ -8595,7 +8528,7 @@ public static int UnloadHive(RegistryHives hive, string subKey) # If ProgId doesn't exist set the specified ProgId for the extensions # Due to "Set-StrictMode -Version Latest" we have to check everything - if (-not (Get-Variable -Name OrigProgID -ErrorAction Ignore)) + if (-not (Get-Variable -Name ProgId -ErrorAction Ignore)) { if (-not (Test-Path -Path "HKCU:\Software\Classes\$Extension")) { @@ -8629,7 +8562,7 @@ public static int UnloadHive(RegistryHives hive, string subKey) New-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\$Extension\OpenWithProgids" -Name $ProgID -PropertyType None -Value ([byte[]]@()) -Force # Removing the UserChoice key - Remove-UserChoiceKey -SubKey "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\$Extension\UserChoice" + [WinAPI.Action]::DeleteKey([Microsoft.Win32.RegistryHive]::CurrentUser, "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\$Extension\UserChoice") # Setting parameters in UserChoice. The key is being autocreated if (-not (Test-Path -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\$Extension\UserChoice")) @@ -8647,8 +8580,17 @@ public static int UnloadHive(RegistryHives hive, string subKey) } New-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\$Extension\UserChoice" -Name Hash -PropertyType String -Value $ProgHash -Force - # Setting a ban on changing the UserChoice section - Set-UserAccessKey -SubKey "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\$Extension\UserChoice" + # Setting a block on changing the UserChoice section + $OpenSubKey = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\$Extension\UserChoice", "ReadWriteSubTree", "TakeOwnership") + if ($OpenSubKey) + { + $Acl = [System.Security.AccessControl.RegistrySecurity]::new() + # Get current user SID + $UserSID = (Get-CimInstance -ClassName Win32_UserAccount | Where-Object -FilterScript {$_.Name -eq $env:USERNAME}).SID + $Acl.SetSecurityDescriptorSddlForm("O:$UserSID`G:$UserSID`D:AI(D;;DC;;;$UserSID)") + $OpenSubKey.SetAccessControl($Acl) + $OpenSubKey.Close() + } } function Write-AdditionalKeys @@ -8959,7 +8901,11 @@ public static long MakeLong(uint left, uint right) if ($Icon) { - Set-Icon -ProgId $ProgId -Icon $Icon + if (-not (Test-Path -Path "HKCU:\Software\Classes\$ProgId\DefaultIcon")) + { + New-Item -Path "HKCU:\Software\Classes\$ProgId\DefaultIcon" -Force + } + New-ItemProperty -Path "HKCU:\Software\Classes\$ProgId\DefaultIcon" -Name "(default)" -PropertyType String -Value $Icon -Force } Write-Information -MessageData "" -InformationAction Continue