Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lastwespoke opened this issue Oct 7, 2024 · 4 comments
Open

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

lastwespoke opened this issue Oct 7, 2024 · 4 comments

Comments

@lastwespoke
Copy link

lastwespoke commented Oct 7, 2024

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?

@lastwespoke lastwespoke changed the title It there a way to load primary.cfg at system start? Is there a way to load primary.cfg at system start? Oct 7, 2024
@Nonary
Copy link
Owner

Nonary commented Oct 22, 2024

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

  • Name: MonitorSwapAutomation
  • Description (Optional): Briefly describe the task's purpose, e.g., "Automates monitor swap processes at user logon."
  • Security Options:
    • Select "Run only when user is logged on".
      This ensures the task runs within the user's session, which is necessary for UI interactions.

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

  • Use Strong SSH Keys: Ensure your SSH keys are strong (e.g., RSA 4096-bit) and protect your private keys.
  • Firewall Settings: Ensure your Windows Firewall allows SSH connections or configure port forwarding if accessing remotely.
  • Regular Updates: Keep your Windows system and OpenSSH server updated to mitigate security vulnerabilities.
  • Disable Password Authentication (Optional): For enhanced security, disable password-based SSH authentication by setting PasswordAuthentication no in sshd_config.

@lastwespoke
Copy link
Author

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
Copy link
Owner

Nonary commented Nov 7, 2024

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants