使用Runbook编写PowerShell脚本以获取虚拟机的快照:运行方式帐户: Azure

ghg1uchk  于 2022-11-25  发布在  Shell
关注(0)|答案(1)|浏览(130)

我尝试以自动方式使用Runbook编写PowerShell脚本,该脚本必须通过RUN AS ACCOUNT获取虚拟机(磁盘和数据磁盘)的快照,RUN AS ACCOUNT使用服务主体连接到az帐户并列出快照
任何想法或帮助,谢谢:)

g52tjvyc

g52tjvyc1#

  • 我尝试在我的环境中使用Runbook中的PowerShell创建快照虚拟机,并获得以下结果 *
  • 我有自动化帐户,其中包含新创建的Runbook*
  • 已创建具有相同资源组的虚拟机在创建运行手册之前,我已添加了名为*az.acount*az.compute的模块,如果我们添加这些模块,则只有运行手册可以工作 *

  • 如果找不到模块,则必须从浏览库导入模块 *
  • 以下是我的PowerShell脚本,用于创建虚拟机的快照,该快照使用服务主体连接AZ帐户并列出快照 *
Param
(
    [Parameter (Mandatory= $true)]
    [string]$VmResourceGroup,
    [Parameter (Mandatory= $true)]
    [string]$VmName
)

$connectionName = "AzureRunAsConnection"
try
{
    $servicePrincipalConnection = Get-AutomationConnection -Name $connectionName
    Connect-AzAccount `
        -ServicePrincipal `
        -TenantId $servicePrincipalConnection.TenantId `
        -ApplicationId $servicePrincipalConnection.ApplicationId `
        -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint 
}
catch {
    if (!$servicePrincipalConnection)
    {
        $ErrorMessage = "Connection $connectionName not found."
        throw $ErrorMessage
    } else{
        Write-Error -Message $_.Exception
        throw $_.Exception
    }
}
$vm = get-azvm -Name $VmName -ResourceGroupName $VmResourceGroup
$snapscreated = @()

Write-Output "VM $($vm.name) OS Disk Snapshot Begin"
$snapshotdisk = $vm.StorageProfile

$OSDiskSnapshotConfig = New-AzSnapshotConfig -SourceUri $snapshotdisk.OsDisk.ManagedDisk.id -CreateOption Copy -Location australiaeast # -OsType Windows
$snapshotNameOS = "snap_$($snapshotdisk.OsDisk.Name)_snap_osdisk_$(Get-Date -Format ddMMyyhhmm)"

New-AzSnapshot -ResourceGroupName $VmResourceGroup -SnapshotName $snapshotNameOS -Snapshot $OSDiskSnapshotConfig -ErrorAction Stop
$snapscreated += $snapshotNameOS

Write-Output "VM $($vm.name) OS Disk Snapshot End"
Write-Output "=====================================" 
 
Write-Output "VM $($vm.name) Data Disk Snapshots Begin"
 
$dataDisks = ($snapshotdisk.DataDisks).name

foreach ($datadisk in $datadisks) {

    $dataDisk = Get-AzDisk -ResourceGroupName $vm.ResourceGroupName -DiskName $datadisk

    Write-Output "VM $($vm.name) data Disk $($datadisk.Name) Snapshot Begin"

    $DataDiskSnapshotConfig = New-AzSnapshotConfig -SourceUri $dataDisk.Id -CreateOption Copy -Location australiaeast
    $snapshotNameData = "snap_$($datadisk.name)_snap_datadisk_$(Get-Date -Format ddMMyyhhmm)"

    New-AzSnapshot -ResourceGroupName $VmResourceGroup -SnapshotName $snapshotNameData -Snapshot $DataDiskSnapshotConfig -ErrorAction Stop

    $snapscreated += $snapshotNameData
    
    Write-Output "VM $($vm.name) data Disk $($datadisk.Name) Snapshot End"   
    Write-Output "====================================="
}
Write-Output "VM $($vm.name) Data Disk Snapshots End" 
Write-Output "List of Snaps created for VM $($vm.name) : -"
$snapscreated
Write-Output "====================================="

以上代码取自schowdary22

  • 运行脚本时,我已指定rg_name和虚拟机名称 *

  • 成功后,我可以看到快照列表,如下所示 *

相关问题