Microsoft Azureへの不慣れが多い状況ですが、少しづつ触っています。
今回は、PowerShellからMicrosoft Azureの仮想マシンにリモート接続する手順をメモ。
使わないと分からないものですね。
リモート接続するまでの流れ
- Azure PowerShell をインストール
- Azure PowerShell を起動する
- Azure アカウントを認証する
- Azure 仮想マシンを起動する
- Azure 仮想マシンへリモート接続
- 方法1
- 方法2
Azure PowerShellをインストール
「How to install and configure Azure PowerShell」を参考に設定します。
- Windows Web Platform Installer 5.0をインストール
- [Microsoft Azure PowerShell]を探し、[追加]ボタンをクリック
- [インストール(I)]ボタンをクリック
- [同意する(A)]ボタンをクリック
- インストール完了後、[終了(E)]ボタンをクリック
Azure PowerShellを起動する
Windows 7の場合、スタートボタン→[プログラム一覧]→[Microsoft Azure]※→[Microsoft Azure PowerShell]を実行します。
似たような[Windows Azure] フォルダーがありますが、こちらではありません。
似たような[Windows Azure] フォルダーがありますが、こちらではありません。
Azure アカウントを認証する
Azure の各種サービスを利用するためには、Azure アカウントを認証しなければなりません。
私はMicrosoft アカウントを使っているので、下記手順で認証しました。
- Add-AzureAccount コマンドレットを実行
- Microsoft アカウントの認証処理を行う
- Get-AzureAccount コマンドレットを実行し、アカウントを確認
Azure 仮想マシンを起動
作成済みのAzure 仮想マシンを確認、起動します。
- Get-AzureVM コマンドレットを実行、起動したいServiceNameおよびNameを確認
- Start-AzureVM -ServiceName <サービス名> -Name <ホスト名>
- 「Completed Operation: Start-AzureVM」の文言が表示されるまで待つ
Azure 仮想マシンへリモート接続~方法1
WinRM経由でAzure 仮想マシンへリモート接続する方法は、ローカルのWindows Serverにリモート接続する手順と「基本」変わりませんが、全く同じスクリプトだとエラーになりました。
試行錯誤した結果、いくつかのパラメーターが必要でした。
試行錯誤した結果、いくつかのパラメーターが必要でした。
$u = "WSEのユーザー アカウント名" $p = "パスワード" | ConvertTo-SecureString -AsPlainText -Force $c = New-Object System.Management.Automation.PSCredential($u,$p) Enter-PSSession -ComputerName <ホスト名>.cloudapp.net -Credential $c -UseSSL -SessionOption (New-PSSessionOption -SkipCACheck)
-UseSSL
-UseSSL パラメータがないと、よく見るエラーが返ってきます。Azure 仮想マシンのエンドポイントを確認するとWinRMのパブリック ポート/プライベート ポートは5986になっています。
Microsoft サポート「Configuring WINRM for HTTPS」を確認すると、
Windows 7以上のWinRM HTTPSは通常5986を使うと説明されており、これよりHTTPSが必要である事が分かります。
-SessionOption (New-PSSessionOption -SkipCACheck)
-SessionOption パラメーターなしでリモート接続するとサーバー証明書がない旨のエラーが出力されました。そこで、New-PSSessionOption -SkipCACheckを-SessionOption パラメーターにセットし実行する事で、サーバー証明書は確認されなくなりました。
この方法は簡単に接続出来ましたが、セキュリティ レベルが低下する問題があります。
「New-PSSessionOption」コマンドレットの-SkipCACheck パラメーターには
Use this option only when the remote computer is trusted by using another mechanism, such as when the remote computer is part of a network that is physically secure and isolated or when the remote computer is listed as a trusted host in a WinRM configuration.と説明されているとおり「クライアントで信頼できるリモート コンピューターを登録する」などが必要のようですね。
環境を把握し可能な限りセキュアな状況で利用したいところです。
Azure 仮想マシンへリモート接続~方法2
方法2は、サーバー証明書を作成・インポートした上でPowerShell からリモート接続します。こちらが本命ですよね。
SQLMag.comさんの記事「Use PowerShell Remoting to Manage Azure VMs」やMichael Washamさんのブログ記事「Windows Azure PowerShell Updates for IaaS GA」を参考になりました。ありがとうございます。
下記スクリプトは、Azure 仮想マシンのサーバー証明書をインストールする関数になります。
なお、実行には管理者権限が必要です。
下記スクリプトは、Azure 仮想マシンのサーバー証明書をインストールする関数になります。
なお、実行には管理者権限が必要です。
function Import-AzureVMCert{
param(
[string]$ServiceName,
[string]$Name
)
try{
#Get AzureVM
$vm = Get-AzureVM -ServiceName $ServiceName -Name $Name
#Get WinRM Certificate Thumbprint
$VmCert = $vm.VM.DefaultWinRMCertificateThumbprint
#Get Azure Certificate
$X509 = Get-AzureCertificate -ServiceName $ServiceName -Thumbprint $VmCert -ThumbprintAlgorithm sha1
#Create Temp File
$CertFile = [IO.Path]::GetTempFileName()
$X509.Data | Out-File $CertFile
#Install Certificate
$VMCert2 = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2 $CertFile
$store = New-Object System.Security.Cryptography.X509Certificates.X509Store "Root", "LocalMachine"
$store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite)
$store.Add($VMCert2)
$store.Close()
#Delete Temp FIle
Remove-Item $CertFile
Write-Host "Install AzureVM Certificate - Complete!" -ForegroundColor Yellow
}catch{
Write-Host "Install AzureVM Certificate - Error!!!" -ForegroundColor Red
}
}Internet Explorerのインターネット オプションからサーバー証明書を確認、インストールされていました。確認後、Enter-PSSession コマンドレットを実行すると無事接続しました。
$u = "WSEのユーザー アカウント名" $p = "パスワード" | ConvertTo-SecureString -AsPlainText -Force $c = New-Object System.Management.Automation.PSCredential($u,$p) Enter-PSSession -ComputerName <ホスト名>.cloudapp.net -Credential $c -UseSSL