Nonary / MonitorSwapAutomation

Automates swapping to a dummy plug when streaming, then automates swapping back to primary monitor once finished.
292 stars 8 forks source link

Is there a way to load primary.cfg at system start? #52

Open lastwespoke opened 1 month ago

lastwespoke commented 1 month ago

Primary monitor defaults to virtual sink instead of my physical one sporadically, even if i quit session correctly and turn pc off by hand. I thought it would work if i call primary.cfg at system start, but can't achieve desired effect. Tried delayed start (1-10s) at logon of any user, command executes but has no effect. Command is: C:\Scripts\MonitorSwapper\MultiMonitorTool.exe /LoadConfig "C:\Scripts\MonitorSwapper\primary.cfg" Works well when called manually. Any ideas?

Nonary commented 3 weeks ago

1. Prepare Your PowerShell Script

Ensure your PowerShell script (Events.ps1) is correctly placed in the desired directory. For this guide, we'll assume it's located at:

D:\sources\MonitorSwapAutomation\Events.ps1

2. Set Up Task Scheduler

Open Task Scheduler

  1. Using Run Dialog:

    • Press Win + R.
    • Type taskschd.msc and press Enter.
  2. Using Start Menu:

    • Click the Start button.
    • Search for Task Scheduler and open it.

Create a New Task

  1. In the Task Scheduler window, navigate to the Actions pane on the right.
  2. Click "Create Task…".

Configure General Settings

Set the Trigger

  1. Navigate to the "Triggers" tab.
  2. Click "New…".
  3. Begin the task: Select "At log on".
  4. Settings:
    • Specific user: Choose this to run the task only for selected users.
  5. Click "OK" to save the trigger.

Define the Action

  1. Go to the "Actions" tab.

  2. Click "New…".

  3. Action: Select "Start a program".

  4. Program/script: Enter powershell.exe.

  5. Add arguments (REQUIRED): Input the following command, adjusting the path as necessary:

    -WindowStyle Hidden -ExecutionPolicy Bypass -Command "Set-Location 'D:\sources\MonitorSwapAutomation'; .\Events.ps1 -n MonitorSwapper; OnStreamEnd"

    Example for a different script location:

    -WindowStyle Hidden -ExecutionPolicy Bypass -Command "Set-Location 'C:\Scripts\MyAutomation'; .\Events.ps1 -n MonitorSwapper; OnStreamEnd"
  6. Click "OK" to save the action.

Finalize and Save the Task

  1. After configuring triggers and actions, click "OK" in the Create Task window.
  2. If prompted, enter the password for the user account under which the task will run.

3 Configure Remote Access via SSH (Optional)

You can manage and trigger your PowerShell scripts remotely using SSH from your smartphone. If you'd like a button on your phone to trigger the script, follow these steps.

Install OpenSSH on Windows

Follow the guide to install OpenSSH on Windows:

Configure SSH Keys

Using SSH keys enhances security by allowing password-less authentication. Follow these steps to configure SSH keys:

For iPhone

  1. Using Shortcuts on iPhone:
    • Configure the Run Script Over SSH shortcut:
      1. Open the Shortcuts app.
      2. Create a new shortcut.
      3. Add the "Run Script Over SSH" action.
      4. Enter your Windows machine's IP, username, and select the SSH key for authentication.
      5. Copy the public key, send it via email, and add it to the authorized_keys file.
      6. Specify the command to execute, e.g., the PowerShell script.
      7. Save and add the shortcut to your home screen for easy access.

For Android

  1. Using an SSH Client App:
    • Install an SSH client like Termux or JuiceSSH from the Google Play Store.
    • Import your private key into the SSH client, then export the public key via email to the host computer.
    • Configure the SSH connection:
      1. Open the SSH client app.
      2. Add a new SSH connection with your Windows machine's IP, username, and select the SSH key for authentication.
      3. Connect and execute your desired commands, such as running the PowerShell script.

4. Security Considerations

lastwespoke commented 1 week ago

Hey, thanks for you effort in helping me sorting that issue out. I modified your command to my path, so it look like this:

powershell.exe -ExecutionPolicy Bypass -Command "Set-Location 'C:\Scripts\MonitorSwapper'; .\Events.ps1 -n MonitorSwapper; OnStreamEnd" (I have removed WindowStyle attribute for debug purposes)

However, when I try to run this command manually it fails with error:

The term 'OnStreamEnd' is not recognized as the name of a cmdlet, function, script file, or operable program

Could you check that? Thanks.

Nonary commented 1 week ago

Hey, thanks for you effort in helping me sorting that issue out. I modified your command to my path, so it look like this:

powershell.exe -ExecutionPolicy Bypass -Command "Set-Location 'C:\Scripts\MonitorSwapper'; .\Events.ps1 -n MonitorSwapper; OnStreamEnd" (I have removed WindowStyle attribute for debug purposes)

However, when I try to run this command manually it fails with error:

The term 'OnStreamEnd' is not recognized as the name of a cmdlet, function, script file, or operable program

Could you check that? Thanks.

Try

Add a dot and a space before .\Events.ps1

like . .\Events.ps1

lastwespoke commented 6 days ago

Syntax error is fixed now, thanks for the tip. Sadly, desired effect is still not there. Command executes but still hasn't same effect as disconnecting from stream from my mobile device. If i run command while on main monitor it outputs next string:

Primary monitor(s) have been successfully restored! True

No other effect, as expected.

However, if i switch to mobile device and run command it just outputs:

False

And doesn't switch to main monitor as expected.