Writing

使用 Apple 捷徑方便使用 Windows 功能

Windows,Shortcut,Automation

Created by Ronnie Wong on 2024/9/28

概述

  1. 完成 Windows 的 OpenSSH 伺服器功能
  2. 執行指定的 Powershell 腳本。

使用效果

Video from the original Notion note

Open in the original Notion note

STEP 01 - 啟用 OpenSSH 的伺服器功能

參考文檔:https://gist.github.com/teocci/5a96568ab9bf93a592d7a1a237ebb6ea

安裝 OpenSSH Server

Add-WindowsCapability -Online -Name OpenSSH.Server*

我推薦使用 Powershell 直接安裝即可。

驗證安裝結果

Get-Service -Name *ssh*
PS C:\Users\Qoli\ps1> Get-Service -Name *ssh*

Status   Name               DisplayName                           
------   ----               -----------
Stopped  ssh-agent          OpenSSH Authentication Agent
Stopped  sshd               OpenSSH SSH Server

看到存在上述兩個服務即可。

設定服務為自動啟動

Start-Service sshd
Set-Service -Name sshd -StartupType 'Automatic'

Start-Service 'ssh-agent'
Set-Service -Name 'ssh-agent' -StartupType 'Automatic'

允許通過防火牆

New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22

STEP 02 - Powershell 腳本

param (
    [string]$url = "about:blank" 
)


$taskName = "temp"
$command = "C:\Windows\System32\cmd.exe /c start msedge $url"

schtasks /Create /F /tn $taskName /tr $command /sc once /st 00:00:00
schtasks /run /tn $taskName
schtasks /delete /F /tn $taskName
param (
    [string]$command
)

if (-not $command) {
    Write-Host "Please provide a PowerShell command."
    exit
}

$taskName = "temp"
$escapedCommand = $command -replace '"', '\"'
$scheduledCommand = "powershell.exe -ExecutionPolicy Bypass -Command $escapedCommand"

schtasks /Create /F /tn $taskName /tr "$scheduledCommand" /sc once /st 00:00:00
schtasks /run /tn $taskName
schtasks /delete /F /tn $taskName
Attachment from the original Notion note

Open in the original Notion note

或者直接下載 zip,內容為上述兩個腳本。

STEP 03 - 編寫捷徑

image.png
打開網頁的捷徑
powershell.exe "C:\Users\Qoli\ps1\msedge.ps1" 捷徑輸入
image.png
傳送粘貼板內容的捷徑
powershell.exe "C:\Users\Qoli\ps1\powershell.ps1 -command 'Set-Clipboard -Value "剪貼板"'"