缺少AzureRmProfileProvider模块 [英] Missing AzureRmProfileProvider module

查看:0
本文介绍了缺少AzureRmProfileProvider模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前在Azure中尝试使用PowerShell脚本执行运行手册,但我的脚本显示无法找到此类的错误: Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider

您能否帮助了解如何将此类添加到我的脚本中?

下面提供的是我的脚本:

[CmdletBinding()]
[OutputType([string])]
Param
(
  # VM Name
  [Parameter(Mandatory = $true,
      ValueFromPipelineByPropertyName = $true,
  Position = 0)]
  $VMName
)

$VerbosePreference = 'Continue' #remove when publishing runbook

#region Runbook variables
Write-Verbose -Message 'Retrieving hardcoded Runbook Variables'
$Resourcegroupname = 'scriptextensiondemo-rg'
$ExtensionName = 'WindowsUpdate'
$APIVersion = '2017-03-30'
$ScriptExtensionUrl = 'https://[enteryourvaluehere].blob.core.windows.net/script/Install-WindowsUpdate.ps1'
#endregion

#region Connection to Azure
Write-Verbose -Message 'Connecting to Azure'
$connectionName = 'AzureRunAsConnection'

try
{
  # Get the connection "AzureRunAsConnection "
  $servicePrincipalConnection = Get-AutomationConnection -Name $connectionName         

  'Logging in to Azure...'
  Add-AzureRmAccount `
  -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.Message
    throw $_.Exception
  }
}
#endregion

#region Get AccessToken
Write-Verbose 'Get Access Token'
$currentAzureContext = Get-AzureRmContext
$azureRmProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile
$profileClient = New-Object -TypeName Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient -ArgumentList ($azureRmProfile)
$token = $profileClient.AcquireAccessToken($currentAzureContext.Subscription.TenantId)
#endregion 

#region Get extension info
Write-Verbose -Message 'Get extension info'
$Uri = 'https://management.azure.com/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Compute/virtualMachines/{2}/extensions/{3}?api-version={4}' -f $($currentAzureContext.Subscription), $Resourcegroupname, $VMName, $ExtensionName, $APIVersion
$params = @{
  ContentType = 'application/x-www-form-urlencoded'
  Headers     = @{
    'authorization' = "Bearer $($token.AccessToken)"
  }
  Method      = 'Get'
  URI         = $Uri
}
$ExtensionInfo = Invoke-RestMethod @params -ErrorAction SilentlyContinue
if (!($ExtensionInfo)) 
{
  Write-Verbose 'No Custom Script Extension Configured. Please do an initial script configuration first'
  #region configure custom script extension
  $Uri = 'https://management.azure.com/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Compute/virtualMachines/{2}/extensions/{3}?api-version={4}' -f $($currentAzureContext.Subscription), $Resourcegroupname, $VMName, $ExtensionName, '2017-03-30'

  $body = @"
{
  "location": "westeurope",
  "properties": {
    "publisher":  "Microsoft.Compute",
    "type": "CustomScriptExtension",
    "typeHandlerVersion": "1.4",
    "autoUpgradeMinorVersion": true,
    "forceUpdateTag": "InitialConfig",
    "settings": {
       "fileUris" : ["$ScriptExtensionUrl"],
       "commandToExecute": "powershell -ExecutionPolicy Unrestricted -file Install-WindowsUpdate.ps1"
    }
  }
}
"@

  $params = @{
    ContentType = 'application/json'
    Headers     = @{
      'authorization' = "Bearer $($token.AccessToken)"
    }
    Method      = 'PUT'
    URI         = $Uri
    Body        = $body
  }

  $InitialConfig = Invoke-RestMethod @params
  $InitialConfig
  exit
  #endregion
}
#endregion

#region Get Extension message info
Write-Verbose 'Get Extension message info'
$Uri = 'https://management.azure.com/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Compute/virtualMachines/{2}/extensions/{3}?$expand=instanceView&api-version={4}' -f $($currentAzureContext.Subscription), $Resourcegroupname, $VMName, $ExtensionName, $APIVersion
$params = @{
  ContentType = 'application/x-www-form-urlencoded'
  Headers     = @{
    'authorization' = "Bearer $($token.AccessToken)"
  }
  Method      = 'Get'
  URI         = $Uri
}
$StatusInfo = Invoke-RestMethod @params
#$StatusInfo
[regex]::Replace($($StatusInfo.properties.instanceView.SubStatuses[0].Message), '\n', "`n")
#endregion

#region Update Script Extension
try
{
  Write-Verbose 'Update Script Extension'
  $Uri = 'https://management.azure.com/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Compute/virtualMachines/{2}/extensions/{3}?api-version={4}' -f $($currentAzureContext.Subscription), $Resourcegroupname, $VMName, $ExtensionName, '2017-03-30'

  $body = @"
{
  "location": "westeurope",
  "properties": {
    "publisher":  "Microsoft.Compute",
    "type": "CustomScriptExtension",
    "typeHandlerVersion": "1.4",
    "autoUpgradeMinorVersion": true,
    "forceUpdateTag": "$(New-Guid)",
    "settings": {
       "fileUris" : ["$ScriptExtensionUrl"],
       "commandToExecute": "powershell -ExecutionPolicy Unrestricted -file Install-WindowsUpdate.ps1"
    }
  }
}
"@

  $params = @{
    ContentType = 'application/json'
    Headers     = @{
      'authorization' = "Bearer $($token.AccessToken)"
    }
    Method      = 'PUT'
    URI         = $Uri
    Body        = $body
  }

  $Updating = Invoke-RestMethod @params
  $Updating
}
catch
{
  Write-Error -Message $_.Exception.Message
  throw $_.Exception
}
#endregion

推荐答案

问题很可能是你运行的是过时的Azure模块,或者至少它们与你计算机上安装的模块不匹配。尝试升级您的自动化帐户中的Azure模块。此外,还要确保您正在使用的模块也包括在内。

这篇关于缺少AzureRmProfileProvider模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