Last active
June 19, 2024 19:53
-
-
Save SvenAelterman/fe3d9aa6781ea15c70dfa9cada62e619 to your computer and use it in GitHub Desktop.
Remove-ArcEnabledServersByOsName.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Parameters | |
[CmdletBinding(SupportsShouldProcess)] | |
param ( | |
[Parameter(Mandatory = $true)] | |
[string] $ResourceGroupName, | |
[Parameter()] | |
[string] $OsName = "linux" | |
) | |
# Get all Azure Arc enabled servers in the resource group | |
$arcMachines = Get-AzResource -ResourceGroupName $ResourceGroupName | Where-Object { $_.Type -eq "Microsoft.HybridCompute/machines" } | |
# Review all Azure Arc-enabled servers | |
foreach ($arcMachine in $arcMachines) { | |
$ResourceId = $arcMachine.ResourceId | |
# Retrieve the properties of each server | |
$ResourceProperties = (Get-AzResource -ResourceId $ResourceId -ExpandProperties).Properties | |
# If the OS matches the OS to be deleted | |
if ($ResourceProperties.OsName.ToLower() -eq $OsName) { | |
Write-Output "Removing resource $($arcMachine.Name)" | |
# Delete that server from Azure | |
# Note: This will not remove the agent or any extensions from the OS | |
Remove-AzResource -ResourceId $ResourceId -Force -WhatIf:$WhatIfPreference | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment