Search This Blog

Tuesday, December 26, 2023

PowerShell: Automating Cheats in Star Wars Knights of the Old Republic

Hello,

This blog post builds a bit on techniques such as sendkey/sendwait, allowing the PowerShell user to automate keystrokes which helps a lot when entering lengthy cheat codes into older games.
As I am using a nordic keyboard layout the way to open the Kotor 2 console is to press shift + backtick.
Keep in mind that you have to edit one of the files in the Steam folder to enable cheats for Knights of the Old Republic 2.

The PowerShell code:

# KOTOR 2 Auto Cheater #

# Sendkey functions #
$Backtick = "+(´)"
$Space = " "
$Enter = "{ENTER}"

<# Cheats hard codes from https://steamcommunity.com/sharedfiles/filedetails/?id=149123471
These variable each contains the actual cheat code of choice, giving you a long list of favorite codes.#>
$treat = "settreatinjury 40"
$compuse = "setcomputeruse 40"
$demo = "setdemolitions 40"
$stealth = "setstealth 40"
$aware = "setawareness 40"
$charm = "setcharisma 40"
$wis = "setwisdom 40"
$int = "setintelligence 40"
$const = "setconstitution 40"
$dex = "setdexterity 40"
$str = "setstrength 40"
$sec = "setsecurity 40"
$rep = "setrepair 40"
$pers = "setpersuade 40"
$exp = "addexp 999999"

# List used in loop, here you simply add variables that you want to include in the loop. #
$listofcheats = $treat, $compuse, $demo, $stealth, $aware, $charm, $wis, $int, $const, $dex, $str, $sec, $rep, $pers, $exp

# This function is responsible for opening the console only. One function, one purpose. #
function Open-Console {
[System.Windows.Forms.SendKeys]::SendWait($Backtick)
[System.Windows.Forms.SendKeys]::SendWait($Space)
}

# This function is responsible for using the cheat that was typed into the Kotor 2 console. #
function Send-Cheat {
[System.Windows.Forms.SendKeys]::SendWait($Enter)
}

<# This is a simple one of function, that accurately lets you reuse a single cheat code without the worry of misspelling #>
function Use-Cheat($code) {
Start-Sleep -Seconds 5;
Open-Console;
[System.Windows.Forms.SendKeys]::SendWait($code);
Send-Cheat;
}

# This is is the automatic function, it runs through the entire list you customize in the script. #
function Use-AllCheats {
    foreach ($cheat in $listofcheats) {
        Start-Sleep -Seconds 5;
        Open-Console;
        [System.Windows.Forms.SendKeys]::SendWait($cheat);
        Send-Cheat;
    }
}

Sunday, December 17, 2023

Windows: Automatic login

Introduction

This post will briefly show how you can set up your Windows device to automatically login upon starting the computer, it works for restarts as well.

It consists of two parts, first part is the GUI way and if that way doesn't work there is registry way.

The necessary security caveat:
Do not add this to a computer that can fall into the hands of the wrong person. It will decrease your device security and it is not encouraged, this post simply illustrates how it can be done.

The GUI way

Start netplwiz from either run or PowerShell.

Right away you will see an option "users must enter a user name and password to use this computer".

Uncheck this, confirm with your password. 

If the option is missing you will have to do the registry way instead.

The registry way

Create a registry file with the following text:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\PasswordLess\Device\]
"DevicePasswordLessBuildVersion"=dword:00000000

After you have saved it as a .reg-file, you can run it and it will make the necessary changes.
You can probably see the option now in step one if you are curious.

Give the computer a restart and it should now automatically log you into the user.


PowerShell: Create scheduled task

Case where a scheduled task comes in handy

Lately I've been suffering from a explorer process that is not starting upon boot of the computer.

I log in to Windows and the screen is completely black, apart from the mouse pointer sometimes. I've had this issue on a couple of computers over the years, while it is nothing dangerous and easy to fix, it is still annoying.

The quick fix is easy, start task manager using Ctrl + Shift + Escape and run the new task "explorer.exe".

However, I wanted to explore the PowerShell way of perhaps creating a scheduled task that simply checks if the explorer is running, and if not, apply the quick fix automatically. Also I explored whether I could use .bat-file to run a PowerShell script that registers this scheduled task. Unfortunately, for a quick and safe fix it didn't work, you either have to write a more complicated workaround or run the bat file as an admin, which then introduced some other issues. The goal was to keep it very simple.

The PowerShell script that is a failsafe for the explorer process

The code for the fix script looks like this:

function Start-Explorer {

    Start-Process explorer.exe;

}

$Explorer = Get-Process -Name explorer -ErrorAction SilentlyContinue

if (-Not($Explorer)) {

    Start-Explorer;

}

Manually scheduling a task in taskschd.msc

It is completely fine to save this to a .ps1-file and then manually create a scheduled task. Simply start task scheduler, hit "new basic task" once you are in the main folder of tasks.

Trigger:
Being logging in.

Action:
Start a program.

Program/script:
powershell.exe

The added arguments:
-windowstyle hidden -executionpolicy bypass -scope currentuser "C:\temp\PathToScript.ps1"

Basically this tells the computer that upon login (by the user creating the task), run the specified script while bypassing execution policy and only for the user, this time.

Scheduling a task using PowerShell, the basics

This is the very basic method of a PowerShell script that is intended to register a task in the task scheduler. When I tried dot sourcing it, it failed to bypass the execution policy and when I dot sourced it as an admin it created a scheduled task for all users of the computer. This could probably be altered with more detailed parameters and a permanent fix of the execution policy, but it is beside the point of this post. The point of this segment is to show the basic mechanics of using PowerShell to schedule a task.

$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-executionpolicy bypass -file `\"C:\Temp\Sample.ps1`\""
$trigger = New-ScheduledTaskTrigger -AtLogon
$task = New-ScheduledTask -Action $action -Trigger $trigger
Register-ScheduledTask NameOfTask -InputObject $task

As you can see it is a quite interesting and straightforward mechanic.

  1. The very first step is that you tell the computer what to do, in this case run a script with a few parameters, the extra symbols in the file path helps to character escape the extra quotes.
  2. Secondly you tell the computer when to do it, the trigger, in this case upon logging in.
  3. Third you combine the what and when into a task variable.
  4. Lastly you register this task, giving it a name and supplying the task variable to the cmdlet.
When working with PowerShell and the task scheduler, you can of course supply much more details, for example which user should be affected. However, from what I could see you are required to run this script as an admin if you want to run it from the console (dot sourcing).

Creating a .bat-file that runs a PowerShell script

As an added bonus and to wrap up the post of today, I will show you a simple way to use a bat file to kickstart a .ps1-file, with supplied parameters like execution policy bypass.

Open notepad, create a file with the following command:

powershell.exe -ExecutionPolicy Bypass -File .\ScriptInSameDirectory.ps1

You can add parameters like 
-nologo 
-windowstyle hidden 
-executionpolicy bypass and to specify current user add -scope currentuser

Note that if the .bat-file is not in the same folder as the target .ps1 you need to specify full path.

Good luck!

Thursday, December 7, 2023

Review: Alogic MX3 Docking station

A while back ago I got an Alogic MX3, it is a docking station with 3 displayports that I use to connect both my work computer and personal computer.

They are both connected using a USB-C cable to the dock, so when my work day is complete I can connect my personal computer instead.

So far I am rather okay with it, but I've noticed some unfortunate characteristics.

It is too underpowered to reliably power my Surface Pro 7, my Surface Dock 2 does not have that issues. The symptom is that the Alogic MX3 dock does show content on all three monitors but it doesn't charge the battery. Peripherals does not seem to be affected. 

When switching from my work computer to my personal computer it sometimes does not activate my third monitor as well.

The solution to the power issue is to turn the dock off for a few seconds and then turn it on again, that way it seems to start charging the computer again. As for the monitor issue it is simply a matter of extending the screen again to the third monitor.

The over all impression is that the dock looks good, has the right ports for my needs, is easy to work with but I would not recommend a purchase due to the quality of life issues I am experiencing.