Files
2026-03-10 19:07:03 +01:00

205 lines
8.0 KiB
PowerShell

<#
.SYNOPSIS
Post actions
.VERSION
7.1.4
.DATE
24.02.2026
.COPYRIGHT
(c) 2014—2026 Team Sophia
.LINK
https://github.com/farag2/Sophia-Script-for-Windows
#>
function PostActions
{
#region Refresh Environment
$Signature = @{
Namespace = "WinAPI"
Name = "UpdateEnvironment"
Language = "CSharp"
CompilerOptions = $CompilerOptions
MemberDefinition = @"
private static readonly IntPtr HWND_BROADCAST = new IntPtr(0xffff);
private const int WM_SETTINGCHANGE = 0x1a;
private const int SMTO_ABORTIFHUNG = 0x0002;
[DllImport("shell32.dll", CharSet = CharSet.Auto, SetLastError = false)]
private static extern int SHChangeNotify(int eventId, int flags, IntPtr item1, IntPtr item2);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
private static extern IntPtr SendMessageTimeout(IntPtr hWnd, int Msg, IntPtr wParam, string lParam, int fuFlags, int uTimeout, IntPtr lpdwResult);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
static extern bool SendNotifyMessage(IntPtr hWnd, uint Msg, IntPtr wParam, string lParam);
public static void Refresh()
{
// Update desktop icons
SHChangeNotify(0x8000000, 0x1000, IntPtr.Zero, IntPtr.Zero);
// Update environment variables
SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, IntPtr.Zero, null, SMTO_ABORTIFHUNG, 100, IntPtr.Zero);
// Update taskbar
SendNotifyMessage(HWND_BROADCAST, WM_SETTINGCHANGE, IntPtr.Zero, "TraySettings");
}
private static readonly IntPtr hWnd = new IntPtr(65535);
private const int Msg = 273;
// Virtual key ID of the F5 in File Explorer
private static readonly UIntPtr UIntPtr = new UIntPtr(41504);
[DllImport("user32.dll", SetLastError=true)]
public static extern int PostMessageW(IntPtr hWnd, uint Msg, UIntPtr wParam, IntPtr lParam);
public static void PostMessage()
{
// Simulate pressing F5 to refresh the desktop
PostMessageW(hWnd, Msg, UIntPtr, IntPtr.Zero);
}
"@
}
if (-not ("WinAPI.UpdateEnvironment" -as [type]))
{
Add-Type @Signature
}
# Simulate pressing F5 to refresh the desktop
[WinAPI.UpdateEnvironment]::PostMessage()
# Refresh desktop icons, environment variables, taskbar
[WinAPI.UpdateEnvironment]::Refresh()
# Restart Start menu
Stop-Process -Name StartMenuExperienceHost -Force -ErrorAction Ignore
#endregion Refresh Environment
#region Other actions
# Kill all explorer instances in case "launch folder windows in a separate process" enabled
Get-Process -Name explorer | Stop-Process -Force
Start-Sleep -Seconds 3
# Restoring closed folders
if (Get-Variable -Name OpenedFolder -ErrorAction Ignore)
{
foreach ($Global:OpenedFolder in $Global:OpenedFolders)
{
if (Test-Path -Path $Global:OpenedFolder)
{
Start-Process -FilePath "$env:SystemRoot\explorer.exe" -ArgumentList $Global:OpenedFolder
}
}
}
# Checking whether any of scheduled tasks were created. Unless open Task Scheduler
if ($Global:ScheduledTasks)
{
# Find and close taskschd.msc by its argument
$taskschd_Process_ID = (Get-CimInstance -ClassName CIM_Process | Where-Object -FilterScript {$_.Name -eq "mmc.exe"} | Where-Object -FilterScript {
$_.CommandLine -match "taskschd.msc"
}).Handle
# We have to check before executing due to "Set-StrictMode -Version Latest"
if ($taskschd_Process_ID)
{
Get-Process -Id $taskschd_Process_ID | Stop-Process -Force
}
# Open Task Scheduler
Start-Process -FilePath taskschd.msc
$Global:ScheduledTasks = $false
}
# Apply policies found in registry to re-build database database because gpedit.msc relies in its own database
if (Test-Path -Path "$env:TEMP\LGPO.txt")
{
& "$PSScriptRoot\..\..\Binaries\LGPO.exe" /t "$env:TEMP\LGPO.txt"
& "$env:SystemRoot\System32\gpupdate.exe" /force
}
# PowerShell 5.1 (7.5 too) interprets 8.3 file name literally, if an environment variable contains a non-Latin word
# https://github.com/PowerShell/PowerShell/issues/21070
Get-ChildItem -Path "$env:TEMP\LGPO.txt" -Force -ErrorAction Ignore | Remove-Item -Force -ErrorAction Ignore
#endregion Other actions
#region Toast notifications
# Enable notifications
Remove-ItemProperty -Path HKCU:\Software\Microsoft\Windows\CurrentVersion\PushNotifications -Name ToastEnabled -Force -ErrorAction Ignore
Remove-ItemProperty -Path HKCU:\Software\Microsoft\Windows\CurrentVersion\Notifications\Settings\Windows.ActionCenter.SmartOptOut -Name Enable -Force -ErrorAction Ignore
Remove-ItemProperty -Path HKCU:\Software\Microsoft\Windows\CurrentVersion\Notifications\Settings\Sophia -Name ShowBanner, ShowInActionCenter, Enabled -Force -ErrorAction Ignore
Remove-ItemProperty -Path HKCU:\Software\Microsoft\Windows\CurrentVersion\SystemSettings\AccountNotifications -Name EnableAccountNotifications -Force -ErrorAction Ignore
Remove-ItemProperty -Path HKCU:\Software\Policies\Microsoft\Windows\Explorer, HKLM:\SOFTWARE\Policies\Microsoft\Windows\Explorer -Name DisableNotificationCenter -Force -ErrorAction Ignore
Remove-ItemProperty -Path HKCU:\Software\Policies\Microsoft\Windows\CurrentVersion\PushNotifications -Name NoToastApplicationNotification -Force -ErrorAction Ignore
Set-Policy -Scope Computer -Path SOFTWARE\Policies\Microsoft\Windows\Explorer -Name DisableNotificationCenter -Type CLEAR
Set-Policy -Scope User -Path Software\Policies\Microsoft\Windows\Explorer -Name DisableNotificationCenter -Type CLEAR
if (-not (Test-Path -Path Registry::HKEY_CLASSES_ROOT\AppUserModelId\Sophia))
{
New-Item -Path Registry::HKEY_CLASSES_ROOT\AppUserModelId\Sophia -Force
}
# Register app
New-ItemProperty -Path Registry::HKEY_CLASSES_ROOT\AppUserModelId\Sophia -Name DisplayName -Value Sophia -PropertyType String -Force
# Determines whether the app can be seen in Settings where the user can turn notifications on or off
New-ItemProperty -Path Registry::HKEY_CLASSES_ROOT\AppUserModelId\Sophia -Name ShowInSettings -Value 0 -PropertyType DWord -Force
# Call toast notification
Add-Type -AssemblyName "$PSScriptRoot\..\..\Binaries\WinRT.Runtime.dll"
Add-Type -AssemblyName "$PSScriptRoot\..\..\Binaries\Microsoft.Windows.SDK.NET.dll"
[xml]$ToastTemplate = @"
<toast duration="Long" scenario="reminder">
<visual>
<binding template="ToastGeneric">
<text>$($Localization.ThankfulToastTitle)</text>
<text>$($Localization.DonateToastTitle)</text>
</binding>
</visual>
<audio src="ms-winsoundevent:notification.default" />
<actions>
<action content="Ko-fi" arguments="https://ko-fi.com/farag" activationType="protocol"/>
<action content="Boosty" arguments="https://boosty.to/teamsophia" activationType="protocol"/>
</actions>
</toast>
"@
$ToastXml = [Windows.Data.Xml.Dom.XmlDocument]::New()
$ToastXml.LoadXml($ToastTemplate.OuterXml)
$ToastMessage = [Windows.UI.Notifications.ToastNotification]::New($ToastXML)
[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier("Sophia").Show($ToastMessage)
#endregion Toast notifications
Write-Verbose -Message "https://t.me/sophia_chat" -Verbose
Write-Verbose -Message "https://t.me/sophianews" -Verbose
Write-Verbose -Message "https://discord.gg/sSryhaEv79" -Verbose
Write-Information -MessageData "" -InformationAction Continue
Write-Verbose -Message "https://ko-fi.com/farag" -Verbose
Write-Verbose -Message "https://boosty.to/teamsophia" -Verbose
Write-Information -MessageData "" -InformationAction Continue
if ($Global:Error)
{
($Global:Error | ForEach-Object -Process {
# Some errors may have the Windows nature and don't have a path to any of the module's files
$ErrorInFile = if ($_.InvocationInfo.PSCommandPath)
{
Split-Path -Path $_.InvocationInfo.PSCommandPath -Leaf
}
[PSCustomObject]@{
$Localization.ErrorsLine = $_.InvocationInfo.ScriptLineNumber
# Extract the localized "File" string from %SystemRoot%\System32\shell32.dll
"$([WinAPI.GetStrings]::GetString(4130))" = $ErrorInFile
$Localization.ErrorsMessage = $_.Exception.Message
}
} | Sort-Object -Property $Localization.ErrorsLine | Format-Table -AutoSize -Wrap | Out-String).Trim()
}
Write-Information -MessageData "" -InformationAction Continue
Write-Warning -Message $Localization.RestartWarning
}