当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用 Azure 在 VM 上运行脚本

作者:精品下载站 日期:2024-12-14 12:47:26 浏览:14 分类:玩电脑

使用 Azure 在 VM 上运行脚本


当您的团队开始配置和配置更多 Windows Azure 虚拟机 (VM) 时,您可能会厌倦重新发明轮子。您需要自动化!在本文中,跟我一起逐步引导您开始使用 Azure 自定义脚本扩展来使用 Azure 在 VM 上运行脚本。

如果您的团队处于 Windows 环境中,那么自动化服务器配置的最佳工具之一就是 PowerShell。通过使用 Azure PowerShell 模块,你的组织不仅可以利用 PowerShell 的强大功能自动执行本地任务,还可以在 Azure VM 上批量运行命令,而不是一次运行一个命令。

Microsoft 为我们提供的一项功能称为 Azure 自定义脚本扩展。自定义脚本扩展是一个 Azure 虚拟机扩展,VM 代理运行该扩展以使用 Azure API 对 VM 执行任意 PowerShell 代码,而不是控制台到 VM 中或使用 PowerShell 远程处理。

以这种方式运行命令有几个好处。在 Windows 中使用 Azure 自定义扩展运行命令:

  • 无需为 PowerShell 远程处理打开网络端口,从而提高安全性
  • 允许在 VM 启动时轻松执行 PowerShell 代码
  • 作为预配过程的一部分,自动将资源从 Azure 存储传输到 VM
  • 运行存储在各种 Azure 存储帐户中的 PowerShell 脚本的简单方法

可以通过多种方式为 Windows 启用 Azure 自定义脚本扩展。在本文中,我们将重点介绍通过 PowerShell 启用自定义脚本扩展,但你也可以通过 Azure 资源管理器 (ARM) 模板启用该扩展。

举一个简单的示例,假设您希望确保在订阅中的 Azure VM 上启用 PowerShell 远程处理。为此,您需要在每个虚拟机上本地运行以下命令:

Enable-PSRemoting -Force

让我们构建一个自定义脚本扩展来执行此操作。

使用 Azure 在 VM 上运行脚本

首先,在本地计算机上创建一个名为 Enable-PSRemoting.ps1 的 PowerShell 脚本,其中包含上述命令。此脚本需要在 Azure VM 上运行。为此,我们将构建另一个名为 New-CustomScriptExtension.ps1 的小型 PowerShell 脚本,将其上传到 Azure,并创建一个自定义脚本扩展来执行它。在我们深入之前,您需要一些物品:

  • 将存储脚本的 Azure 资源组和存储帐户名称
  • Azure 存储容器名称
  • 虚拟机名称
  • VM 所在的 Azure 资源组名称

该脚本可以分为两部分;将小型 PowerShell 脚本上传到 Azure 并创建自定义脚本扩展。将这两个过程粘合在一起还需要其他一切。

将 PowerShell 脚本上传到 Azure

首先,我们将 Enable-PSRemoting.ps1 脚本上传到容器 ($ContainerName) 内的 Azure 存储帐户 ($StorageAccountName) )在资源组 ($ResourceGroupName) 中。

$saParams = @{
    'ResourceGroupName' = $ResourceGroupName
    'Name' = $StorageAccountName
}
$storageContainer = Get-AzureRmStorageAccount @saParams |Get-AzureStorageContainer -Container $ContainerName
$bcParams = @{
    'File' = 'C:\Enable-PSRemoting.ps1'
    'BlobType' = 'Block'
    'Blob' = ' Enable-PSRemoting.ps1'
}
$storageContainer | Set-AzureStorageBlobContent @bcParams

运行自定义脚本扩展

当您运行New-CustomScriptExtension.ps1时,此脚本会将Enable-PSRemoting.ps1脚本上传到指定的Azure存储帐户。

现在脚本已存储在 Azure 中,您可以通过 VM 的自定义脚本扩展来执行它:

  • 名称:$VMName
  • 资源组:$rgName
  • 存储帐户:$saName
  • 存储容器:$scName

打开 New-CustomScriptExtension.ps1 的文本编辑器并粘贴以下示例。当您运行此命令时,它将执行适用于 Windows 的 Azure 自定义脚本扩展,该扩展将执行您之前上传的 Enable-PSRemoting.ps1 PowerShell 脚本。

# Get the VM we need to configure
$vm = Get-AzureRmVM -ResourceGroupName $rgName -Name $VMName
# Get storage account key
$key = (Get-AzureRmStorageAccountKey -Name $saName -ResourceGroupName$rgname).Key1
## Enable the custom script extension and run the script
$scriptParams = @{
    'ResourceGroupName' = $rgName
    'VMName' = $VMName
    'Name' = 'Enable-PSRemoting.ps1'
    'Location' = $vm.Location
    'StorageAccountName' = $saName
    'StorageAccountKey' = $key
    'FileName' = 'Enable-PSRemoting.ps1'
    'ContainerName' = $scName
    'Run' = 'Enable-PSRemoting.ps1'
}
Set-AzureRmVMCustomScriptExtension @scriptParams

概括

此脚本完成后,您可以验证 Enable-PSRemoting.ps1 已在虚拟机上执行,并且 PowerShell 远程处理已成功启用。您现在应该能够对 Azure VM 使用 Invoke-Command

通过利用 Windows 中的 Azure 自定义脚本扩展,您现在可以在 Azure VM 上远程运行任何类型的 PowerShell 脚本。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