gpt4 book ai didi

powershell - 缺少 AzureRmProfileProvider 模块

转载 作者:行者123 更新时间:2023-12-03 05:03:51 24 4
gpt4 key购买 nike

我目前在 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 模块无关。

问题来自于 ps 在解析期间无法找到类型但在执行期间可用的事实。因此,只需通过将命令行开关存储在使用 invoke-command 执行的文字中来“欺骗”解析器即可:

$profile = Invoke-Expression "[Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile"

问候。

关于powershell - 缺少 AzureRmProfileProvider 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49681706/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com