Home > Windows にまつわる e.t.c.

マルチプラットホームになった PowerShell 6 でクロスプラットホームする


2018/05/26 に開催された「.NETラボ 勉強会 2018年5月」で登壇した時のスライドと、プラン B 用に撮影していたデモ動画です。

 

マルチプラットホームになった PowerShell 6 でクロスプラットホームする from Syuichi Murashima

 

動画はテロップ入れているので、字幕を ON にして見て下さい。

Demo-01 Windows 10 to CentOS 7

 

デモで使ったカンペ

#■■■ Widnows 10
# Install PowerShell 6


#■■■ CentOS 7 Server

### Connect Windows 10 to CentOS 7 Server
ssh root@192.168.33.75

### Install PowerShell 6
# Install PowerShell
sudo yum install https://github.com/PowerShell/PowerShell/releases/download/v6.0.2/powershell-6.0.2-1.rhel.7.x86_64.rpm -y

# Start PowerShell
pwsh


### Dispaly sshd_config
cat "/etc/ssh/sshd_config"


### Edit sshd_config
$SshdConfig = Get-Content "/etc/ssh/sshd_config"
$NewSshdConfig = @()
$PubkeyAuthentication = $false
foreach( $Line in $SshdConfig ){
    # Enable PubkeyAuthentication
    if( $Line -match "PubkeyAuthentication" ){
        $NewSshdConfig += "PubkeyAuthentication yes"
        $PubkeyAuthentication = $true
        continue
    }

    # skip Subsystem powershell
    if( $Line -match "Subsystem powershell" ){
        continue
    }

    $NewSshdConfig += $Line
}

if( $PubkeyAuthentication -eq $false ){
    $NewSshdConfig += "PubkeyAuthentication yes"
}

# Add Subsystem powershell line
$NewSshdConfig += "Subsystem powershell /usr/bin/pwsh -sshs -NoLogo -NoProfile"

# Update sshd config
Set-Content "/etc/ssh/sshd_config" -Value $NewSshdConfig


### Dispaly sshd_config
cat "/etc/ssh/sshd_config"


### Restart sshd
sudo service sshd restart


#■■■ Widnows 10
# Dispaly PowerSehll Vertion(Windows 10 Local)
$PSVersionTable

# Connect Widnows 10 to CentOS 7 Server by PowerSehll 6
Enter-PSSession -HostName 192.168.33.75 -UserName root

# Dispaly PowerSehll Vertion(CentOS 7)
$PSVersionTable

 

Demo-02 CentOS7 to Windows Server 2012 R2 / Windows 10 to Windows Server 2012 R2

 

デモで使ったカンペ

#■■■ WS12R2 Server

### Install PS6


### Install OpenSSH

# Extract
# Rename Directory name
# Copy to Program Files

# Install sshd
. "C:\Program Files\OpenSSH\install-sshd.ps1"


