ApplicationInsights set sampling to one percent

here is an sample powershell script to set sampling 1% to all appinsights

# Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force
# Connect-AzAccount
# Get-AzSubscription
# Get-AzSubscription | Where-Object Name -Match Azure | Set-AzContext

foreach($item in Get-AzApplicationInsights) {

    if ($item.SamplingPercentage -and $item.SamplingPercentage -le 1) {
        # Write-Host $item.Name -ForegroundColor Cyan
        continue
    }

    $resource = Get-AzResource -Name $item.Name -ResourceType Microsoft.Insights/Components -ResourceGroupName $item.ResourceGroupName
    # $resource.Properties.SamplingPercentage = 12.5
    $resource.Properties.SamplingPercentage = 1
    try {
        $resource | Set-AzResource -Force | Out-Null
        Write-Host $item.Name -ForegroundColor Green
    } catch {
        Write-Host $item.Name -ForegroundColor Red
    }
}