文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 AWS SDK 範例
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 Tools for PowerShell V5 的 WorkSpaces 範例
下列程式碼範例示範如何使用 AWS Tools for PowerShell V5 搭配 WorkSpaces 來執行動作和實作常見案例。
Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數,但您可以在其相關情境中查看內容中的動作。
每個範例都包含完整原始程式碼的連結,您可以在其中找到如何在內容中設定和執行程式碼的指示。
主題
動作
以下程式碼範例顯示如何使用 Approve-WKSIpRule。
- PowerShell V5 的工具
-
範例 1:此範例會將規則新增至現有的 IP 群組
$Rule = @( @{IPRule = "10.1.0.0/0"; RuleDesc = "First Rule Added"}, @{IPRule = "10.2.0.0/0"; RuleDesc = "Second Rule Added"} ) Approve-WKSIpRule -GroupId wsipg-abcnx2fcw -UserRule $Rule-
如需 API 詳細資訊,請參閱AWS Tools for PowerShell 《 Cmdlet Reference (V5)》中的 AuthorizeIpRules。
-
以下程式碼範例顯示如何使用 Copy-WKSWorkspaceImage。
- PowerShell V5 的工具
-
範例 1:此範例會將具有指定 ID 的工作區映像從 us-west-2 複製到名為 "CopiedImageTest" 的目前區域
Copy-WKSWorkspaceImage -Name CopiedImageTest -SourceRegion us-west-2 -SourceImageId wsi-djfoedhw6輸出:
wsi-456abaqfe-
如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet Reference (V5) 中的 CopyWorkspaceImage。
-
以下程式碼範例顯示如何使用 Edit-WKSClientProperty。
- PowerShell V5 的工具
-
範例 1:此範例會啟用工作區用戶端的重新連線
Edit-WKSClientProperty -Region us-west-2 -ClientProperties_ReconnectEnabled "ENABLED" -ResourceId d-123414a369-
如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考 (V5) 中的 ModifyClientProperties。
-
以下程式碼範例顯示如何使用 Edit-WKSSelfServicePermission。
- PowerShell V5 的工具
-
範例 1:此範例可讓自助服務許可變更指定目錄的運算類型和增加磁碟區大小
Edit-WKSSelfservicePermission -Region us-west-2 -ResourceId d-123454a369 -SelfservicePermissions_ChangeComputeType ENABLED -SelfservicePermissions_IncreaseVolumeSize ENABLED-
如需 API 詳細資訊,請參閱AWS Tools for PowerShell 《 Cmdlet Reference (V5)》中的 ModifySelfservicePermissions。
-
以下程式碼範例顯示如何使用 Edit-WKSWorkspaceAccessProperty。
- PowerShell V5 的工具
-
範例 1:此範例可為指定的目錄啟用 Android 和 Chrome OS 上的工作區存取
Edit-WKSWorkspaceAccessProperty -Region us-west-2 -ResourceId d-123454a369 -WorkspaceAccessProperties_DeviceTypeAndroid ALLOW -WorkspaceAccessProperties_DeviceTypeChromeOs ALLOW-
如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考 (V5) 中的 ModifyWorkspaceAccessProperties。
-
以下程式碼範例顯示如何使用 Edit-WKSWorkspaceCreationProperty。
- PowerShell V5 的工具
-
範例 1:此範例可讓網際網路存取和維護模式在建立工作區時成為預設值
Edit-WKSWorkspaceCreationProperty -Region us-west-2 -ResourceId d-123454a369 -WorkspaceCreationProperties_EnableInternetAccess $true -WorkspaceCreationProperties_EnableMaintenanceMode $true-
如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考 (V5) 中的 ModifyWorkspaceCreationProperties。
-
以下程式碼範例顯示如何使用 Edit-WKSWorkspaceProperty。
- PowerShell V5 的工具
-
範例 1:此範例會將指定工作區的工作區執行模式屬性變更為自動停止
Edit-WKSWorkspaceProperty -WorkspaceId ws-w361s100v -Region us-west-2 -WorkspaceProperties_RunningMode AUTO_STOP-
如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考 (V5) 中的 ModifyWorkspaceProperties。
-
以下程式碼範例顯示如何使用 Edit-WKSWorkspaceState。
- PowerShell V5 的工具
-
範例 1:此範例將指定工作區的狀態變更為可用
Edit-WKSWorkspaceState -WorkspaceId ws-w361s100v -Region us-west-2 -WorkspaceState AVAILABLE-
如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet Reference (V5) 中的 ModifyWorkspaceState。
-
以下程式碼範例顯示如何使用 Get-WKSClientProperty。
- PowerShell V5 的工具
-
範例 1:此範例取得指定目錄之工作區用戶端的用戶端屬性
Get-WKSClientProperty -ResourceId d-223562a123-
如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet Reference (V5) 中的 DescribeClientProperties。
-
以下程式碼範例顯示如何使用 Get-WKSIpGroup。
- PowerShell V5 的工具
-
範例 1:此範例取得指定區域中指定 IP 群組的詳細資訊
Get-WKSIpGroup -Region us-east-1 -GroupId wsipg-8m1234v45輸出:
GroupDesc GroupId GroupName UserRules --------- ------- --------- --------- wsipg-8m1234v45 TestGroup {Amazon.WorkSpaces.Model.IpRuleItem, Amazon.WorkSpaces.Model.IpRuleItem}-
如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet Reference (V5) 中的 DescribeIpGroups。
-
以下程式碼範例顯示如何使用 Get-WKSTag。
- PowerShell V5 的工具
-
範例 1:此範例會擷取指定工作區的標籤
Get-WKSTag -WorkspaceId ws-w361s234r -Region us-west-2輸出:
Key Value --- ----- auto-delete no purpose Workbench-
如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet Reference (V5) 中的 DescribeTags。
-
以下程式碼範例顯示如何使用 Get-WKSWorkspace。
- PowerShell V5 的工具
-
範例 1:擷取管道中所有 WorkSpaces 的詳細資訊。
Get-WKSWorkspace輸出:
BundleId : wsb-1a2b3c4d ComputerName : DirectoryId : d-1a2b3c4d ErrorCode : ErrorMessage : IpAddress : RootVolumeEncryptionEnabled : False State : PENDING SubnetId : UserName : myuser UserVolumeEncryptionEnabled : False VolumeEncryptionKey : WorkspaceId : ws-1a2b3c4d WorkspaceProperties : Amazon.WorkSpaces.Model.WorkspaceProperties範例 2:此命令顯示
us-west-2區域中WorkSpaceProperties工作區的子屬性值。如需 子屬性的詳細資訊WorkSpaceProperties,請參閱 https://https://docs.aws.amazon.com/workspaces/latest/api/API_WorkspaceProperties.html。(Get-WKSWorkspace -Region us-west-2 -WorkSpaceId ws-xdaf7hc9s).WorkspaceProperties輸出:
ComputeTypeName : STANDARD RootVolumeSizeGib : 80 RunningMode : AUTO_STOP RunningModeAutoStopTimeoutInMinutes : 60 UserVolumeSizeGib : 50範例 3:此命令顯示
us-west-2區域中RootVolumeSizeGibWorkSpaceProperties工作區的 子屬性值。GiB 的根磁碟區大小為 80。(Get-WKSWorkspace -Region us-west-2 -WorkSpaceId ws-xdaf7hc9s).WorkspaceProperties.RootVolumeSizeGib輸出:
80-
如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考 (V5) 中的 DescribeWorkspaces。
-
以下程式碼範例顯示如何使用 Get-WKSWorkspaceBundle。
- PowerShell V5 的工具
-
範例 1:此範例會擷取目前區域中所有工作區套件的詳細資訊
Get-WKSWorkspaceBundle輸出:
BundleId : wsb-sfhdgv342 ComputeType : Amazon.WorkSpaces.Model.ComputeType Description : This bundle is custom ImageId : wsi-235aeqges LastUpdatedTime : 12/26/2019 06:44:07 Name : CustomBundleTest Owner : 233816212345 RootStorage : Amazon.WorkSpaces.Model.RootStorage UserStorage : Amazon.WorkSpaces.Model.UserStorage-
如需 API 詳細資訊,請參閱AWS Tools for PowerShell 《 Cmdlet Reference (V5)》中的 DescribeWorkspaceBundles。
-
以下程式碼範例顯示如何使用 Get-WKSWorkspaceDirectory。
- PowerShell V5 的工具
-
範例 1:此範例列出已註冊目錄的目錄詳細資訊
Get-WKSWorkspaceDirectory輸出:
Alias : TestWorkspace CustomerUserName : Administrator DirectoryId : d-123414a369 DirectoryName : TestDirectory.com DirectoryType : MicrosoftAD DnsIpAddresses : {172.31.43.45, 172.31.2.97} IamRoleId : arn:aws:iam::761234567801:role/workspaces_RoleDefault IpGroupIds : {} RegistrationCode : WSpdx+4RRT43 SelfservicePermissions : Amazon.WorkSpaces.Model.SelfservicePermissions State : REGISTERED SubnetIds : {subnet-1m3m7b43, subnet-ard11aba} Tenancy : SHARED WorkspaceAccessProperties : Amazon.WorkSpaces.Model.WorkspaceAccessProperties WorkspaceCreationProperties : Amazon.WorkSpaces.Model.DefaultWorkspaceCreationProperties WorkspaceSecurityGroupId : sg-0ed2441234a123c43-
如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考 (V5) 中的 DescribeWorkspaceDirectories。
-
以下程式碼範例顯示如何使用 Get-WKSWorkspaceImage。
- PowerShell V5 的工具
-
範例 1:此範例會擷取區域中所有影像的所有詳細資訊
Get-WKSWorkspaceImage輸出:
Description :This image is copied from another image ErrorCode : ErrorMessage : ImageId : wsi-345ahdjgo Name : CopiedImageTest OperatingSystem : Amazon.WorkSpaces.Model.OperatingSystem RequiredTenancy : DEFAULT State : AVAILABLE-
如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet Reference (V5) 中的 DescribeWorkspaceImages。
-
以下程式碼範例顯示如何使用 Get-WKSWorkspaceSnapshot。
- PowerShell V5 的工具
-
範例 1:此範例顯示為指定工作區建立的最新快照時間戳記
Get-WKSWorkspaceSnapshot -WorkspaceId ws-w361s100v輸出:
RebuildSnapshots RestoreSnapshots ---------------- ---------------- {Amazon.WorkSpaces.Model.Snapshot} {Amazon.WorkSpaces.Model.Snapshot}-
如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考 (V5) 中的 DescribeWorkspaceSnapshots。
-
以下程式碼範例顯示如何使用 Get-WKSWorkspacesConnectionStatus。
- PowerShell V5 的工具
-
範例 1:此範例會擷取指定工作區的連線狀態
Get-WKSWorkspacesConnectionStatus -WorkspaceId ws-w123s234r-
如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet Reference (V5) 中的 DescribeWorkspacesConnectionStatus。
-
以下程式碼範例顯示如何使用 New-WKSIpGroup。
- PowerShell V5 的工具
-
範例 1:此範例會建立名為 FreshEmptyIpGroup 的空 Ip 群組
New-WKSIpGroup -GroupName "FreshNewIPGroup"輸出:
wsipg-w45rty4ty-
如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet Reference (V5) 中的 CreateIpGroup。
-
以下程式碼範例顯示如何使用 New-WKSTag。
- PowerShell V5 的工具
-
範例 1:此範例會將新標籤新增至名為 的工作區
ws-wsname。標籤的索引鍵為 "Name",索引鍵值為AWS_Workspace。$tag = New-Object Amazon.WorkSpaces.Model.Tag $tag.Key = "Name" $tag.Value = "AWS_Workspace" New-WKSTag -Region us-west-2 -WorkspaceId ws-wsname -Tag $tag範例 2:此範例會將多個標籤新增至名為 的工作區
ws-wsname。一個標籤的索引鍵為「Name」且索引鍵值為AWS_Workspace;另一個標籤的標籤索引鍵為「Stage」且索引鍵值為「Test」。$tag = New-Object Amazon.WorkSpaces.Model.Tag $tag.Key = "Name" $tag.Value = "AWS_Workspace" $tag2 = New-Object Amazon.WorkSpaces.Model.Tag $tag2.Key = "Stage" $tag2.Value = "Test" New-WKSTag -Region us-west-2 -WorkspaceId ws-wsname -Tag $tag,$tag2-
如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet Reference (V5) 中的 CreateTags。
-
以下程式碼範例顯示如何使用 New-WKSWorkspace。
- PowerShell V5 的工具
-
範例 1:為提供的套件、目錄和使用者建立 WorkSpace。
New-WKSWorkspace -Workspace @{"BundleID" = "wsb-1a2b3c4d"; "DirectoryId" = "d-1a2b3c4d"; "UserName" = "USERNAME"}範例 2:此範例會建立多個 WorkSpaces
New-WKSWorkspace -Workspace @{"BundleID" = "wsb-1a2b3c4d"; "DirectoryId" = "d-1a2b3c4d"; "UserName" = "USERNAME_1"},@{"BundleID" = "wsb-1a2b3c4d"; "DirectoryId" = "d-1a2b3c4d"; "UserName" = "USERNAME_2"}-
如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考 (V5) 中的 CreateWorkspaces。
-
以下程式碼範例顯示如何使用 Register-WKSIpGroup。
- PowerShell V5 的工具
-
範例 1:此範例會向指定的目錄註冊指定的 IP 群組
Register-WKSIpGroup -GroupId wsipg-23ahsdres -DirectoryId d-123412e123-
如需 API 詳細資訊,請參閱AWS Tools for PowerShell 《 Cmdlet Reference (V5)》中的 AssociateIpGroups。
-
以下程式碼範例顯示如何使用 Register-WKSWorkspaceDirectory。
- PowerShell V5 的工具
-
範例 1:此範例會註冊 Workspaces Service 的指定目錄
Register-WKSWorkspaceDirectory -DirectoryId d-123412a123 -EnableWorkDoc $false-
如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考 (V5) 中的 RegisterWorkspaceDirectory。
-
以下程式碼範例顯示如何使用 Remove-WKSIpGroup。
- PowerShell V5 的工具
-
範例 1:此範例會刪除指定的 IP 群組
Remove-WKSIpGroup -GroupId wsipg-32fhgtred輸出:
Confirm Are you sure you want to perform this action? Performing the operation "Remove-WKSIpGroup (DeleteIpGroup)" on target "wsipg-32fhgtred". [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): Y-
如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet Reference (V5) 中的 DeleteIpGroup。
-
以下程式碼範例顯示如何使用 Remove-WKSTag。
- PowerShell V5 的工具
-
範例 1:此範例會移除與工作區相關聯的標籤
Remove-WKSTag -ResourceId ws-w10b3abcd -TagKey "Type"輸出:
Confirm Are you sure you want to perform this action? Performing the operation "Remove-WKSTag (DeleteTags)" on target "ws-w10b3abcd". [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): Y-
如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考 (V5) 中的 DeleteTags。
-
以下程式碼範例顯示如何使用 Remove-WKSWorkspace。
- PowerShell V5 的工具
-
範例 1:終止多個 WorkSpaces。使用 -Force 切換會停止 cmdlet 提示進行確認。
Remove-WKSWorkspace -WorkspaceId "ws-1a2b3c4d5","ws-6a7b8c9d0" -Force範例 2:擷取所有 WorkSpaces 的集合,並將 IDs 輸送至 Remove-WKSWorkspace 的 -WorkSpaceId 參數,以終止所有 WorkSpaces。Cmdlet 會在每個 WorkSpace 終止之前提示。若要隱藏確認提示,請新增 -Force 切換。
Get-WKSWorkspaces | Remove-WKSWorkspace範例 3:此範例示範如何傳遞定義要終止之 WorkSpaces 的 TerminateRequest 物件。除非也指定 -Force 切換參數,否則 cmdlet 會在繼續之前提示確認。
$arrRequest = @() $request1 = New-Object Amazon.WorkSpaces.Model.TerminateRequest $request1.WorkspaceId = 'ws-12345678' $arrRequest += $request1 $request2 = New-Object Amazon.WorkSpaces.Model.TerminateRequest $request2.WorkspaceId = 'ws-abcdefgh' $arrRequest += $request2 Remove-WKSWorkspace -Request $arrRequest-
如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet Reference (V5) 中的 TerminateWorkspaces。
-
以下程式碼範例顯示如何使用 Reset-WKSWorkspace。
- PowerShell V5 的工具
-
範例 1:重建指定的 WorkSpace。
Reset-WKSWorkspace -WorkspaceId "ws-1a2b3c4d"範例 2:擷取所有 WorkSpaces 的集合,並將 IDs 輸送至 Reset-WKSWorkspace 的 -WorkSpaceId 參數,導致 WorkSpaces 重建。
Get-WKSWorkspaces | Reset-WKSWorkspace-
如需 API 詳細資訊,請參閱AWS Tools for PowerShell 《 Cmdlet Reference (V5)》中的 RebuildWorkspaces。
-
以下程式碼範例顯示如何使用 Restart-WKSWorkspace。
- PowerShell V5 的工具
-
範例 1:重新啟動指定的 WorkSpace。
Restart-WKSWorkspace -WorkspaceId "ws-1a2b3c4d"範例 2:重新啟動多個 WorkSpaces。
Restart-WKSWorkspace -WorkspaceId "ws-1a2b3c4d","ws-5a6b7c8d"範例 3:擷取所有 WorkSpaces 的集合,並將 IDs 輸送至 Restart-WKSWorkspace 的 -WorkSpaceId 參數,導致 WorkSpaces 重新啟動。
Get-WKSWorkspaces | Restart-WKSWorkspace-
如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考 (V5) 中的 RebootWorkspaces。
-
以下程式碼範例顯示如何使用 Stop-WKSWorkspace。
- PowerShell V5 的工具
-
範例 1:停止多個 WorkSpaces。
Stop-WKSWorkspace -WorkspaceId "ws-1a2b3c4d5","ws-6a7b8c9d0"範例 2:擷取所有 WorkSpaces 的集合,並將 IDs 輸送至 Stop-WKSWorkspace 的 -WorkSpaceId 參數,導致 WorkSpaces 停止。
Get-WKSWorkspaces | Stop-WKSWorkspace範例 3:此範例示範如何傳遞 StopRequest 物件,定義要停止的 WorkSpaces。
$arrRequest = @() $request1 = New-Object Amazon.WorkSpaces.Model.StopRequest $request1.WorkspaceId = 'ws-12345678' $arrRequest += $request1 $request2 = New-Object Amazon.WorkSpaces.Model.StopRequest $request2.WorkspaceId = 'ws-abcdefgh' $arrRequest += $request2 Stop-WKSWorkspace -Request $arrRequest-
如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考 (V5) 中的 StopWorkspaces。
-
以下程式碼範例顯示如何使用 Unregister-WKSIpGroup。
- PowerShell V5 的工具
-
範例 1:此範例會從指定的目錄取消註冊指定的 IP 群組
Unregister-WKSIpGroup -GroupId wsipg-12abcdphq -DirectoryId d-123454b123-
如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet Reference (V5) 中的 DisassociateIpGroups。
-