-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathEnableAzureArc.ps1
577 lines (452 loc) · 22.9 KB
/
EnableAzureArc.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
<#
.DESCRIPTION
This Script is meant to be in a network share and onboards a server in Azure Arc
using a Service Principal:
https://docs.microsoft.com/en-us/azure/azure-arc/servers/onboard-service-principal#create-a-service-principal-for-onboarding-at-scale
It makes the following validation checks before the onboarding:
- Checks if the machine is an Azure VM or a Non Azure Machine
- Checks Framework Version
- Checks PowerShell Version
If the server doesn't pass the requirements, all the information from the server: OS, Framework version,
PowerShell version, VM type ... is stored in a network share for further analisys.
If the server pass the requirements, the script checks if the Azure Hybrid Instance Metadata Service is already installed
If not, the script:
- Install the Connected Machine agent on the machine
- Connects the server to Azure Arc using a Service Principal
- Tags Azure Arc server with a given Tag
- Any Connection error is logged and the Agent code get: https://docs.microsoft.com/en-us/azure/azure-arc/servers/troubleshoot-agent-onboard#agent-error-codes
In positive case, the script:
- Checks azcmagent.exe version and updates the agent if a new version is found in the network folder
- Checks its connection status
- In case the server is disconnected it logs the last errors from the azcmagent.exe Agent on the shared folder
.PARAMETER ReportServerFQDN
FQDN of the Server that will act as report Server (and source files)
.PARAMETER AssessOnly
Switch parameter that makes script work in Assess mode.
No machines will be onboarded in Azure Arc.
Machines will only report if their prerequesites are met or not to the report share
.EXAMPLE
This example onboards machines in Azure Arc
.\EnableAzureArc.ps1 -ArcRemoteShare AzureArcOnBoard
.EXAMPLE
This example assesses machines Azure Arc prerequisites and sends the info to the report share
.\EnableAzureArc.ps1 -ArcRemoteShare AzureArcOnBoard -AssessOnly
#>
Param (
[Parameter(Mandatory = $true)]
[System.String]$ArcRemoteShare,
[Parameter(Mandatory = $true)]
[System.String]$ReportServerFQDN,
[switch]$AssessOnly
)
#Calculate Logging path
$LoggingNetworkPath = "$((Join-Path -Path "\\$ReportServerFQDN" -ChildPath $ArcRemoteShare) -replace "\\$")" + "\AzureArcLogging"
#Calculate network full path
$SourceFilesFullPath = "$((Join-Path -Path "\\$ReportServerFQDN" -ChildPath $ArcRemoteShare) -replace "\\$")" + "\AzureArcDeploy"
$arcInfo = Get-Content (Join-Path $SourceFilesFullPath "ArcInfo.json") | ConvertFrom-Json
###########################################################################################################
# Add the service principal application ID and more data here:
$servicePrincipalClientId = $arcInfo.ServicePrincipalClientId
$tenantid = $arcInfo.TenantId
$subscriptionid = $arcInfo.SubscriptionId
$ResourceGroup = $arcInfo.ResourceGroup
$location = $arcInfo.Location
$PrivateLinkScopeId = $arcInfo.PrivateLinkScopeId
$AgentProxy = $arcInfo.AgentProxy
$tags = @{ # Tags to be added to the Arc servers
DeployedBy = "GPO"
}
if($arcInfo.Tags){
$arcInfo.Tags.psobject.properties | Foreach { $tags[$_.Name] = $_.Value }
}
$workfolder = "$env:SystemDrive\temp"
$logpath = "$workfolder\AzureArcOnboarding.log" #Local log file
###########################################################################################################
#region Functions and classes Definition
Function Get-ArcAgentstatus {
Param (
[Switch]$loglocally,
[ValidateSet("info", "error")]
[System.String[]]$logtype
)
$agentstatus = (& "$($env:ProgramW6432)\AzureConnectedMachineAgent\azcmagent.exe" show -j) | ConvertFrom-Json
if ($loglocally) {
# Logs the status in the AzureArcAgentOnboarding.log
switch ($logtype) {
"info" {
$agentstatus | Get-Member -MemberType NoteProperty -Name "*HeartBeat" | ForEach-Object {
Write-Log -msg "$($_.Name) : $($agentstatus.$($_.Name))" -msgtype INFO
}
}
"error" {
$agentstatus | Get-Member -MemberType NoteProperty -Name "*Error*" | ForEach-Object {
Write-Log -msg "$($_.Name) : $($agentstatus.$($_.Name))" -msgtype ERROR
}
}
}
}
$agentstatushash = [ordered]@{}
$agentstatus.psobject.properties | Sort-Object -Property Name | ForEach-Object { $agentstatushash[$_.Name] = $_.Value } # Converts to Hashtable
return $agentstatushash
}
Function Get-ArcAgentErrorLogs {
[cmdletbinding()]
Param(
[datetime]$since
)
$script:azcmagentErrors = Get-Content "$env:Programdata\AzureConnectedMachineAgent\Log\azcmagent.log" | Select-String "level=fatal" | Where-Object { [datetime](($_ -split "\s")[0] -replace "time=" -replace '"') -gt $since }
$HimdsErrors = Get-Content "$env:Programdata\AzureConnectedMachineAgent\Log\himds.log" | Select-String "error" |
Where-Object { [datetime](($_ -split "\s")[0] -replace "time=" -replace '"') -gt $since }
foreach ($azcmagentError in $script:azcmagentErrors) {
Write-Log -msg $azcmagentError -msgtype ERROR
}
foreach ($HimdsError in $HimdsErrors) {
Write-Log -msg $HimdsError -msgtype ERROR
}
}
Function Install-ArcAgent {
# Install the package
if (-not (Test-Path "$SourceFilesFullPath\AzureConnectedMachineAgent.msi" -ErrorAction SilentlyContinue)) {
Write-Log -msg "AzureConnectedMachineAgent.msi file not found in the network folder. Exiting... " -msgtype ERROR
exit
}
# If no local msi found, downloads it from the network share
if (-not (Test-Path "$env:LOCALAPPDATA\AzureConnectedMachineAgent.msi" -ErrorAction SilentlyContinue)) {
Copy-Item -Path "$SourceFilesFullPath\AzureConnectedMachineAgent.msi" -Destination "$env:LOCALAPPDATA" -Force
}
Write-Log -msg "Installing Azure Connected Machine Agent" -msgtype INFO
Set-Location $workfolder
$exitCode = (Start-Process -FilePath msiexec.exe -ArgumentList @("/i", "$env:LOCALAPPDATA\AzureConnectedMachineAgent.msi" , "/l*v", "Azcmagentinstallationlog.txt", "/qn") -Wait -Passthru).ExitCode
$message = (net helpmsg $exitCode)
if ($exitCode -ne 0) {
Write-Log -msg "Installation failed: $message. See Azcmagentinstallationlog.txt for additional details" -msgtype ERROR
throw $message
}
else {
Write-Log -msg "Azure Connected Machine Agent installed. $message" -msgtype INFO
}
}
Function Get-MsiVersion {
Param (
[IO.FileInfo]$path
)
$windowsInstaller = New-Object -ComObject WindowsInstaller.Installer
$database = $windowsInstaller.GetType().InvokeMember(
"OpenDatabase", "InvokeMethod", $Null,
$windowsInstaller, @($path.FullName, 0)
)
$query = "SELECT Value FROM Property WHERE Property = 'ProductVersion'"
$View = $database.GetType().InvokeMember(
"OpenView", "InvokeMethod", $Null, $database, ($query)
)
$View.GetType().InvokeMember("Execute", "InvokeMethod", $Null, $View, $Null)
$record = $View.GetType().InvokeMember( "Fetch", "InvokeMethod", $Null, $View, $Null )
$version = ($record.GetType().InvokeMember( "StringData", "GetProperty", $Null, $record, 1 ))
return $version
}
Function Update-ArcAgentVersion {
Write-Log -msg "Checking Local Agent version for updates ..." -msgtype INFO
#Checks Current Agent Version
[Version]$LocalAgentVersion = (Get-ArcAgentstatus).Agentversion
#Checks network share version
[Version]$NetworkShareVersion = (Get-MsiVersion -path "$SourceFilesFullPath\AzureConnectedMachineAgent.msi")[1]
#Compare versions
if ($LocalAgentVersion -lt $NetworkShareVersion) {
# New Agent Version Found
Write-Log -msg "New Agent version found in network share folder: $($NetworkShareVersion.ToString()) , local version is $($LocalAgentVersion.ToString()). Executing update ..." -msgtype WARNING
# Download the package
Copy-Item -Path "$SourceFilesFullPath\AzureConnectedMachineAgent.msi" -Destination "$env:LOCALAPPDATA" -Force
#Install the package
Update-ArcAgent
#Checks new Version
[Version]$LocalAgentVersion = (Get-ArcAgentstatus).Agentversion
Write-Log -msg "Azure Connected Machine Agent Version is now $($LocalAgentVersion.ToString())" -msgtype INFO
}
else {
#Latest Version Found
Write-Log -msg "Machine has latest available version of the agent: $($LocalAgentVersion.ToString())" -msgtype INFO
}
}
Function Update-ArcAgent {
# Update Agent
Write-Log -msg "Updating Azure Connected Machine Agent" -msgtype INFO
Set-Location $workfolder
$exitCode = (Start-Process -FilePath msiexec.exe -ArgumentList @("/i", "$env:LOCALAPPDATA\AzureConnectedMachineAgent.msi" , "/l*v", "Azcmagentupdatesetup.txt", "/qn") -Wait -Passthru).ExitCode
$message = (net helpmsg $exitCode)
if ($exitCode -ne 0) {
Write-Log -msg "Update failed: $message. See Azcmagentupdatesetup.txt for additional details" -msgtype ERROR
throw $message
}
else {
Write-Log -msg "Azure Connected Machine Agent updated. $message" -msgtype INFO
}
}
Function Get-ServicePrincipalSecret {
try {
Copy-Item (Join-Path $SourceFilesFullPath "AzureArcDeployment.psm1") $workfolder -Force
Import-Module (Join-Path $workfolder "AzureArcDeployment.psm1")
$encryptedSecret = Get-Content (Join-Path $SourceFilesFullPath encryptedServicePrincipalSecret)
$sps = [DpapiNgUtil]::UnprotectBase64($encryptedSecret)
Remove-Item (Join-Path $workfolder "AzureArcDeployment.psm1") -Force
}
catch {
Write-Log -msg "Could not fetch service principal secret: $($_.Exception)" -msgtype ERROR
return $false
}
return $sps
}
Function Add-SiteTag {
$adsite = (& "$env:windir\system32\nltest.exe" /dsgetsite)[0]
if ($LastExitCode -eq 0) {
Write-Log -msg "Tag Site:$adsite added to azure connected machine tags" -msgtype INFO
$script:tags["Site"] = $adsite
}
else {
Write-Log -msg "Could not determine machine's AD Site" -msgtype WARNING
$script:tags["Site"] = "Unknown"
}
}
Function Connect-ArcAgent {
# Add AD site as a tag
Add-SiteTag
# Run connect command
Write-Log -msg "Trying to Connect Azure Connected Machine Agent ..." -msgtype INFO
$FinalTag = ($tags.GetEnumerator() | ForEach-Object -Process { "$($_.key)" + "=" + "'$($_.value)'" }) -join ","
$sps = Get-ServicePrincipalSecret
# if agent proxy is specified
if ($AgentProxy) {
$Proxyconf = & "$env:ProgramW6432\AzureConnectedMachineAgent\azcmagent.exe" config set proxy.url $AgentProxy #Proxy Configured
}
# if private link scope is specified
if ($PrivateLinkScopeId) {
if ($AgentProxy -ne "") {
& "$env:ProgramW6432\AzureConnectedMachineAgent\azcmagent.exe" config set proxy.bypass "Arc" #Bypass proxy for Arc services (his.arc.azure.com, guestconfiguration.azure.com, guestnotificationservice.azure.com, servicebus.windows.net)
}
$ConnectionOutput = & "$env:ProgramW6432\AzureConnectedMachineAgent\azcmagent.exe" connect --resource-name $env:computername --service-principal-id $servicePrincipalClientId --service-principal-secret $sps --resource-group $ResourceGroup --tenant-id $tenantid --location $location --subscription-id $subscriptionid --cloud AzureCloud --tags $FinalTag --private-link-scope $PrivateLinkScopeId --correlation-id "478b97c2-9310-465a-87df-f21e66c2b248"
}
else {
$ConnectionOutput = & "$env:ProgramW6432\AzureConnectedMachineAgent\azcmagent.exe" connect --resource-name $env:computername --service-principal-id $servicePrincipalClientId --service-principal-secret $sps --resource-group $ResourceGroup --tenant-id $tenantid --location $location --subscription-id $subscriptionid --cloud AzureCloud --tags $FinalTag --correlation-id "478b97c2-9310-465a-87df-f21e66c2b248"
}
if ($LastExitCode -eq 0) {
Write-Log -msg "Agent connected successfully!. To view your onboarded server(s), navigate to https://ms.portal.azure.com/#blade/Microsoft_Azure_HybridCompute/AzureArcCenterBlade/servers" -msgtype INFO
return $True
}
else {
#check for any errors in Agent connection
Write-Log -msg "Agent Connection was unsuccessful. Waiting for logs to be generated..." -msgtype ERROR
Start-Job -ScriptBlock {
$script:Agentcode = $null
do {
$script:Agentcode = Get-Content "$env:ProgramData\AzureConnectedMachineAgent\Log\azcmagent.log"-Tail 50 | Select-String 'AZCM\d*:[\s\w]*' | ForEach-Object { $_.matches } | Select-Object -Last 1 -ExpandProperty value
}
Until ($null -ne $script:Agentcode)
Write-Log -msg "Agent Code: $script:Agentcode" -msgtype ERROR
Write-Log -msg "Check AZCM Agent codes here: https://docs.microsoft.com/en-us/azure/azure-arc/servers/troubleshoot-agent-onboard#agent-error-codes" -msgtype INFO
} | Wait-Job -Timeout 30
return $false
}
}
Function Repair-ArcAgentConnection {
#TODO:
#The Machine could be moved, it has to maintaing the same config:
#$RegistrationInfo = Get-Registrationinfo
#$ConnectionOuput = & "$env:ProgramW6432\AzureConnectedMachineAgent\azcmagent.exe" connect --service-principal-id $servicePrincipalClientId --service-principal-secret $RegistrationInfo --resource-group $Agentconfig.resourceGroup --tenant-id $Agentconfig.tenantId --location $Agentconfig.location --subscription-id $Agentconfig.subscriptionId --cloud $Agentconfig.Cloud --tags $FinalTag --correlation-id $Agentconfig.correlationId
}
Function Send-ArcData {
[cmdletbinding()]
Param(
[Parameter(Mandatory = $true)]
[psobject]$Data,
[Parameter(Mandatory = $true)]
[System.String]$Path
)
Write-Log -msg "Sending information to central Shared folder ..." -msgtype INFO
$ParentPath = Split-Path -Path $path -Parent
if ($? -eq $false) { Write-Log -msg "$($error[0].Exception)" -msgtype ERROR }
if (-not(Test-Path ($ParentPath))) {
New-Item -Path $ParentPath -ItemType Directory -Force
if ($? -eq $false) { Write-Log -msg "$($error[0].Exception)" -msgtype ERROR }
}
$Data | Export-Clixml -Path $Path -Force
if ($? -eq $false) { Write-Log -msg "$($error[0].Exception)" -msgtype ERROR }
}
Function Test-ArcAgentConnection {
#Check if the Azure Connected Machine Agent is connected
try { $Agentresponse = Invoke-WebRequest -Uri "http://localhost:40342/agentstatus" -UseBasicParsing -ErrorAction Stop }
catch { Write-Log "There was an error when trying to get agent status: $($_.Exception)" -msgtype ERROR; throw $_.Exception }
$azcmagentstatus = $Agentresponse | ConvertFrom-Json
if ($azcmagentstatus.status -ne "Connected") {
return $false
}
else {
return $true
}
}
Function Test-ArcService {
#Check if the Azure Hybrid Instance Metadata Service is already installed
try { Get-Service himds -ErrorAction Stop }
catch {
Write-Log -msg "No Hybrid Instance Metadate Service installed" -msgtype ERROR
return $false
}
Write-Log -msg "Machine has the Hybrid Instance Metadata Service already installed ..." -msgtype INFO
return $true
}
Function Write-Log {
Param (
[System.String]$msg,
[ValidateSet("INFO", "ERROR", "WARNING")]
[System.String]$msgtype,
[switch]$Force
)
if ($PSBoundParameters.ContainsKey("Force")) {
Write-Output -InputObject "$(Get-Date -Format ("[yyyy-MM-dd][HH:mm:ss]")) $msgtype $msg" | Out-File $logpath
}
else {
Write-Output -InputObject "$(Get-Date -Format ("[yyyy-MM-dd][HH:mm:ss]")) $msgtype $msg" | Out-File $logpath -Append
}
}
#endregion
# MAIN
if (-not (Test-Path $workfolder))
{ New-Item -Path $workfolder -Force -ItemType Directory | Out-Null }
#Reduces by 1/4 the log file if excedees of 10mb
if ((Get-Item $logpath -ErrorAction SilentlyContinue).Length -gt 10mb) {
$content = Get-Content $logpath
$content[[int]($content.Length / 4)..($content.length - 1)] | Out-File $logpath -Force
}
Write-Log -msg "=========================================" -msgtype INFO
Write-Log -msg "Starting Azure Arc Onboarding process ..." -msgtype INFO
Write-Log -msg "Sources full path: $SourceFilesFullPath" -msgtype INFO
Write-Log -msg "Logging network full path: $LoggingNetworkPath" -msgtype INFO
$hash = @{
Computer = $env:COMPUTERNAME
OSVersion = (Get-WmiObject -Class win32_Operatingsystem).Caption
FrameworkVersion = ""
PowershellVersion = ($PSVersionTable.PSVersion.ToString()).substring(0, 3)
AzureVM = $false
ArcCompatible = $true
AgentStatus = ""
AgentLastHeartbeat = ""
AgentErrorCode = ""
AgentErrorTimestamp = (New-Object -TypeName datetime -ArgumentList ($null))
AgentErrorDetails = ""
httpsProxy = ""
}
$ArcOnboardingData = New-Object -TypeName PSobject -Property $hash
# Check Framework version
$FrameworkRegistry = "HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Client"
try {
Test-Path -Path $FrameworkRegistry -ErrorAction Stop | Out-Null
[version]$FWVersion = Get-ItemProperty -Path $FrameworkRegistry -Name Version -ErrorAction Stop | Select-Object -ExpandProperty Version
$ArcOnboardingData.FrameworkVersion = $FWVersion.ToString()
}
catch {
Write-Log -msg "Framework version is 3.5 or less and it's unsupported" -msgtype ERROR
$ArcOnboardingData.FrameworkVersion = "3.5 or less"
$ArcOnboardingData.ArcCompatible = $false
}
if (($FWVersion.major -lt 4) -or (($FWVersion.Major -eq 4) -and ($FWVersion.Minor -lt 6))) {
Write-Log -msg "Framework version $($FWVersion.ToString()) is unsupported" -msgtype ERROR
$ArcOnboardingData.ArcCompatible = $false
}
else {
Write-Log -msg "Machine has Framework version $($FWVersion.ToString()), this is supported" -msgtype INFO
}
#Check PowerShell Version (Powershell 4 is required)
if ($PSVersionTable.PSVersion.Major -lt 4) {
Write-Log -msg "Machine has Powershell version $($PSVersionTable.PSVersion.Major), minimum version required is PowerShell v4." -msgtype ERROR
$ArcOnboardingData.ArcCompatible = $false
}
else {
Write-Log -msg "Machine has PowerShell version version $($PSVersionTable.PSVersion.Major), this is supported" -msgtype INFO
}
#Check if it's an Azure VM
try {
$TestAzuremachine = Invoke-RestMethod -Headers @{"Metadata" = "true" } `
-Method GET -Proxy $Null -Uri "http://169.254.169.254/metadata/instance?api-version=2020-09-01"`
-TimeoutSec 3 -ErrorAction SilentlyContinue | ConvertTo-Json -Depth 64
}
catch {}
If ($Null -ne $TestAzuremachine) {
Write-Log -msg "Machine is an Azure VM, it won't be onboarded in Azure Arc" -msgtype ERROR
$ArcOnboardingData.AzureVM = $true
$ArcOnboardingData.ArcCompatible = $false
Write-Log -msg "Exiting Onboarding Process" -msgtype INFO
exit
}
else {
Write-Log -msg "Machine is not an Azure VM. Continuing the onboarding process" -msgtype INFO
$ArcOnboardingData.AzureVM = $false
}
#In case the onboarding is in AssessOnly mode, the onboarding process posts the data and exits
if ($PSBoundParameters.ContainsKey('AssessOnly')) {
Write-Log -msg "Arc GPO Onboarding is in AssessOnly mode. Remove the -AssessOnly parameter from the scheduled task in the GPO if you want the machines to be onboarded in Azure ARC." -msgtype WARNING
Send-ArcData -Data $ArcOnboardingData -Path "$LoggingNetworkPath\$($env:COMPUTERNAME).xml"
Write-Log -msg "End of the Azure Arc Onboarding process..." -msgtype INFO
exit
}
#Post to share if Machine has dependencies to solve
if (($ArcOnboardingData.AzureVM -eq $false) -and ($ArcOnboardingData.ArcCompatible -eq $false)) {
Write-Log -msg "Machine doesn't meet the minimum requirements for Azure Arc: Windows PowerShell 4 and NET Framework 4.6, or it is an Azure VM." -msgtype ERROR
Send-ArcData -Data $ArcOnboardingData -Path "$LoggingNetworkPath\$($env:COMPUTERNAME).xml"
Write-Log -msg "End of the Azure Arc Onboarding process..." -msgtype INFO
exit
}
#region Onboarding Process
#Checks Arc Service
if ((Test-ArcService) -eq $false) {
Install-ArcAgent
$StartConnection = Get-Date
if ((Connect-ArcAgent) -eq $false) {
Get-ArcAgentErrorLogs -since $StartConnection
Write-Log -msg "End of the Azure Arc Onboarding process." -msgtype INFO
exit
}
}
else {
# the Azure Hybrid Instance Metadata Service is already installed
#Ensures server has the latest Agent Version
Update-ArcAgentVersion
}
#Checks Agent Connectivity
if ((Test-ArcAgentConnection) -eq $false) {
#Log info locally
Write-Log -msg "Azure Connected Machine Agent is not connected ..." -msgtype ERROR
Get-ArcAgentErrorLogs -since (Get-Date).AddMinutes(-7)
$AgentProxyConfigured = (Get-ArcAgentstatus).httpsProxy
if ("" -eq $AgentProxyConfigured) {
Write-Log -msg "Machine has no proxy configured" -msgtype INFO
}
else { Write-Log -msg "Machine has the following proxy configured: $AgentProxyConfigured" -msgtype INFO }
if (-not (Test-Path "$env:Programdata\AzureConnectedMachineAgent\Config\agentconfig.json" )) {
Write-Log -msg "This machine has never been connected to Azure Arc, retrying one more time ..." -msgtype ERROR
$StartConnection = Get-Date
if ((Connect-ArcAgent) -eq $false) {
Get-ArcAgentErrorLogs -since $StartConnection
}
else {
# Machine connected successfuly
Write-Log -msg "End of the Azure Arc Onboarding process." -msgtype INFO
exit
}
}
#Prepare Information
$ArcAgentInfo = Get-ArcAgentstatus -logtype "error", "info" -loglocally # The status is also logged locally
$ArcOnboardingData.AgentStatus = $ArcAgentInfo.status
$ArcOnboardingData.AgentErrorCode = $script:AgentCode
$ArcOnboardingData.AgentLastHeartbeat = $ArcAgentInfo.lastHeartbeat
$ArcOnboardingData.AgentErrorTimestamp = [datetime]([regex]::match(($script:azcmagentErrors | Select-Object -Last 1 ), '^time="([\d\w\W]*)"\s').groups[1].value)
$ArcOnboardingData.AgentErrorDetails = [regex]::match(($script:azcmagentErrors | Select-Object -Last 1 ), 'msg="[\w\W\s]*').Groups[0].Value
$ArcOnboardingData.httpsProxy = $ArcAgentInfo.httpsProxy
#Send information to Share Folder
Send-ArcData -Data $ArcOnboardingData -Path "$LoggingNetworkPath\NotConnected\$($env:COMPUTERNAME).NotConnected.xml"
}
else {
Get-ArcAgentstatus -logtype "info" -loglocally
Write-Log -msg "Azure Connected Machine Agent is already connected." -msgtype INFO
}
Write-Log -msg "End of the Azure Arc Onboarding process." -msgtype INFO
#endregion