Skip to content

Commit

Permalink
WindowsScriptHost improved
Browse files Browse the repository at this point in the history
  • Loading branch information
farag2 committed Jun 29, 2024
1 parent 24013e9 commit 2a96da4
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 35 deletions.
29 changes: 24 additions & 5 deletions src/Sophia_Script_for_Windows_10/Module/Sophia.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ public static extern bool SetForegroundWindow(IntPtr hWnd);
# Detect Windows bitness
if (-not [System.Environment]::Is64BitOperatingSystem)
{
Write-Information -MessageData "" -InformationAction Continue
Write-Warning -Message $Localization.UnsupportedOSBitness
Write-Information -MessageData "" -InformationAction Continue

Write-Verbose -Message "https://t.me/sophia_chat" -Verbose
Write-Verbose -Message "https://discord.gg/sSryhaEv79" -Verbose
Expand Down Expand Up @@ -871,7 +873,12 @@ public static extern bool SetForegroundWindow(IntPtr hWnd);
Get-ChildItem -Path "$env:TEMP\Computer.txt", "$env:TEMP\User.txt" -Force -ErrorAction Ignore | Remove-Item -Recurse -Force -ErrorAction Ignore

# Save all opened folders in order to restore them after File Explorer restart
$Script:OpenedFolders = {(New-Object -ComObject Shell.Application).Windows() | ForEach-Object -Process {$_.Document.Folder.Self.Path}}.Invoke()
try
{
$Script:OpenedFolders = {(New-Object -ComObject Shell.Application).Windows() | ForEach-Object -Process {$_.Document.Folder.Self.Path}}.Invoke()
}
catch [System.Management.Automation.PropertyNotFoundException]
{}

<#
.SYNOPSIS
Expand Down Expand Up @@ -997,7 +1004,7 @@ public static extern bool SetForegroundWindow(IntPtr hWnd);
# Get the name of a preset (e.g Sophia.ps1) regardless it was named
# $_.File has no EndsWith() method
Write-Information -MessageData "" -InformationAction Continue
$PresetName = Split-Path -Path (((Get-PSCallStack).Position | Where-Object -FilterScript {$_.File}).File | Where-Object -FilterScript {$_.EndsWith(".ps1")}) -Leaf
[string]$PresetName = ((Get-PSCallStack).Position | Where-Object -FilterScript {$_.File}).File | Where-Object -FilterScript {$_.EndsWith(".ps1")}
Write-Verbose -Message ($Localization.CustomizationWarning -f $PresetName) -Verbose

do
Expand Down Expand Up @@ -12577,6 +12584,7 @@ function SoftwareDistributionTask
# Get user's SID the task was created as
$Script:SID = ([xml]$_.xml).Task.Principals.Principal.UserID
}

# Convert SID to username
$TaskUserAccount = (New-Object System.Security.Principal.SecurityIdentifier($SID)).Translate([System.Security.Principal.NTAccount]).Value -split "\\" | Select-Object -Last 1

