Japanese notation
はじめに
PowerShellでSSHアクセス
-
PowerShellの開始 (キー入力):
Win
+x
>a
>はい
※Windows7以降標準搭載
パワーシェル7のインストールとショートカット作成
- PowerShell 5.1 環境でPowerShell 7のインストールとショートカット作成のスクリプト(デスクトップ)
powershell
$currentVersion = $PSVersionTable.PSVersion
Write-Host "Current PowerShell version: $($currentVersion)"
$installed = Get-Command pwsh -ErrorAction SilentlyContinue
if ($installed) {
Write-Host "PowerShell 7 is already installed. Skipping installation."
} else {
Write-Host "Installing PowerShell 7..."
$url = "https://aka.ms/install-powershell.ps1"
Invoke-WebRequest -Uri $url -OutFile "install-powershell.ps1"
.\install-powershell.ps1
Write-Host "PowerShell 7 installation completed."
}
$desktop = [Environment]::GetFolderPath("Desktop")
$shortcutPath = "$desktop\PowerShell 7 (Admin).lnk"
$targetPath = "C:\Program Files\PowerShell\7\pwsh.exe"
$arguments = "-Command Start-Process pwsh -Verb runAs"
$shell = New-Object -ComObject WScript.Shell
$shortcut = $shell.CreateShortcut($shortcutPath)
$shortcut.TargetPath = $targetPath
$shortcut.Arguments = $arguments
$shortcut.Description = "PowerShell 7 Administrator Shortcut"
$shortcut.WorkingDirectory = "$HOME"
$shortcut.IconLocation = $targetPath
$shortcut.Save()
Write-Host "PowerShell 7 administrator shortcut has been created."
ssh root@192.168.1.1
ssh root@192.168.1.1のショートカット作成(デスクトップ)
powershell
$DESKTOP = ([Environment]::GetFolderPath("Desktop") + "\192.168.1.1.lnk")
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$DESKTOP")
$Shortcut.TargetPath = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
$Shortcut.Arguments = '-windowstyle hidden -ExecutionPolicy RemoteSigned "Start-Process ssh root@192.168.1.1"'
$Shortcut.IconLocation = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe,0"
$Shortcut.WorkingDirectory = "."
$Shortcut.Save()
強制的に貼り付け
yes
SSHログイン出来ない場合![:exclamation: :exclamation:](https://cdn.qiita.com/emoji/twemoji/unicode/2757-fe0f.png)
known_hostsクリア
-
C:\Users\yourusername\.ssh\known_hosts
※Windows隠しファイル
powershell
Clear-Content .ssh\known_hosts -Force
OpenSSHのインストール
※Windows 10 Fall Creators Update(1709)以降標準搭載
- 機能の確認
powershell
Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*'
- 機能のインストール
powershell
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
Windows小ネタ
ソフトウェアの最新バージョンクローラー
Serva
$LINKS = Invoke-WebRequest "https://www.vercot.com/~serva/download.html" -UseBasicParsing
$LINKS_VERSION = $LINKS.Links | Where-Object {$_.href -like "*Serva_Community_*.zip*"} | Select-Object -ExpandProperty href
$VERSION = ($LINKS_VERSION -split '/')[-1]
Write-Host "Version to install: $VERSION"
$ONAMAE = (whoami).Split('\')[1]
$downloadPath = "C:\Users\$ONAMAE\Downloads\Serva.zip"
Invoke-WebRequest -Uri "https://www.vercot.com/~serva/download/$VERSION" -OutFile $downloadPath
$extractPath = "C:\Users\$ONAMAE\Downloads\Serva"
Expand-Archive -Path $downloadPath -DestinationPath $extractPath -Force
Remove-Item -Path $downloadPath -Force
$installPath = "C:\Program Files\Serva"
if (-not (Test-Path -Path $installPath)) { New-Item -ItemType Directory -Path $installPath -Force }
Copy-Item -Path "$extractPath\*" -Destination $installPath -Recurse -Force
Remove-Item -Path $extractPath -Recurse -Force
if ([Environment]::Is64BitOperatingSystem) { $exePath = "$installPath\Serva64.exe" } else { $exePath = "$installPath\Serva32.exe" }
$desktop = [Environment]::GetFolderPath("Desktop")
$shortcutPath = "$desktop\Serva (Admin).lnk"
$shell = New-Object -ComObject WScript.Shell
$shortcut = $shell.CreateShortcut($shortcutPath)
$shortcut.TargetPath = "$exePath"
$shortcut.Arguments = ""
$shortcut.Description = "Serva Admin"
$shortcut.WorkingDirectory = "$installPath"
$shortcut.IconLocation = "$exePath,0"
$shortcut.Save()
Write-Host "Serva has been installed, and a shortcut has been created on the desktop."
WinSCP
$psVersion = $PSVersionTable.PSVersion.Major
$LINKS = Invoke-WebRequest "https://winscp.net/eng/download.php"
$LINKS_VERSION = $LINKS.Links | Where-Object {$_.href -like "*WinSCP-*-Setup.exe*"} | Select-Object -ExpandProperty href
$VERSION = ($LINKS_VERSION -split '/')[-2] -replace "WinSCP-([0-9]+\.[0-9]+\.[0-9]+).*", '$1'
Write-Host "Version to install: $VERSION"
$downloadUrl = "https://jaist.dl.sourceforge.net/project/winscp/WinSCP/$VERSION/WinSCP-$VERSION-Setup.exe?viasf=1"
Write-Host "Downloading from: $downloadUrl"
$ONAMAE = (whoami).Split('\')[1]
$destinationPath = "C:\Users\$ONAMAE\Downloads\WinSCP-$VERSION-Setup.exe"
Invoke-WebRequest -Uri $downloadUrl -OutFile $destinationPath
Write-Host "Installing WinSCP..."
Start-Process -FilePath $destinationPath -ArgumentList "/VERYSILENT /NORESTART" -Wait
Invoke-Expression "C:\Users\$ONAMAE\AppData\Local\Programs\WinSCP\WinSCP.exe"