我有一个很好的代码来创建虚拟机并安装混合工作线程和混合工作线程扩展,但是在尝试安装混合工作线程扩展时出现错误。
$maxRetryCount = 3
$retryCount = 0
$retryIntervalSeconds = 120
do {
$testvm = Get-AzVM -ResourceGroupName $resourcegroupname -Status | Where-Object { $_.Name -like "winvm1" }
if ($testvm.PowerState -ne "VM running") {
$retryCount ++
Write-Output "INFORMATION:: Waiting for Man VM to be operational. Retries : $($retryCount) of $($maxRetryCount)"
Start-Sleep -Seconds $retryIntervalSeconds
} else {
Write-Output "INFORMATION:: winvm1 is in running state."
break
}
} while ($retryCount -lt $maxRetryCount)
if ($testvm.PowerState -ne "VM running") {
Write-Error "VM not running."
}
$HW1 = Get-AzAutomationHybridRunbookWorkerGroup -ResourceGroupName $resourcegroupname -AutomationAccountName $aaname -Name "hwrwinvm1"
$getHW1 = Get-AzAutomationHybridRunbookWorker -ResourceGroupName $resourcegroupname -AutomationAccountName $aaname -HybridRunbookWorkerGroupName $HW1.Name | Where-Object { $_.VmResourceId -like $testvm.id}
if(!$getHW1){
[guid]$worker1 = [guid]::NewGuid()
Write-Output "ATTEMPTING:: Hybrid Worker Creation for $($testvm.Name)"
try {
$hybridWorkerConfiguration = @{
ResourceGroupName = $resourcegroupname
AutomationAccountName = $aaname
VmResourceId = $testvm.Id
HybridRunbookWorkerGroupName = $HW1.Name
Name = $worker1
}
# Attempt to create a new Hybrid Worker
$null = New-AzAutomationHybridRunbookWorker @hybridWorkerConfiguration
} catch {
Write-Error "An error occurred: $($_.Exception.Message)"
}
}
if(!$getHW1.Ip){
#create AutomationURI
$token = Get-AzBearerToken
$automationurl1 = "https://management.usgovcloudapi.net/subscriptions/${subscriptionId}/resourceGroups/${resourcegroupname}/providers/Microsoft.Automation/automationAccounts/${aaName}?api-version=2021-06-22"
$settingsMan = @{
AutomationAccountURL = (Invoke-RestMethod -Uri $automationurl1 -Headers @{ "Authorization" = $token } -Method Get).properties.AutomationHybridServiceUrl
}
try {
$installextension = @{
ResourceGroupName = $testvm.ResourceGroupName
Location = $testvm.Location
VMName = $testvm.Name
Name = "HybridWorkerExtension"
Publisher = "Microsoft.Azure.Automation.HybridWorker"
ExtensionType = "HybridWorkerForWindows"
TypeHandlerVersion = "1.1"
Settings = $settingsMan
EnableAutomaticUpgrade = $true
}
# Attempt to set the VM extension
Set-AzVMExtension @installextension
} catch {
Write-Error "An error occurred: $($_.Exception.Message)"
}
}else{
Write-Warning "Extension installed"
}
返回错误:
. '/home/vsts/work/1/s/SCRIPTS/PIPELINE/failover.ps1' -subscriptionId …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| An error occurred while setting the VM extension: Long running operation
| failed with status 'Failed'. Additional Info:'VM has reported a failure
| when processing extension 'HybridWorkerExtension' (publisher
| 'Microsoft.Azure.Automation.HybridWorker' and type
| 'HybridWorkerForWindows'). Error message: "[Internal Error] The Hybrid
| Worker Extension failed to execute:
| System.Net.Http.HttpRequestException: An error occurred while sending
| the request. ---> System.Net.WebException: The remote name could not be
| resolved:
| 'XXXX.jrds.usge.azure-automation.us'
| at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult
| asyncResult, TransportContext& context) at
| System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult
| ar) --- End of inner exception stack trace --- at
| System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at
我确信这有时会起作用,当它起作用时,当我在自动化帐户中检查混合工作人员时,它会给出尚未配置扩展的消息。但是,当我检查虚拟机时,它显示已安装扩展。
An error occurred while setting the VM extension: Long running operation
| failed with status 'Failed'. Additional Info:'VM has reported a failure
| when processing extension
当提供的资源详细信息不正确或者没有必要的资源来创建
HybridWorkerExtension
时,就会出现错误。
这里是创建
Hybrid Worker Group
、 Hybrid Worker
和 HybridWorkerExtension
的更新代码。
# Your existing code to create Hybrid Worker
$rgname = '<Resource-Group-Name>'
$AutomateAcc = '<Automation-Account>'
$hybridWorkerGroupName = 'Venkat'
$workerName = New-Guid
# Provide the full resource ID of your VM
$vmResourceId = '/subscriptions/<sub-ID>/resourceGroups/Venkat/providers/Microsoft.Compute/virtualMachines/VenaktVM'
# Create a new Hybrid Worker Group configuration
New-AzAutomationHybridRunbookWorkerGroup -AutomationAccountName $AutomateAcc -Name $hybridWorkerGroupName -ResourceGroupName $rgname
# Create a new Hybrid Worker configuration
$hybridWorkerConfiguration = @{
ResourceGroupName = $rgname
AutomationAccountName = $aaname
VmResourceId = $vmResourceId
HybridRunbookWorkerGroupName = $hybridWorkerGroupName
Name = $workerName
}
# Add the new Hybrid Worker to the group
New-AzAutomationHybridRunbookWorker @hybridWorkerConfiguration
$settings = @{
"AutomationAccountURL" = "https://03701b48-9c64-49a1-ab68-058d73996c8d.jrds.eus.azure-automation.net/automationAccounts/03701b48-9c64-49a1-ab68-058d73996c8d";
};
Set-AzVMExtension -ResourceGroupName "Venkat" -Location "East US" -VMName "VenaktVM" -Name "HybridWorkerExtension" -Publisher "Microsoft.Azure.Automation.HybridWorker" -ExtensionType HybridWorkerForLinux -TypeHandlerVersion 1.1 -Settings $settings -EnableAutomaticUpgrade $true
混合工作者创建
HybridWorker扩展安装
参考: 新 AzAutomationHybridRunbookWorkerGroup