本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
DeleteMaintenanceWindow
搭配 AWS SDK或 使用 CLI
下列程式碼範例示範如何使用 DeleteMaintenanceWindow
。
動作範例是大型程式的程式碼摘錄,必須在內容中執行。您可以在下列程式碼範例的內容中看到此動作:
- CLI
-
- AWS CLI
-
若要刪除維護時段
此
delete-maintenance-window
範例會移除指定的維護時段。aws ssm delete-maintenance-window \ --window-id
"mw-1a2b3c4d5e6f7g8h9"
輸出:
{ "WindowId":"mw-1a2b3c4d5e6f7g8h9" }
如需詳細資訊,請參閱 AWS Systems Manager 使用者指南中的刪除維護時段 (AWS CLI)。
-
如需API詳細資訊,請參閱 命令參考 DeleteMaintenanceWindow
中的 。 AWS CLI
-
- Java
-
- SDK 適用於 Java 2.x
-
注意
還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 /** * Deletes an AWS SSM Maintenance Window asynchronously. * * @param winId The ID of the Maintenance Window to delete. * <p> * This method initiates an asynchronous request to delete an SSM Maintenance Window. * If an exception occurs, it handles the error appropriately. */ public void deleteMaintenanceWindow(String winId) { DeleteMaintenanceWindowRequest windowRequest = DeleteMaintenanceWindowRequest.builder() .windowId(winId) .build(); CompletableFuture<Void> future = CompletableFuture.runAsync(() -> { getAsyncClient().deleteMaintenanceWindow(windowRequest) .thenAccept(response -> { System.out.println("The maintenance window was successfully deleted."); }) .exceptionally(ex -> { throw new CompletionException(ex); }).join(); }).exceptionally(ex -> { Throwable cause = (ex instanceof CompletionException) ? ex.getCause() : ex; if (cause instanceof SsmException) { throw new RuntimeException("SSM error: " + cause.getMessage(), cause); } else { throw new RuntimeException("Unexpected error: " + cause.getMessage(), cause); } }); try { future.join(); } catch (CompletionException ex) { throw ex.getCause() instanceof RuntimeException ? (RuntimeException) ex.getCause() : ex; } }
-
如需API詳細資訊,請參閱 參考 DeleteMaintenanceWindow中的 。 AWS SDK for Java 2.x API
-
- JavaScript
-
- SDK 適用於 JavaScript (v3)
-
注意
還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 import { DeleteMaintenanceWindowCommand, SSMClient } from "@aws-sdk/client-ssm"; import { parseArgs } from "util"; /** * Delete an SSM maintenance window. * @param {{ windowId: string }} */ export const main = async ({ windowId }) => { const client = new SSMClient({}); try { await client.send( new DeleteMaintenanceWindowCommand({ WindowId: windowId }), ); console.log("Maintenance window '" + windowId + "' deleted."); return { Deleted: true }; } catch (caught) { if (caught instanceof Error && caught.name === "MissingParameter") { console.warn(`${caught.message}. Did you provide this value?`); } else { throw caught; } } };
-
如需API詳細資訊,請參閱 參考 DeleteMaintenanceWindow中的 。 AWS SDK for JavaScript API
-
- PowerShell
-
- 適用於 的工具 PowerShell
-
範例 1:此範例會移除維護時段。
Remove-SSMMaintenanceWindow -WindowId "mw-06d59c1a07c022145"
輸出:
mw-06d59c1a07c022145
-
如需API詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考 DeleteMaintenanceWindow中的 。
-
- Python
-
- SDK for Python (Boto3)
-
注意
還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 class MaintenanceWindowWrapper: """Encapsulates AWS Systems Manager maintenance window actions.""" def __init__(self, ssm_client): """ :param ssm_client: A Boto3 Systems Manager client. """ self.ssm_client = ssm_client self.window_id = None self.name = None @classmethod def from_client(cls): ssm_client = boto3.client("ssm") return cls(ssm_client) def delete(self): """ Delete the associated AWS Systems Manager maintenance window. """ if self.window_id is None: return try: self.ssm_client.delete_maintenance_window(WindowId=self.window_id) logger.info("Deleted maintenance window %s.", self.window_id) print(f"Deleted maintenance window {self.name}") self.window_id = None except ClientError as err: logger.error( "Couldn't delete maintenance window %s. Here's why: %s: %s", self.window_id, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise
-
如需API詳細資訊,請參閱 DeleteMaintenanceWindow 中的 AWS SDK for Python (Boto3) API參考 。
-
如需開發人員指南和程式碼範例的完整清單 AWS SDK,請參閱 搭配 使用 Systems Manager AWS SDK。本主題也包含有關入門的資訊,以及先前SDK版本的詳細資訊。