我尝试以自动方式使用Runbook编写PowerShell脚本,该脚本必须通过RUN AS ACCOUNT获取虚拟机(磁盘和数据磁盘)的快照,RUN AS ACCOUNT使用服务主体连接到az帐户并列出快照任何想法或帮助,谢谢:)
g52tjvyc1#
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
1条答案
按热度按时间g52tjvyc1#
以上代码取自schowdary22