首页 > 系统相关 >Microsoft Azure解决方案:如何用Powershell批量安装Microsoft.Graph PowerShell模块

Microsoft Azure解决方案:如何用Powershell批量安装Microsoft.Graph PowerShell模块

时间:2023-01-11 13:32:05浏览次数:42  
标签:Status ModuleName Graph Microsoft Azure 安装 InstalledModules

51CTO 博客地址: https://blog.51cto.com/14669127

Azure培训视频地址: https://space.bilibili.com/2000820534

很多熟悉Azure Automation和Graph API的小伙伴提到在Automation Account中安装Microsoft.Graph PowerShell模块有点麻烦,因为Graph API模块有30多个子模块,如果通过GUI手动安装的话,工作量消耗有点多和繁琐,所以今天我将介绍如何使用PowerShell安装Microsoft Graph Module for Azure Automation。

说明:确保我们能够成功安装模块之前,需要我们提前部署安装AZ PowerShell Module和配置安装相关Module的权限。

Set-AzContext -SubscriptionId $SubscriptionId
$ResourceGroup = 'resourcegroupname'
$AutomationAccount = 'automationaccountname'
[System.Collections.Generic.List[Object]]$InstalledModules = @()
$GraphModule = Find-Module Microsoft.Graph
$DependencyList = $GraphModule | select -ExpandProperty Dependencies | ConvertTo-Json | ConvertFrom-Json
$ModuleVersion = $GraphModule.Version
$ModuleName = 'Microsoft.Graph.Authentication'
$ContentLink = "https://www.powershellgallery.com/api/v2/package/$ModuleName/$ModuleVersion"
New-AzAutomationModule -ResourceGroupName $ResourceGroup -AutomationAccountName $AutomationAccount -Name $ModuleName -ContentLinkUri $ContentLink -ErrorAction Stop | Out-Null
do {
Start-Sleep 20
$Status = Get-AzAutomationModule -ResourceGroupName $ResourceGroup -AutomationAccountName $AutomationAccount -Name $ModuleName | select -ExpandProperty ProvisioningState
} until ($Status -in ('Failed','Succeeded'))

if ($Status -eq 'Succeeded') {
$InstalledModules.Add($ModuleName)

foreach ($Dependency in $DependencyList) {
$ModuleName = $Dependency.Name
if ($ModuleName -notin $InstalledModules) {
$ContentLink = "https://www.powershellgallery.com/api/v2/package/$ModuleName/$ModuleVersion"
New-AzAutomationModule -ResourceGroupName $ResourceGroup -AutomationAccountName $AutomationAccount -ContentLinkUri $ContentLink -Name $ModuleName -ErrorAction Stop | Out-Null
sleep 3
}
}

$LoopIndex = 0
do {
foreach ($Dependency in $DependencyList) {
$ModuleName = $Dependency.Name
if ($ModuleName -notin $InstalledModules) {
$Status = Get-AzAutomationModule -ResourceGroupName $ResourceGroup -AutomationAccountName $AutomationAccount -Name $ModuleName -ErrorAction SilentlyContinue | select -ExpandProperty ProvisioningState
sleep 3
if ($Status -in ('Failed','Succeeded')) {
if ($Status -eq 'Succeeded') {
$InstalledModules.Add($ModuleName)
}

[PSCustomObject]@{
Status = $Status
ModuleName = $ModuleName
ModulesInstalled = $InstalledModules.Count
}
}
}
}
$LoopIndex++
} until (($InstalledModules.Count -ge $GraphModule.Dependencies.count) -or ($LoopIndex -ge 10))
}

Microsoft Azure解决方案:如何用Powershell批量安装Microsoft.Graph PowerShell模块_PowerShell

Microsoft Azure解决方案:如何用Powershell批量安装Microsoft.Graph PowerShell模块_PowerShell_02

按照上述分享的脚本,我们就能够安装Microsoft Graph Module for Azure Automation。说明:此方法目前仅支持PowerShell版本5.1,因此如果想为PowerShell 7和更高版本安装模块,仍需要在GUI手动安装。

分享本文,希望能给有需要的小伙伴提供智能化安装,避免繁琐的重复操作,谢谢大家阅读。

标签:Status,ModuleName,Graph,Microsoft,Azure,安装,InstalledModules
From: https://blog.51cto.com/u_14669127/6002151

相关文章