BeforeCode
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit }
& "$PSScriptRoot\handle64.exe" -accepteula -a -p D2R.exe > $PSScriptRoot\d2r_handles.txt
$proc_id_populated = ""
$handle_id_populated = ""
foreach($line in Get-Content $PSScriptRoot\d2r_handles.txt) {
$proc_id = $line | Select-String -Pattern '^D2R.exe pid\: (?<g1>.+) ' | %{$_.Matches.Groups[1].value}
if ($proc_id)
{
$proc_id_populated = $proc_id
}
$handle_id = $line | Select-String -Pattern '^(?<g2>.+): Event.*DiabloII Check For Other Instances' | %{$_.Matches.Groups[1].value}
if ($handle_id)
{
$handle_id_populated = $handle_id
}
if($handle_id){
Write-Host "Closing" $proc_id_populated $handle_id_populated
& "$PSScriptRoot\handle64.exe" -p $proc_id_populated -c $handle_id_populated -y
}
}
read-host "Press ENTER to continue..."
AfterCode
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit }
& "$PSScriptRoot\D2Rhandle_del\handle64.exe" -accepteula -a -p D2R.exe > $PSScriptRoot\D2Rhandle_del\d2r_handles.txt
$proc_id_populated = ""
$handle_id_populated = ""
foreach($line in Get-Content $PSScriptRoot\D2Rhandle_del\d2r_handles.txt) {
$proc_id = $line | Select-String -Pattern '^D2R.exe pid\: (?<g1>.+) ' | %{$_.Matches.Groups[1].value}
if ($proc_id)
{
$proc_id_populated = $proc_id
}
$handle_id = $line | Select-String -Pattern '^(?<g2>.+): Event.*DiabloII Check For Other Instances' | %{$_.Matches.Groups[1].value}
if ($handle_id)
{
$handle_id_populated = $handle_id
}
if($handle_id){
Write-Host "Closing" $proc_id_populated $handle_id_populated
& "$PSScriptRoot\D2Rhandle_del\handle64.exe" -p $proc_id_populated -c $handle_id_populated -y
}
}
pause
read-host "Press ENTER to continue..."
Batch Script OptionalCode
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File "%~dp0D2Rhandle_del.ps1"' -Verb RunAs}"
Windows 11 : Frustrated & Upset because PowerShell Scripts can't run as admin ???
Shortcut Created ( Run as admin checkbox greyed out )
Installed PowerShell 7, same thing Result.
Fix Of the Day #1 !Was able to click start > Search power shell, then Right-Click as admiistrator ( copy & paste the script ) and run it just fine * Had to edit the directory locations
Just Ctrl+F to search or Ctrl+H to replace text.
$PSScriptRoot\ = C:\
So basically you just add the missing directory folder named D2Rhandle_del
Instead of "$PSScriptRoot\handle64.exe"
You will do "$PSScriptRoot\D2Rhandle_del\handle64.exe"
Instead of $PSScriptRoot\d2r_handles.txt
You will do $PSScriptRoot\D2Rhandle_del\d2r_handles.txt
Fix Of the Day #2 !Even though the mentioned fix above works.
There is still an even better workaround. !!!
Instead of making changes to the directories.
In the D2Rhandle_del Folder , just Create a NewFile.txt ( You can Delete this text file after its saved as Start.Bat)
Copy & Paste This Batch Script that I've Provided , save it as Start.Bat
What this will do Is it will Open command Prompt ( No need for run as admin ) , and it will Automatically Launch PowerShell as Admin , and Run the D2Rhandle_del Script
Enjoy.