Skip to content

Instantly share code, notes, and snippets.

View Kaidja's full-sized avatar

Kaido Järvemets Kaidja

View GitHub Profile
#Query AD tiering level from AD
Function Get-ADTieringLevel
{
Param(
$ServerName
)
$OU = ([adsisearcher]"(&(name=$ServerName)(objectClass=computer))").FindOne().path
#Add the AMA Extension - this may take a few minutes to run
$AMAExtension = @{
Name = "AMAWindows"
ExtensionType = "AzureMonitorWindowsAgent"
Publisher = "Microsoft.Azure.Monitor"
ResourceGroupName = $ResourceGroup
MachineName = $ServerName
Location = $Location
#Remove PowerShell session
Remove-PSSession -Session $PSRemoteSession
#Onboard new server remotly
$ADTieringLevel = Get-ADTieringLevel -ServerName $ServerName
$Tags = @{
"ADTieringLevel" = $ADTieringLevel;
}
$ResourceGroup = "RG-PROD-IT-AZURE-ARC-WE"
$Location = "West Europe"
#Get the credentials and open a remote session
$ServerName = ("SRV02").ToUpper()
$Credentials = Get-Credential
$PSRemoteSession = New-PSSession -ComputerName $ServerName -Credential $Credentials
Write-Output "PowerShell remote session status: $($PSRemoteSession.State)"
#Query AD tiering level from AD
Function Get-ADTieringLevel
{
Param(
$ServerName
)
$OU = ([adsisearcher]"(&(name=$ServerName)(objectClass=computer))").FindOne().path
#Connect Azure
Connect-AzAccount
#You may need to change the subscription co next(OPTIONAL)
Set-AzContext -Subscription XXXXX-XXXXX-XXXX-XXXX-XXXXXXX
Install-Module -Name Az.ConnectedMachine -Force -Verbose
Name Location ProvisioningState
---- -------- -----------------
AMAWindows westeurope Succeeded
time="2021-09-18T21:01:35+03:00" level=info msg="Onboarding Machine. It usually takes a few minutes to complete. Sometimes it may take longer depending
on network and server load status."
time="2021-09-18T21:01:35+03:00" level=info msg="Check network connectivity to all endpoints..."
time="2021-09-18T21:01:36+03:00" level=info msg="All endpoints are available... continue onboarding"
time="2021-09-18T21:01:46+03:00" level=info msg="Successfully Onboarded Resource to Azure" VM Id=XXXXX-XXXX-XXXX-XXXX-XXXXXXXX
Get-AzConnectedMachine : The Resource 'Microsoft.HybridCompute/machines/ SRV02' under resource group 'RG-PROD-IT-AZURE-ARC-WE' was not found. For more
details please go to https://aka.ms/ARMResourceNotFoundFix
At C:\Program Files\WindowsPowerShell\Modules\Az.ConnectedMachine\0.2.0\custom\Connect-AzConnectedMachine.ps1:250 char:9
+ Get-AzConnectedMachine -Name $Name -ResourceGroupName $Resour ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~