### Open FW
New-NetFirewallRule `
    -Name sshd `
    -DisplayName “OpenSSH Server (sshd)” `
    -Enabled True `
    -Direction Inbound `
    -Protocol TCP `
    -Action Allow `
    -LocalPort 22


### Add Path
setx /m Path ($env:Path + ";" + (Join-Path $env:ProgramFiles OpenSSH))


### sshd status
# Dispaly sshd Service status
Get-Service sshd

# Display GUI
services.msc

### Set sshd service auto start & start sshd service
Set-Service sshd -StartupType Automatic
Start-Service sshd

# Display GUI

### Update sshd_config
# Dispaly sshd_config
notepad C:\ProgramData\ssh\sshd_config


# Edit sshd_config
$SshdConfig = Get-Content "C:\ProgramData\ssh\sshd_config"
$NewSshdConfig = @()
$PubkeyAuthentication = $false
$PasswordAuthentication = $false
foreach( $Line in $SshdConfig ){
    # Enable PubkeyAuthentication
    if( $Line -match "PubkeyAuthentication" ){
        $NewSshdConfig += "PubkeyAuthentication yes"
        $PubkeyAuthentication = $true
        continue
    }

    # Enable PasswordAuthentication
    if( $Line -match "PasswordAuthentication" ){
        $NewSshdConfig += "PasswordAuthentication yes"
        $PasswordAuthentication = $true
        continue
    }

    # skip Subsystem powershell
    if( $Line -match "Subsystem powershell" ){
        continue
    }

    $NewSshdConfig += $Line
}

if( $PubkeyAuthentication -eq $false ){
    $NewSshdConfig += "PubkeyAuthentication yes"
}

if( $PasswordAuthentication -eq $false ){
    $NewSshdConfig += "PasswordAuthentication yes"
}

# Add Subsystem powershell line
$NewSshdConfig += "Subsystem powershell C:\Program Files\PowerShell\6.0.2\pwsh.exe -sshs -NoLogo -NoProfile"

# Update sshd config
Set-Content "C:\ProgramData\ssh\sshd_config" -Value $NewSshdConfig


# Dispaly sshd_config
notepad C:\ProgramData\ssh\sshd_config


# Restart sshd
Restart-Service sshd


#■■■ CentOS 7 Client

### Connect CentOS 7 Client form Windows 10
ssh root@192.168.33.87

### Install PowerShell 6
# Install PowerShell
sudo yum install https://github.com/PowerShell/PowerShell/releases/download/v6.0.2/powershell-6.0.2-1.rhel.7.x86_64.rpm -y

# Start PowerShell
pwsh


### Connect CentOS 7 → Windows Server
# Display PowerShell Version(CentOS 7 Local)
$PSVersionTable


# Connect Windows Server form CentOS 7 Client
Enter-PSSession -HostName 192.168.33.89 -UserName administrator


# Display PowerShell Vertion(Windwos Server)
$PSVersionTable


#■■■ Windows 10

# Display PowerShell Version(Windows 10 Local)
$PSVersionTable


### Connect Windows 10 → Windows Server
Enter-PSSession -HostName 192.168.33.89 -UserName administrator


# Display PowerShell Vertion(Windwos Server)
$PSVersionTable

 

Demo-03 公開鍵認証

 

デモで使ったカンペ

#■■■ Windows 10

### Make Key pair
# Display Profile Directory
ii ~


# Make Key pair
ssh-keygen -t rsa -f "C:\Users\mura\.ssh\mura_rsa"


### Copy Public Key to Server
# Copy public key to WS12R2
scp C:\Users\mura\.ssh\mura_rsa.pub Administrator@192.168.33.89:C:\Users\Administrator


# Copy public key to CentOS 7
scp C:\Users\mura\.ssh\mura_rsa.pub root@192.168.33.75:~/


#■■■ WS12R2

### Set Public Key
# Display Profile Directory(Public Key copied)
ii ~


# Make .ssh dirctory
md ~\.ssh


# Add Public Key to authorized_keys
Get-Content ~\mura_rsa.pub | Add-Content ~\.ssh\authorized_keys


# パーミッション調整(Windows Server)
. "C:\Program Files\OpenSSH\FixHostFilePermissions.ps1" -Confirm:$false


#■■■ CentOS 7 Server

### Connect CentOS 7 Server from Windows 10
ssh root@192.168.33.75

### Set Public Key
# Display Profile Directory(Public Key copied)
ls ~

# 公開鍵セット(CentOS 7 Server)
cd ~
mkdir -p .ssh
chmod 700 .ssh
cat mura_rsa.pub >> .ssh/authorized_keys

# パーミッション調整(CentOS 7 Server)
chmod 640 .ssh/authorized_keys


#■■■ Windows 10
### Connect to CentOS 7
Enter-PSSession -HostName 192.168.33.75 -UserName root -KeyFilePath ~\.ssh\mura_rsa

# ssh-agent セットしていないのでパスフレーズが聞かれる


### Setup ssh-agent
# start setting ssh-agent
Set-Service ssh-agent -StartupType Automatic

# start service
Start-Service ssh-agent

# Entry Key to ssh-agent
ssh-add "C:\Users\mura\.ssh\mura_rsa"


### Reconnect to CentOS 7
Enter-PSSession -HostName 192.168.33.75 -UserName root

# 今度はパスフレーズ聞かれない


### Connect to CentOS WS12R2
Enter-PSSession -HostName 192.168.33.89 -UserName administrator -KeyFilePath ~\.ssh\mura_rsa

 

レコーディング

 

 

参考情報

リモート コンピューターの対話操作(Enter-PSSession)
http://www.vwnet.jp/Windows/PowerShell/EnterPSSession.htm

リモート コンピューターのバッチ操作(Invoke-Command)
http://www.vwnet.jp/Windows/PowerShell/InvokeCommand.htm

リモート コンピューターのパラレル バッチ操作(Invoke-Command -AsJob)
http://www.vwnet.jp/Windows/PowerShell/Invoke-CommandAsJob.htm

PowerShell 6 へリモート接続する(Windows 編)
http://www.vwnet.jp/Windows/PowerShell/2018020501/ConnectRemotePS6.htm

PowerShell 6 へリモート接続する(クロスプラットフォーム/パスワード認証編)
http://www.vwnet.jp/Windows/PowerShell/2018031701/PsRemoteOverSSH.htm

PowerShell 6 へリモート接続する(クロスプラットフォーム/公開鍵認証編)
http://www.vwnet.jp/Windows/PowerShell/2018032101/PsRemoteOverSSHwKey.htm

今日から使う PowerShell
http://www.vwnet.jp/Windows/etc.asp#GettingStartedWithPowerShell

 

 

back.gif (1980 バイト)

home.gif (1907 バイト)

Copyright © MURA All rights reserved.