Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save SvenAelterman/fe3d9aa6781ea15c70dfa9cada62e619 to your computer and use it in GitHub Desktop.
Save SvenAelterman/fe3d9aa6781ea15c70dfa9cada62e619 to your computer and use it in GitHub Desktop.
Remove-ArcEnabledServersByOsName.ps1
# 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