

AWS Tools for PowerShell V4 已進入維護模式。

我們建議您遷移至 [AWS Tools for PowerShell V5](https://docs.aws.amazon.com/powershell/v5/userguide/)。如需如何遷移的其他詳細資訊和資訊，請參閱我們的[維護模式公告](https://aws.amazon.com/blogs/developer/aws-tools-for-powershell-v4-maintenance-mode-announcement/)。

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 Windows PowerShell 啟動 Amazon EC2 執行個體
<a name="pstools-ec2-launch"></a>

若要啟動 Amazon EC2 執行個體，您需要您在先前章節中建立的金鑰對和安全群組。您也需要 Amazon Machine Image (AMI) 的 ID。如需詳細資訊，請參閱下列 文件：
+  [建立金鑰對](pstools-ec2-keypairs.md) 
+  [使用 Windows PowerShell 建立安全群組](pstools-ec2-sg.md) 
+  [使用 Windows PowerShell 來尋找 Amazon Machine Image](pstools-ec2-get-amis.md) 

**重要**  
如果您啟動的執行個體不包含在免費方案內，則需要在啟動該執行個體後開始收費，收費根據執行個體的執行時間而定，即使其保持閒置狀態仍會計費。

**Topics**
+ [在 VPC 中啟動執行個體](#new-ec2instance-vpc)
+ [在 VPC 中啟動 Spot 執行個體](#new-ec2instance-spot)

## 在 VPC 中啟動執行個體
<a name="new-ec2instance-vpc"></a>

**警告**  
EC2-Classic 在 2022 年 8 月 15 日淘汰。建議您從 EC2-Classic 遷移至 VPC。如需詳細資訊，請參閱部落格文章 [EC2-Classic Networking 正在淘汰 – 以下是如何準備](https://aws.amazon.com/blogs/aws/ec2-classic-is-retiring-heres-how-to-prepare/)。

下列命令會在指定的私有子網路中建立單一 `m1.small` 執行個體。安全群組必須針對指定的子網路有效。

```
PS > New-EC2Instance `
    -ImageId ami-c49c0dac `
    -MinCount 1 -MaxCount 1 `
    -KeyName myPSKeyPair `
    -SecurityGroupId sg-5d293231 `
    -InstanceType m1.small `
    -SubnetId subnet-d60013bf

ReservationId   : r-b70a0ef1
OwnerId         : 123456789012
RequesterId     :
Groups          : {}
GroupName       : {}
Instances       : {}
```

您的執行個體一開始會處於 `pending` 狀態，但在幾分鐘之後就會進入 `running` 狀態。若要檢視執行個體的相關資訊，請使用 `Get-EC2Instance` cmdlet。如果您有多個執行個體，則可以使用 `Filter` 參數來篩選預留 ID 的結果。首先，建立 `Amazon.EC2.Model.Filter` 類型的物件。接下來，請呼叫使用篩選條件的 `Get-EC2Instance`，然後顯示 `Instances` 屬性。

```
PS > $reservation = New-Object 'collections.generic.list[string]'
PS > $reservation.add("r-b70a0ef1")
PS > $filter_reservation = New-Object Amazon.EC2.Model.Filter -Property @{Name = "reservation-id"; Values = $reservation}
PS > (Get-EC2Instance -Filter $filter_reservation).Instances

AmiLaunchIndex        : 0
Architecture          : x86_64
BlockDeviceMappings   : {/dev/sda1}
ClientToken           :
EbsOptimized          : False
Hypervisor            : xen
IamInstanceProfile    :
ImageId               : ami-c49c0dac
InstanceId            : i-5203422c
InstanceLifecycle     :
InstanceType          : m1.small
KernelId              :
KeyName               : myPSKeyPair
LaunchTime            : 12/2/2018 3:38:52 PM
Monitoring            : Amazon.EC2.Model.Monitoring
NetworkInterfaces     : {}
Placement             : Amazon.EC2.Model.Placement
Platform              : Windows
PrivateDnsName        :
PrivateIpAddress      : 10.25.1.11
ProductCodes          : {}
PublicDnsName         :
PublicIpAddress       : 198.51.100.245
RamdiskId             :
RootDeviceName        : /dev/sda1
RootDeviceType        : ebs
SecurityGroups        : {myPSSecurityGroup}
SourceDestCheck       : True
SpotInstanceRequestId :
SriovNetSupport       :
State                 : Amazon.EC2.Model.InstanceState
StateReason           :
StateTransitionReason :
SubnetId              : subnet-d60013bf
Tags                  : {}
VirtualizationType    : hvm
VpcId                 : vpc-a01106c2
```

## 在 VPC 中啟動 Spot 執行個體
<a name="new-ec2instance-spot"></a>

以下範例指令碼會請求指定子網路中的 Spot 執行個體。安全群組必須是您為 VPC 所建立，且包含指定的子網路。

```
$interface1 = New-Object Amazon.EC2.Model.InstanceNetworkInterfaceSpecification
$interface1.DeviceIndex = 0
$interface1.SubnetId = "subnet-b61f49f0"
$interface1.PrivateIpAddress = "10.0.1.5"
$interface1.Groups.Add("sg-5d293231")
Request-EC2SpotInstance `
    -SpotPrice 0.007 `
    -InstanceCount 1 `
    -Type one-time `
    -LaunchSpecification_ImageId ami-7527031c `
    -LaunchSpecification_InstanceType m1.small `
    -Region us-west-2 `
    -LaunchSpecification_NetworkInterfaces $interface1
```