

文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 [AWS SDK 範例](https://github.com/awsdocs/aws-doc-sdk-examples)。

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

# 使用 Tools for PowerShell V5 的 Amazon EFS 範例
<a name="powershell_5_efs_code_examples"></a>

下列程式碼範例示範如何使用 AWS Tools for PowerShell V5 搭配 Amazon EFS 來執行動作和實作常見案例。

*Actions* 是大型程式的程式碼摘錄，必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數，但您可以在其相關情境中查看內容中的動作。

每個範例均包含完整原始碼的連結，您可在連結中找到如何設定和執行內容中程式碼的相關指示。

**Topics**
+ [動作](#actions)

## 動作
<a name="actions"></a>

### `Edit-EFSMountTargetSecurityGroup`
<a name="efs_ModifyMountTargetSecurityGroups_powershell_5_topic"></a>

以下程式碼範例顯示如何使用 `Edit-EFSMountTargetSecurityGroup`。

**Tools for PowerShell V5**  
**範例 1：更新指定掛載目標的有效安全群組。最多可以指定 5 個，格式為 "sg-xxxxxxxx"。**  

```
Edit-EFSMountTargetSecurityGroup -MountTargetId fsmt-1a2b3c4d -SecurityGroup sg-group1,sg-group3
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V5)》**中的 [ModifyMountTargetSecurityGroups](https://docs.aws.amazon.com/powershell/v5/reference)。

### `Get-EFSFileSystem`
<a name="efs_DescribeFileSystems_powershell_5_topic"></a>

以下程式碼範例顯示如何使用 `Get-EFSFileSystem`。

**Tools for PowerShell V5**  
**範例 1：傳回區域中呼叫者帳戶擁有的所有檔案系統的集合。**  

```
Get-EFSFileSystem
```
**輸出：**  

```
CreationTime         : 5/26/2015 4:02:38 PM
CreationToken        : 1a2bff54-85e0-4747-bd95-7bc172c4f555
FileSystemId         : fs-1a2b3c4d
LifeCycleState       : available
Name                 :
NumberOfMountTargets : 0
OwnerId              : 123456789012
SizeInBytes          : Amazon.ElasticFileSystem.Model.FileSystemSize

CreationTime         : 5/26/2015 4:06:23 PM
CreationToken        : 2b4daa14-85e0-4747-bd95-7bc172c4f555
FileSystemId         : fs-4d3c2b1a
...
```
**範例 2：傳回指定檔案系統的詳細資訊。**  

```
Get-EFSFileSystem -FileSystemId fs-1a2b3c4d
```
**範例 3：使用建立檔案系統時指定的冪等性建立字符，傳回檔案系統的詳細資訊。**  

```
Get-EFSFileSystem -CreationToken 1a2bff54-85e0-4747-bd95-7bc172c4f555
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V5)》**中的 [DescribeFileSystems](https://docs.aws.amazon.com/powershell/v5/reference)。

### `Get-EFSMountTarget`
<a name="efs_DescribeMountTargets_powershell_5_topic"></a>

以下程式碼範例顯示如何使用 `Get-EFSMountTarget`。

**Tools for PowerShell V5**  
**範例 1：傳回與指定檔案系統相關聯的掛載目標集合。**  

```
Get-EFSMountTarget -FileSystemId fs-1a2b3c4d
```
**輸出：**  

```
FileSystemId       : fs-1a2b3c4d
IpAddress          : 10.0.0.131
LifeCycleState     : available
MountTargetId      : fsmt-1a2b3c4d
NetworkInterfaceId : eni-1a2b3c4d
OwnerId            : 123456789012
SubnetId           : subnet-1a2b3c4d
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V5)》**中的 [DescribeMountTargets](https://docs.aws.amazon.com/powershell/v5/reference)。

### `Get-EFSMountTargetSecurityGroup`
<a name="efs_DescribeMountTargetSecurityGroups_powershell_5_topic"></a>

以下程式碼範例顯示如何使用 `Get-EFSMountTargetSecurityGroup`。

**Tools for PowerShell V5**  
**範例 1：傳回目前指派給與掛載目標相關聯之網路介面的安全群組 ID。**  

```
Get-EFSMountTargetSecurityGroup -MountTargetId fsmt-1a2b3c4d
```
**輸出：**  

```
sg-1a2b3c4d
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V5)》**中的 [DescribeMountTargetSecurityGroups](https://docs.aws.amazon.com/powershell/v5/reference)。

### `Get-EFSTag`
<a name="efs_DescribeTags_powershell_5_topic"></a>

以下程式碼範例顯示如何使用 `Get-EFSTag`。

**Tools for PowerShell V5**  
**範例 1：傳回目前與指定檔案系統相關聯的標籤集合。**  

```
Get-EFSTag -FileSystemId fs-1a2b3c4d
```
**輸出：**  

```
Key         Value
---         -----
Name        My File System
tagkey1     tagvalue1
tagkey2     tagvalue2
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V5)》**中的 [DescribeTags](https://docs.aws.amazon.com/powershell/v5/reference)。

### `New-EFSFileSystem`
<a name="efs_CreateFileSystem_powershell_5_topic"></a>

以下程式碼範例顯示如何使用 `New-EFSFileSystem`。

**Tools for PowerShell V5**  
**範例 1：建立新的空檔案系統。用於確保冪等建立的字符會自動產生，並且可以從傳回物件的 `CreationToken` 成員存取。**  

```
New-EFSFileSystem
```
**輸出：**  

```
CreationTime         : 5/26/2015 4:02:38 PM
CreationToken        : 1a2bff54-85e0-4747-bd95-7bc172c4f555
FileSystemId         : fs-1a2b3c4d
LifeCycleState       : creating
Name                 :
NumberOfMountTargets : 0
OwnerId              : 123456789012
SizeInBytes          : Amazon.ElasticFileSystem.Model.FileSystemSize
```
**範例 2：使用自訂字符建立新的空檔案系統，以確保冪等建立。**  

```
New-EFSFileSystem -CreationToken "MyUniqueToken"
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V5)》**中的 [CreateFileSystem](https://docs.aws.amazon.com/powershell/v5/reference)。

### `New-EFSMountTarget`
<a name="efs_CreateMountTarget_powershell_5_topic"></a>

以下程式碼範例顯示如何使用 `New-EFSMountTarget`。

**Tools for PowerShell V5**  
**範例 1：建立檔案系統的新掛載目標。指定的子網路將用於判斷將在其中建立掛載目標的虛擬私有雲端 (VPC)，以及將自動指派的 IP 位址 (來自子網路的位址範圍)。指派的 IP 位址可用於在 Amazon EC2 執行個體上掛載此檔案系統。由於未指定安全群組，為目標建立的網路介面會與子網路 VPC 的預設安全群組相關聯。**  

```
New-EFSMountTarget -FileSystemId fs-1a2b3c4d -SubnetId subnet-1a2b3c4d
```
**輸出：**  

```
FileSystemId       : fs-1a2b3c4d
IpAddress          : 10.0.0.131
LifeCycleState     : creating
MountTargetId      : fsmt-1a2b3c4d
NetworkInterfaceId : eni-1a2b3c4d
OwnerId            : 123456789012
SubnetId           : subnet-1a2b3c4d
```
**範例 2：為具有自動指派 IP 位址的指定檔案系統，建立新的掛載目標。將掛載目標建立的網路介面，與指定的安全群組相關聯 (最多可指定 5 個，格式為 "sg-xxxxxxxx")**。  

```
New-EFSMountTarget -FileSystemId fs-1a2b3c4d -SubnetId subnet-1a2b3c4d -SecurityGroup sg-group1,sg-group2,sg-group3
```
**範例 3：為具有指定 IP 位址的指定檔案系統，建立新的掛載目標。**  

```
New-EFSMountTarget -FileSystemId fs-1a2b3c4d -SubnetId subnet-1a2b3c4d -IpAddress 10.0.0.131
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V5)》**中的 [CreateMountTarget](https://docs.aws.amazon.com/powershell/v5/reference)。

### `New-EFSTag`
<a name="efs_CreateTags_powershell_5_topic"></a>

以下程式碼範例顯示如何使用 `New-EFSTag`。

**Tools for PowerShell V5**  
**範例 1：將標籤集合套用至指定的檔案系統。如果檔案系統上已存在具有指定索引鍵的標籤，則標籤的值會更新。**  

```
New-EFSTag -FileSystemId fs-1a2b3c4d -Tag @{Key="tagkey1";Value="tagvalue1"},@{Key="tagkey2";Value="tagvalue2"}
```
**範例 2：設定指定檔案系統的名稱標籤。使用 Get-EFSFileSystem Cmdlet 時，此值會與其他檔案系統詳細資訊一起傳回。**  

```
New-EFSTag -FileSystemId fs-1a2b3c4d -Tag @{Key="Name";Value="My File System"}
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V5)》**中的 [CreateTags](https://docs.aws.amazon.com/powershell/v5/reference)。

### `Remove-EFSFileSystem`
<a name="efs_DeleteFileSystem_powershell_5_topic"></a>

以下程式碼範例顯示如何使用 `Remove-EFSFileSystem`。

**Tools for PowerShell V5**  
**範例 1：刪除不再使用的指定檔案系統 (如果檔案系統具有掛載目標，必須先將其移除)。在 Cmdlet 繼續進行之前，系統會提示您確認 - 若要隱藏確認，請使用 `-Force` 切換變數。**  

```
Remove-EFSFileSystem -FileSystemId fs-1a2b3c4d
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V5)》**中的 [DeleteFileSystem](https://docs.aws.amazon.com/powershell/v5/reference)。

### `Remove-EFSMountTarget`
<a name="efs_DeleteMountTarget_powershell_5_topic"></a>

以下程式碼範例顯示如何使用 `Remove-EFSMountTarget`。

**Tools for PowerShell V5**  
**範例 1：刪除指定的掛載目標。在繼續操作之前，系統會提示您確認。若要隱藏提示，請使用 `-Force` 切換變數。請注意，此操作會透過目標強制中斷檔案系統的任何掛載 - 如果可行，您可能需要考慮在執行此命令之前卸載檔案系統。**  

```
Remove-EFSMountTarget -MountTargetId fsmt-1a2b3c4d
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V5)》**中的 [DeleteMountTarget](https://docs.aws.amazon.com/powershell/v5/reference)。

### `Remove-EFSTag`
<a name="efs_DeleteTags_powershell_5_topic"></a>

以下程式碼範例顯示如何使用 `Remove-EFSTag`。

**Tools for PowerShell V5**  
**範例 1：從檔案系統刪除一或多個標籤的集合。在 Cmdlet 繼續進行之前，系統會提示您確認 - 若要隱藏確認，請使用 `-Force` 切換變數。**  

```
Remove-EFSTag -FileSystemId fs-1a2b3c4d -TagKey "tagkey1","tagkey2"
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V5)》**中的 [DeleteTags](https://docs.aws.amazon.com/powershell/v5/reference)。