Expand Down Expand Up @@ -13686,6 +13694,14 @@ function WindowsScriptHost
{
"Disable"
{
# Check if any scheduled tasks were created before, because they rely on Windows Host running vbs files
Get-ScheduledTask -TaskName SoftwareDistribution, Temp, "Windows Cleanup", "Windows Cleanup Notification" | ForEach-Object -Process {
if ($_.State -eq "Ready")
{
continue
}
}

if (-not (Test-Path -Path "HKCU:\Software\Microsoft\Windows Script Host\Settings"))
{
New-Item -Path "HKCU:\Software\Microsoft\Windows Script Host\Settings" -Force
Expand Down Expand Up @@ -14862,11 +14878,14 @@ public static void PostMessage()
Start-Sleep -Seconds 3

# Restoring closed folders
foreach ($Script:OpenedFolder in $Script:OpenedFolders)
if (Get-Variable -Name OpenedFolder -ErrorAction Ignore)
{
if (Test-Path -Path $Script:OpenedFolder)
foreach ($Script:OpenedFolder in $Script:OpenedFolders)
{
Start-Process -FilePath explorer -ArgumentList $Script:OpenedFolder
if (Test-Path -Path $Script:OpenedFolder)
{
Start-Process -FilePath explorer -ArgumentList $Script:OpenedFolder
}
}
}

Expand Down
31 changes: 26 additions & 5 deletions src/Sophia_Script_for_Windows_10_LTSC_2019/Module/Sophia.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ public static extern bool SetForegroundWindow(IntPtr hWnd);
# Detect Windows bitness
if (-not [System.Environment]::Is64BitOperatingSystem)
{
Write-Information -MessageData "" -InformationAction Continue
Write-Warning -Message $Localization.UnsupportedOSBitness
Write-Information -MessageData "" -InformationAction Continue

Write-Verbose -Message "https://t.me/sophia_chat" -Verbose
Write-Verbose -Message "https://discord.gg/sSryhaEv79" -Verbose
Expand Down Expand Up @@ -584,7 +586,9 @@ public static extern bool SetForegroundWindow(IntPtr hWnd);
# Detect Windows build version
if ((Get-WindowsEdition -Online).Edition -notmatch "EnterpriseS")
{
Write-Information -MessageData "" -InformationAction Continue
Write-Warning -Message $Localization.UnsupportedOSBuild
Write-Information -MessageData "" -InformationAction Continue

Write-Verbose -Message "https://t.me/sophia_chat" -Verbose
Write-Verbose -Message "https://discord.gg/sSryhaEv79" -Verbose
Expand Down Expand Up @@ -839,7 +843,12 @@ public static extern bool SetForegroundWindow(IntPtr hWnd);
Get-ChildItem -Path "$env:TEMP\Computer.txt", "$env:TEMP\User.txt" -Force -ErrorAction Ignore | Remove-Item -Recurse -Force -ErrorAction Ignore

# Save all opened folders in order to restore them after File Explorer restart
$Script:OpenedFolders = {(New-Object -ComObject Shell.Application).Windows() | ForEach-Object -Process {$_.Document.Folder.Self.Path}}.Invoke()
try
{
$Script:OpenedFolders = {(New-Object -ComObject Shell.Application).Windows() | ForEach-Object -Process {$_.Document.Folder.Self.Path}}.Invoke()
}
catch [System.Management.Automation.PropertyNotFoundException]
{}

<#
.SYNOPSIS
Expand Down Expand Up @@ -965,7 +974,7 @@ public static extern bool SetForegroundWindow(IntPtr hWnd);
# Get the name of a preset (e.g Sophia.ps1) regardless it was named
# $_.File has no EndsWith() method
Write-Information -MessageData "" -InformationAction Continue
$PresetName = Split-Path -Path (((Get-PSCallStack).Position | Where-Object -FilterScript {$_.File}).File | Where-Object -FilterScript {$_.EndsWith(".ps1")}) -Leaf
[string]$PresetName = ((Get-PSCallStack).Position | Where-Object -FilterScript {$_.File}).File | Where-Object -FilterScript {$_.EndsWith(".ps1")}
Write-Verbose -Message ($Localization.CustomizationWarning -f $PresetName) -Verbose

do
Expand Down Expand Up @@ -9429,6 +9438,7 @@ function SoftwareDistributionTask
# Get user's SID the task was created as
$Script:SID = ([xml]$_.xml).Task.Principals.Principal.UserID
}

# Convert SID to username
$TaskUserAccount = (New-Object System.Security.Principal.SecurityIdentifier($SID)).Translate([System.Security.Principal.NTAccount]).Value -split "\\" | Select-Object -Last 1

Expand Down Expand Up @@ -10538,6 +10548,14 @@ function WindowsScriptHost
{
"Disable"
{
# Check if any scheduled tasks were created before, because they rely on Windows Host running vbs files
Get-ScheduledTask -TaskName SoftwareDistribution, Temp, "Windows Cleanup", "Windows Cleanup Notification" | ForEach-Object -Process {
if ($_.State -eq "Ready")
{
continue
}
}

if (-not (Test-Path -Path "HKCU:\Software\Microsoft\Windows Script Host\Settings"))
{
New-Item -Path "HKCU:\Software\Microsoft\Windows Script Host\Settings" -Force
Expand Down Expand Up @@ -11637,11 +11655,14 @@ public static void PostMessage()
Start-Sleep -Seconds 3

# Restoring closed folders
foreach ($Script:OpenedFolder in $Script:OpenedFolders)
if (Get-Variable -Name OpenedFolder -ErrorAction Ignore)
{
if (Test-Path -Path $Script:OpenedFolder)
foreach ($Script:OpenedFolder in $Script:OpenedFolders)
{
Start-Process -FilePath explorer -ArgumentList $Script:OpenedFolder
if (Test-Path -Path $Script:OpenedFolder)
{
Start-Process -FilePath explorer -ArgumentList $Script:OpenedFolder
}
}
}

Expand Down
31 changes: 26 additions & 5 deletions src/Sophia_Script_for_Windows_10_LTSC_2021/Module/Sophia.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ public static extern bool SetForegroundWindow(IntPtr hWnd);
# Detect Windows bitness
if (-not [System.Environment]::Is64BitOperatingSystem)
{
Write-Information -MessageData "" -InformationAction Continue
Write-Warning -Message $Localization.UnsupportedOSBitness
Write-Information -MessageData "" -InformationAction Continue

Write-Verbose -Message "https://t.me/sophia_chat" -Verbose
Write-Verbose -Message "https://discord.gg/sSryhaEv79" -Verbose
Expand Down Expand Up @@ -584,7 +586,9 @@ public static extern bool SetForegroundWindow(IntPtr hWnd);
# Detect Windows build version
if ((Get-WindowsEdition -Online).Edition -notmatch "EnterpriseS")
{
Write-Information -MessageData "" -InformationAction Continue
Write-Warning -Message $Localization.UnsupportedOSBuild
Write-Information -MessageData "" -InformationAction Continue

Write-Verbose -Message "https://t.me/sophia_chat" -Verbose
Write-Verbose -Message "https://discord.gg/sSryhaEv79" -Verbose
Expand Down Expand Up @@ -839,7 +843,12 @@ public static extern bool SetForegroundWindow(IntPtr hWnd);
Get-ChildItem -Path "$env:TEMP\Computer.txt", "$env:TEMP\User.txt" -Force -ErrorAction Ignore | Remove-Item -Recurse -Force -ErrorAction Ignore

# Save all opened folders in order to restore them after File Explorer restart
$Script:OpenedFolders = {(New-Object -ComObject Shell.Application).Windows() | ForEach-Object -Process {$_.Document.Folder.Self.Path}}.Invoke()
try
{
$Script:OpenedFolders = {(New-Object -ComObject Shell.Application).Windows() | ForEach-Object -Process {$_.Document.Folder.Self.Path}}.Invoke()
}
catch [System.Management.Automation.PropertyNotFoundException]
{}

<#
.SYNOPSIS
Expand Down Expand Up @@ -965,7 +974,7 @@ public static extern bool SetForegroundWindow(IntPtr hWnd);
# Get the name of a preset (e.g Sophia.ps1) regardless it was named
# $_.File has no EndsWith() method
Write-Information -MessageData "" -InformationAction Continue
$PresetName = Split-Path -Path (((Get-PSCallStack).Position | Where-Object -FilterScript {$_.File}).File | Where-Object -FilterScript {$_.EndsWith(".ps1")}) -Leaf
[string]$PresetName = ((Get-PSCallStack).Position | Where-Object -FilterScript {$_.File}).File | Where-Object -FilterScript {$_.EndsWith(".ps1")}
Write-Verbose -Message ($Localization.CustomizationWarning -f $PresetName) -Verbose

do
Expand Down Expand Up @@ -10563,6 +10572,7 @@ function SoftwareDistributionTask
# Get user's SID the task was created as
$Script:SID = ([xml]$_.xml).Task.Principals.Principal.UserID
}

# Convert SID to username
$TaskUserAccount = (New-Object System.Security.Principal.SecurityIdentifier($SID)).Translate([System.Security.Principal.NTAccount]).Value -split "\\" | Select-Object -Last 1

Expand Down Expand Up @@ -11672,6 +11682,14 @@ function WindowsScriptHost
{
"Disable"
{
# Check if any scheduled tasks were created before, because they rely on Windows Host running vbs files
Get-ScheduledTask -TaskName SoftwareDistribution, Temp, "Windows Cleanup", "Windows Cleanup Notification" | ForEach-Object -Process {
if ($_.State -eq "Ready")
{
continue
}
}

if (-not (Test-Path -Path "HKCU:\Software\Microsoft\Windows Script Host\Settings"))
{
New-Item -Path "HKCU:\Software\Microsoft\Windows Script Host\Settings" -Force
Expand Down Expand Up @@ -12831,11 +12849,14 @@ public static void PostMessage()
Start-Sleep -Seconds 3

# Restoring closed folders
foreach ($Script:OpenedFolder in $Script:OpenedFolders)
if (Get-Variable -Name OpenedFolder -ErrorAction Ignore)
{
if (Test-Path -Path $Script:OpenedFolder)
foreach ($Script:OpenedFolder in $Script:OpenedFolders)
{
Start-Process -FilePath explorer -ArgumentList $Script:OpenedFolder
if (Test-Path -Path $Script:OpenedFolder)
{
Start-Process -FilePath explorer -ArgumentList $Script:OpenedFolder
}
}
}

Expand Down
33 changes: 26 additions & 7 deletions src/Sophia_Script_for_Windows_10_PowerShell_7/Module/Sophia.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ public static extern bool SetForegroundWindow(IntPtr hWnd);
# Detect Windows bitness
if (-not [System.Environment]::Is64BitOperatingSystem)
{
Write-Information -MessageData "" -InformationAction Continue
Write-Warning -Message $Localization.UnsupportedOSBitness
Write-Information -MessageData "" -InformationAction Continue

Write-Verbose -Message "https://t.me/sophia_chat" -Verbose
Write-Verbose -Message "https://discord.gg/sSryhaEv79" -Verbose
Expand Down Expand Up @@ -875,7 +877,12 @@ public static extern bool SetForegroundWindow(IntPtr hWnd);
Get-ChildItem -Path "$env:TEMP\Computer.txt", "$env:TEMP\User.txt" -Force -ErrorAction Ignore | Remove-Item -Recurse -Force -ErrorAction Ignore

# Save all opened folders in order to restore them after File Explorer restart
$Script:OpenedFolders = {(New-Object -ComObject Shell.Application).Windows() | ForEach-Object -Process {$_.Document.Folder.Self.Path}}.Invoke()
try
{
$Script:OpenedFolders = {(New-Object -ComObject Shell.Application).Windows() | ForEach-Object -Process {$_.Document.Folder.Self.Path}}.Invoke()
}
catch [System.Management.Automation.PropertyNotFoundException]
{}

<#
.SYNOPSIS
Expand Down Expand Up @@ -1001,7 +1008,7 @@ public static extern bool SetForegroundWindow(IntPtr hWnd);
# Get the name of a preset (e.g Sophia.ps1) regardless it was named
# $_.File has no EndsWith() method
Write-Information -MessageData "" -InformationAction Continue
$PresetName = Split-Path -Path (((Get-PSCallStack).Position | Where-Object -FilterScript {$_.File}).File | Where-Object -FilterScript {$_.EndsWith(".ps1")}) -Leaf
[string]$PresetName = ((Get-PSCallStack).Position | Where-Object -FilterScript {$_.File}).File | Where-Object -FilterScript {$_.EndsWith(".ps1")}
Write-Verbose -Message ($Localization.CustomizationWarning -f $PresetName) -Verbose

do
Expand Down Expand Up @@ -4950,8 +4957,8 @@ function Cursors
Namespace = "WinAPI"
Name = "Cursor"
Language = "CSharp"
CompilerOptions = $CompilerOptions
MemberDefinition = @"
CompilerOptions = $CompilerOptions
MemberDefinition = @"
[DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
public static extern bool SystemParametersInfo(uint uiAction, uint uiParam, uint pvParam, uint fWinIni);
"@
Expand Down Expand Up @@ -12593,6 +12600,7 @@ function SoftwareDistributionTask
# Get user's SID the task was created as
$Script:SID = ([xml]$_.xml).Task.Principals.Principal.UserID
}

# Convert SID to username
$TaskUserAccount = (New-Object System.Security.Principal.SecurityIdentifier($SID)).Translate([System.Security.Principal.NTAccount]).Value -split "\\" | Select-Object -Last 1

Expand Down Expand Up @@ -13702,6 +13710,14 @@ function WindowsScriptHost
{
"Disable"
{
# Check if any scheduled tasks were created before, because they rely on Windows Host running vbs files
Get-ScheduledTask -TaskName SoftwareDistribution, Temp, "Windows Cleanup", "Windows Cleanup Notification" | ForEach-Object -Process {
if ($_.State -eq "Ready")
{
continue
}
}

if (-not (Test-Path -Path "HKCU:\Software\Microsoft\Windows Script Host\Settings"))
{
New-Item -Path "HKCU:\Software\Microsoft\Windows Script Host\Settings" -Force
Expand Down Expand Up @@ -14878,11 +14894,14 @@ public static void PostMessage()
Start-Sleep -Seconds 3

# Restoring closed folders
foreach ($Script:OpenedFolder in $Script:OpenedFolders)
if (Get-Variable -Name OpenedFolder -ErrorAction Ignore)
{
if (Test-Path -Path $Script:OpenedFolder)
foreach ($Script:OpenedFolder in $Script:OpenedFolders)
{
Start-Process -FilePath explorer -ArgumentList $Script:OpenedFolder
if (Test-Path -Path $Script:OpenedFolder)
{
Start-Process -FilePath explorer -ArgumentList $Script:OpenedFolder
}
}
}

Expand Down
29 changes: 23 additions & 6 deletions src/Sophia_Script_for_Windows_11/Module/Sophia.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,12 @@ public static extern bool SetForegroundWindow(IntPtr hWnd);
Get-ChildItem -Path "$env:TEMP\Computer.txt", "$env:TEMP\User.txt" -Force -ErrorAction Ignore | Remove-Item -Recurse -Force -ErrorAction Ignore

# Save all opened folders in order to restore them after File Explorer restart
$Script:OpenedFolders = {(New-Object -ComObject Shell.Application).Windows() | ForEach-Object -Process {$_.Document.Folder.Self.Path}}.Invoke()
try
{
$Script:OpenedFolders = {(New-Object -ComObject Shell.Application).Windows() | ForEach-Object -Process {$_.Document.Folder.Self.Path}}.Invoke()
}
catch [System.Management.Automation.PropertyNotFoundException]
{}

<#
.SYNOPSIS
Expand Down Expand Up @@ -987,7 +992,7 @@ public static extern bool SetForegroundWindow(IntPtr hWnd);
# Get the name of a preset (e.g Sophia.ps1) regardless it was named
# $_.File has no EndsWith() method
Write-Information -MessageData "" -InformationAction Continue
$PresetName = Split-Path -Path (((Get-PSCallStack).Position | Where-Object -FilterScript {$_.File}).File | Where-Object -FilterScript {$_.EndsWith(".ps1")}) -Leaf
[string]$PresetName = ((Get-PSCallStack).Position | Where-Object -FilterScript {$_.File}).File | Where-Object -FilterScript {$_.EndsWith(".ps1")}
Write-Verbose -Message ($Localization.CustomizationWarning -f $PresetName) -Verbose

do
Expand Down Expand Up @@ -11752,6 +11757,7 @@ function SoftwareDistributionTask
# Get user's SID the task was created as
$Script:SID = ([xml]$_.xml).Task.Principals.Principal.UserID
}

# Convert SID to username
$TaskUserAccount = (New-Object System.Security.Principal.SecurityIdentifier($SID)).Translate([System.Security.Principal.NTAccount]).Value -split "\\" | Select-Object -Last 1

Expand Down Expand Up @@ -12836,7 +12842,7 @@ function SaveZoneInformation
.NOTES
Current user
#>
function WindowsScriptHost ###
function WindowsScriptHost
{
param
(
Expand All @@ -12859,6 +12865,14 @@ function WindowsScriptHost ###
{
"Disable"
{
# Check if any scheduled tasks were created before, because they rely on Windows Host running vbs files
Get-ScheduledTask -TaskName SoftwareDistribution, Temp, "Windows Cleanup", "Windows Cleanup Notification" | ForEach-Object -Process {
if ($_.State -eq "Ready")
{
continue
}
}

if (-not (Test-Path -Path "HKCU:\Software\Microsoft\Windows Script Host\Settings"))
{
New-Item -Path "HKCU:\Software\Microsoft\Windows Script Host\Settings" -Force
Expand Down Expand Up @@ -14038,11 +14052,14 @@ public static void PostMessage()
Start-Sleep -Seconds 3

# Restoring closed folders
foreach ($Script:OpenedFolder in $Script:OpenedFolders)
if (Get-Variable -Name OpenedFolder -ErrorAction Ignore)
{
if (Test-Path -Path $Script:OpenedFolder)
foreach ($Script:OpenedFolder in $Script:OpenedFolders)
{
Start-Process -FilePath explorer -ArgumentList $Script:OpenedFolder
if (Test-Path -Path $Script:OpenedFolder)
{
Start-Process -FilePath explorer -ArgumentList $Script:OpenedFolder
}
}
}

Expand Down
Loading

0 comments on commit 2a96da4

Please sign in to comment.