Azure Cloud Shell の PowerShell のクイックスタートQuickstart for PowerShell in Azure Cloud Shell
このドキュメントでは、Azure Portal で Cloud Shell の PowerShell を使う方法について詳しく説明します。This document details how to use the PowerShell in Cloud Shell in the Azure portal.
注意
Azure Cloud Shell の Bash のクイックスタートもあります。A Bash in Azure Cloud Shell Quickstart is also available.
Cloud Shell の起動Start Cloud Shell
Azure Portal 上部のナビゲーションの [Cloud Shell] ボタンをクリックしますClick on Cloud Shell button from the top navigation bar of the Azure portal
ドロップダウンで PowerShell 環境を選ぶと、Azure ドライブ
(Azure:)
になりますSelect the PowerShell environment from the drop-down and you will be in Azure drive(Azure:)
PowerShell コマンドを実行するRun PowerShell commands
次のように、通常の PowerShell コマンドを Cloud Shell で実行します。Run regular PowerShell commands in the Cloud Shell, such as:
PS Azure:\> Get-Date
# Expected Output
Friday, July 27, 2018 7:08:48 AM
PS Azure:\> Get-AzVM -Status
# Expected Output
ResourceGroupName Name Location VmSize OsType ProvisioningState PowerState
----------------- ---- -------- ------ ------ ----------------- ----------
MyResourceGroup2 Demo westus Standard_DS1_v2 Windows Succeeded running
MyResourceGroup MyVM1 eastus Standard_DS1 Windows Succeeded running
MyResourceGroup MyVM2 eastus Standard_DS2_v2_Promo Windows Succeeded deallocated
仮想マシンを操作するInteract with virtual machines
VirtualMachines
ディレクトリを使うと、現在のサブスクリプションの下にあるすべての仮想マシンを確認できます。You can find all your virtual machines under the current subscription via VirtualMachines
directory.
PS Azure:\MySubscriptionName\VirtualMachines> dir
Directory: Azure:\MySubscriptionName\VirtualMachines
Name ResourceGroupName Location VmSize OsType NIC ProvisioningState PowerState
---- ----------------- -------- ------ ------ --- ----------------- ----------
TestVm1 MyResourceGroup1 westus Standard_DS2_v2 Windows my2008r213 Succeeded stopped
TestVm2 MyResourceGroup1 westus Standard_DS1_v2 Windows jpstest Succeeded deallocated
TestVm10 MyResourceGroup2 eastus Standard_DS1_v2 Windows mytest Succeeded running
リモート VM 間で PowerShell スクリプトを呼び出すInvoke PowerShell script across remote VMs
警告
Azure VM のリモート管理のトラブルシューティングに関するページを参照してください。Please refer to Troubleshooting remote management of Azure VMs.
MyVM1 という VM があるとすると、Invoke-AzVMCommand
を使ってリモート マシンの PowerShell スクリプト ブロックを呼び出すことができます。Assuming you have a VM, MyVM1, let's use Invoke-AzVMCommand
to invoke a PowerShell script block on the remote machine.
Enable-AzVMPSRemoting -Name MyVM1 -ResourceGroupname MyResourceGroup
Invoke-AzVMCommand -Name MyVM1 -ResourceGroupName MyResourceGroup -Scriptblock {Get-ComputerInfo} -Credential (Get-Credential)
また、最初に VirtualMachines ディレクトリに移動して、次のように Invoke-AzVMCommand
を実行することもできます。You can also navigate to the VirtualMachines directory first and run Invoke-AzVMCommand
as follows.
PS Azure:\> cd MySubscriptionName\ResourceGroups\MyResourceGroup\Microsoft.Compute\virtualMachines
PS Azure:\MySubscriptionName\ResourceGroups\MyResourceGroup\Microsoft.Compute\virtualMachines> Get-Item MyVM1 | Invoke-AzVMCommand -Scriptblock {Get-ComputerInfo} -Credential (Get-Credential)
# You will see output similar to the following:
PSComputerName : 65.52.28.207
RunspaceId : 2c2b60da-f9b9-4f42-a282-93316cb06fe1
WindowsBuildLabEx : 14393.1066.amd64fre.rs1_release_sec.170327-1835
WindowsCurrentVersion : 6.3
WindowsEditionId : ServerDatacenter
WindowsInstallationType : Server
WindowsInstallDateFromRegistry : 5/18/2017 11:26:08 PM
WindowsProductId : 00376-40000-00000-AA947
WindowsProductName : Windows Server 2016 Datacenter
WindowsRegisteredOrganization :
...
リモート VM に対話形式でログオンするInteractively log on to a remote VM
Enter-AzVM
を使うと、Azure で実行されている VM に対話形式でログインできます。You can use Enter-AzVM
to interactively log into a VM running in Azure.
PS Azure:\> Enter-AzVM -Name MyVM1 -ResourceGroupName MyResourceGroup -Credential (Get-Credential)
また、最初に VirtualMachines
ディレクトリに移動して、次のように Enter-AzVM
を実行することもできますYou can also navigate to the VirtualMachines
directory first and run Enter-AzVM
as follows
PS Azure:\MySubscriptionName\ResourceGroups\MyResourceGroup\Microsoft.Compute\virtualMachines> Get-Item MyVM1 | Enter-AzVM -Credential (Get-Credential)
WebApps を検出するDiscover WebApps
WebApps
ディレクトリに入ることで、Web アプリ リソース間を簡単に移動することができます。By entering into the WebApps
directory, you can easily navigate your web apps resources
PS Azure:\MySubscriptionName> dir .\WebApps\
Directory: Azure:\MySubscriptionName\WebApps
Name State ResourceGroup EnabledHostNames Location
---- ----- ------------- ---------------- --------
mywebapp1 Stopped MyResourceGroup1 {mywebapp1.azurewebsites.net... West US
mywebapp2 Running MyResourceGroup2 {mywebapp2.azurewebsites.net... West Europe
mywebapp3 Running MyResourceGroup3 {mywebapp3.azurewebsites.net... South Central US
# You can use Azure cmdlets to Start/Stop your web apps
PS Azure:\MySubscriptionName\WebApps> Start-AzWebApp -Name mywebapp1 -ResourceGroupName MyResourceGroup1
Name State ResourceGroup EnabledHostNames Location
---- ----- ------------- ---------------- --------
mywebapp1 Running MyResourceGroup1 {mywebapp1.azurewebsites.net ... West US
# Refresh the current state with -Force
PS Azure:\MySubscriptionName\WebApps> dir -Force
Directory: Azure:\MySubscriptionName\WebApps
Name State ResourceGroup EnabledHostNames Location
---- ----- ------------- ---------------- --------
mywebapp1 Running MyResourceGroup1 {mywebapp1.azurewebsites.net... West US
mywebapp2 Running MyResourceGroup2 {mywebapp2.azurewebsites.net... West Europe
mywebapp3 Running MyResourceGroup3 {mywebapp3.azurewebsites.net... South Central US
SSHSSH
SSH を使ってサーバーまたは VM に対する認証を行うには、Cloud Shell で公開/秘密キー ペアを生成して、リモート マシン上の authorized_keys
(/home/user/.ssh/authorized_keys
など) に公開キーを発行します。To authenticate to servers or VMs using SSH, generate the public-private key pair in Cloud Shell and publish the public key to authorized_keys
on the remote machine, such as /home/user/.ssh/authorized_keys
.
注意
ssh-keygen
を使って SSH の公開/秘密キーを作成し、Cloud Shell でそれらを $env:USERPROFILE\.ssh
に発行できます。You can create SSH private-public keys using ssh-keygen
and publish them to $env:USERPROFILE\.ssh
in Cloud Shell.
SSH の使用Using SSH
こちらの説明に従い、Azure PowerShell コマンドレットを使って新しい VM 構成を作成します。Follow instructions here to create a new VM configuration using Azure PowerShell cmdlets.
New-AzVM
を呼び出してデプロイを始める前に、SSH 公開キーを VM の構成に追加します。Before calling into New-AzVM
to kick off the deployment, add SSH public key to the VM configuration.
新しく作成される VM には ~\.ssh\authorized_keys
にある公開キーが含まれ、それにより VM への資格情報不要の SSH セッションが有効になります。The newly created VM will contain the public key in the ~\.ssh\authorized_keys
location, thereby enabling credential-free SSH session to the VM.
# Create VM config object - $vmConfig using instructions on linked page above
# Generate SSH keys in Cloud Shell
ssh-keygen -t rsa -b 2048 -f $HOME\.ssh\id_rsa
# Ensure VM config is updated with SSH keys
$sshPublicKey = Get-Content "$HOME\.ssh\id_rsa.pub"
Add-AzVMSshPublicKey -VM $vmConfig -KeyData $sshPublicKey -Path "/home/azureuser/.ssh/authorized_keys"
# Create a virtual machine
New-AzVM -ResourceGroupName <yourResourceGroup> -Location <vmLocation> -VM $vmConfig
# SSH to the VM
ssh azureuser@MyVM.Domain.Com
使用可能なコマンドを一覧表示するList available commands
Azure
ドライブで、「Get-AzCommand
」と入力してコンテキスト固有の Azure コマンドを取得します。Under Azure
drive, type Get-AzCommand
to get context-specific Azure commands.
または、Get-Command *az* -Module Az.*
を使うと、使用できる Azure コマンドをいつでも検索できます。Alternatively, you can always use Get-Command *az* -Module Az.*
to find out the available Azure commands.
カスタム モジュールをインストールするInstall custom modules
Install-Module
を実行して、PowerShell ギャラリーからモジュールをインストールできます。You can run Install-Module
to install modules from the PowerShell Gallery.
Get-HelpGet-Help
Azure Cloud Shell の PowerShell についての情報を取得するには、「Get-Help
」と入力します。Type Get-Help
to get information about PowerShell in Azure Cloud Shell.
Get-Help
特定のコマンドの場合は、Get-Help
の後にコマンドレットを指定します。For a specific command, you can still do Get-Help
followed by a cmdlet.
Get-Help Get-AzVM
Azure Files を使ってデータを保存するUse Azure Files to store your data
たとえば helloworld.ps1
といったスクリプトを作成して clouddrive
に保存し、それを異なるシェル セッションで使うことができます。You can create a script, say helloworld.ps1
, and save it to your clouddrive
to use it across shell sessions.
cd $HOME\clouddrive
# Create a new file in clouddrive directory
New-Item helloworld.ps1
# Open the new file for editing
code .\helloworld.ps1
# Add the content, such as 'Hello World!'
.\helloworld.ps1
Hello World!
Cloud Shell で PowerShell を次に使用するときは、helloworld.ps1
ファイルが Azure Files 共有をマウントした $HOME\clouddrive
ディレクトリにあります。Next time when you use PowerShell in Cloud Shell, the helloworld.ps1
file will exist under the $HOME\clouddrive
directory that mounts your Azure Files share.
カスタム プロファイルを使うUse custom profile
PowerShell プロファイル (profile.ps1
または Microsoft.PowerShell_profile.ps1
) を作成することで、PowerShell 環境をカスタマイズできます。You can customize your PowerShell environment, by creating PowerShell profile(s) - profile.ps1
(or Microsoft.PowerShell_profile.ps1
).
それを $profile.CurrentUserAllHosts
(または $profile.CurrentUserAllHosts
) に保存して、Cloud Shell セッションのすべての PowerShell で読み込めるようにします。Save it under $profile.CurrentUserAllHosts
(or $profile.CurrentUserAllHosts
), so that it can be loaded in every PowerShell in Cloud Shell session.
プロファイルの作成方法については、「プロファイルについて」を参照してください。For how to create a profile, refer to About Profiles.
Git を使うUse Git
Cloud Shell で Git リポジトリを複製するには、個人用アクセス トークンを作成し、それをユーザー名として使う必要があります。To clone a Git repo in the Cloud Shell, you need to create a personal access token and use it as the username. トークンを作成した後は、次のようにしてリポジトリを複製します。Once you have your token, clone the repository as follows:
git clone https://<your-access-token>@github.com/username/repo.git
シェルを終了します。Exit the shell
「exit
」と入力してセッションを終了します。Type exit
to terminate the session.