使用 Microsoft Azure Automation 预览版来管理 Azure 服务

作者: Eamon O’Reilly ,自动化与集成首席项目经理

上周,我们发布了基于 System Center R2 服务管理自动化的 Microsoft Azure Automation 预览版服务。

关于该服务的更多信息,您可登陆网站查询。网址为:https://azure.microsoft.com/en-us/documentation/services/automation/

注册预览版本,可访问https://azure.microsoft.com/en-us/services/preview/ 并在 Azure Automation 下点击 Try it 链接。

或者观看 //Build 网站上的 Azure Automation 公告视频,网址为:https://channel9.msdn.com/Events/Build/2014/3-621,Joe 在视频的第 30 分钟开始介绍相关内容。

在预览版本中,您可以使用一些 Runbook 来管理 Azure 服务。假设您之前没有使用过 Azure PowerShell 命令来管理 Azure,我将带领您一步一步完成这个过程。

在开始之前,您需要在 Azure 订阅中拥有管理证书,它将允许您在 Azure Automation 中管理您的服务。我们的团队目前正在研究加载证书的方法,以便使您不必再手动执行此操作。您可以在 Azure 反馈论坛上跟踪这一功能的开发进程,网址为:https://feedback.azure.com/forums/246290-azure-automation/suggestions/5741122-automatically-install-root-certificates

您还需要利用 makecert.exe 创建一个自签名证书。该工具位于面向 Windows 8 的 Windows 软件开发工具包(SDK)中,网址为:https://msdn.microsoft.com/en-us/library/windows/desktop/hh852363.aspx,或从 Visual Studio 中获得。我将演示如何从 Windows SDK 中获得它。

第1 步: 从以上链接中下载并安装 SDK,如下图所示:

clip_image002 

第2 步: 只需安装 Windows Software Development Kit,取消选定其他选项,因为 makecert.exe 只在基本安装中。

clip_image004 

第3 步: 创建自签名证书,从 Azure Automation 中管理服务时可将其添加至 Azure 订阅并使用。要了解更多关于管理证书的信息,请点击https://msdn.microsoft.com/en-us/library/azure/gg551722.aspx。如下图所示,打开管理员命令提示符,将目录更改至 makecert.exe 的位置。

clip_image005 

第4 步: 使用以下命令创建证书(用您选择的名称替换掉 AzureManagement)。然后将其添加至 Azure 设置里的管理证书中。

C:\Program Files (x86)\Windows Kits\8.0\bin\x64>makecert -sky exchange -r –n "CN=AzureManagement" -pe -a sha1 -len 2048 -ss My "AzureManagement.cer"

clip_image006 

第5 步: 在 Windows 8.1 或 Windows 2012 R2 中,您可以使用内置的 PowerShell 命令利用私钥将证书导出为 PFX 文件(此文件可被导入 Azure Automation 中)。

更多关于该命令的信息可点击https://technet.microsoft.com/en-us/library/hh848635.aspx

打开管理员 PowerShell 界面并运行以下命令:

$MyPwd = ConvertTo-SecureString -String "CbA123321AbC" -Force –AsPlainText (用您自己的密码替换这里的密码)

$AzureCert = Get-ChildItem -Path Cert:\CurrentUser\My | where {$_.Subject -match "AzureManagement”} (用您在第 4 步中的证书名称替换这里的 AzureManagement)

Export-PfxCertificate -FilePath C:\AzureManagementCert.pfx -Password $MyPwd -Cert $AzureCert (这将会在您的计算机的 c:\ 上创建 AzureManagementCert.pfx)

clip_image008 

另外,如果您想通过 UI 使用 MMC 控制台来完成,可按照以下步骤:

clip_image009 

从File 菜单中选择 Add/Remove Snap-in。

clip_image011 

选择为我的用户帐户应用证书管理单元。

clip_image013 

clip_image014

 

导航至 Personal\Certificates,右键单击您创建的证书,然后单击 Export。

clip_image016 

导出私钥。

clip_image018 

选择 .PFX 作为导出格式。

clip_image020 

为证书设置密码,将 .PFX 文件导入至 Azure Automation 时会用到该密码。

clip_image022 

clip_image024

 

第6 步: 登录到 Microsoft Azure 管理门户(https://manage.windowsazure.com),选择 Settings -> Management Certificates area。单击上传命令并上传您在第 4 步中创建的证书(扩展名为 .cer 的那个)。

image 

第7 步: 上传管理证书(扩展名为 .pfx)至 Microsoft Azure Automation 并创建连接资产。转到 Automation -><Automation Account you created> ->  Assets area (资产区域)。单击 Add Setting并选择 Add Connection。在此向导中,您需要用到 Azure 订阅 ID。该 ID 可在快速浏览下的自动化帐户控制面板中或您上传第一个证书的设置区域门户中找到。

为该连接命名,并将其作为此后创建 Runbook 的参考。

image 

您可以使用任何名称来命名自动化证书。在下一步创建证书设置时还将使用相同名称存储管理证书。

image 

第8 步: 创建证书类型的凭据设置,名称要与第 7 步中创建 Azure 连接的名称相同。上传 .pfx 证书,此时会用到第 5 步创建的密码。

image 

image

现在,让我们在 Runbook 中使用 Azure 管理连接!

第9 步: 从https://gallery.technet.microsoft.com/scriptcenter/Connect-to-an-Azure-f27a81bb中导入 connect-azure.ps1 文件并发布该 Runbook。运行 Runbook 并输入第 7 步创建的 Azure 连接名称。如果在其运行完成后查看此 Runbook 的工作情况,应该不会出现任何故障。

clip_image033 

clip_image035

 

第10 步: 创建一个新的名为 Get-Website 的 Runbook,我们将用它来获取 Azure 订阅中的网站列表。导航至该 Runbook 并单击 Author,然后单击 Draft。

clip_image037 

clip_image039

 

第11 步: 在 Runbook 页面中,单击 INSERT->Runbook 命令,选择之前发布的 Connect-Azure Runbook。

clip_image041 

clip_image043 

clip_image044

 

第12 步: 将 <System.String> 更换成您的 Auzre 连接名称。

clip_image045 

第13 步: 单击 INSERT->Activity 命令,选择 Select-AzureSubscription,如果有多个订阅,可指定您想要管理的那个。

clip_image047 

clip_image049 

clip_image051

 

第14 步: 除 –SubscriptionName 之外,删除所有可选参数,并输入以上步骤使用的 Azure 连接名称。

clip_image053 

第15 步: 在 Azure 服务中添加诸如网站等的调用,如通过单击 INSERT->Activity 命令,选择 Get-AzureWebSites。

clip_image055 

clip_image057

 

第16 步: 删除 –Name 参数,因为我们可以检索所有网站,而不是只检索某个特定网站。

clip_image059 

第17 步: 单击Test 命令并运行 Runbook。如果您创建了网站列表,将会在订阅中看到。

clip_image061 

clip_image063

 

通过正确使用 Azure cmdlet,您可以使用以上相同步骤管理所有 Azure 服务。当您想要在 Azure Automation 中管理 Azure 资源时,还可以重复使用 Connect-Azure Runbook。