Untitled

 avatar
unknown
plain_text
a year ago
2.2 kB
5
Indexable
# Set path to the CSV file
$csvPath = "C:\temp\user.csv"

# Import the CSV file
$users = Import-Csv -Path $csvPath

# Group users by 'Functie'
$groupedUsers = $users | Group-Object -Property Functie

# Iterate over each group
foreach ($group in $groupedUsers) {
    # Calculate 8% of users for the current function
    $totalUsers = $group.Count
    $percent8 = [math]::Ceiling($totalUsers * 0.08)

    # Get the users already marked as "ok"
    $existingOkUsers = $group.Group | Where-Object { $_.Retea_test -eq "ok" }
    $existingOkCount = $existingOkUsers.Count

    # Debug output
    Write-Host "Function: $($group.Name)"
    Write-Host "Total Users: $totalUsers"
    Write-Host "Percent 8%: $percent8"

    # If there are existing users marked as "ok"
    if ($existingOkCount -gt 0) {
        $existingOkNames = $existingOkUsers | ForEach-Object { $_.Name }
        Write-Host "Existing Ok Users: $($existingOkNames -join ', ')"
    } else {
        Write-Host "Existing Ok Users: None"
    }

    Write-Host "Existing Ok Count: $existingOkCount"

    # Calculate the number of additional users needed to reach 8%
    $numberToSelect = $percent8 - $existingOkCount

    Write-Host "Number to Select: $numberToSelect"

    # Ensure the number of additional users to select is at least 0
    if ($numberToSelect -gt 0) {
        # Select random users from the remaining users in the current function
        $remainingUsers = $group.Group | Where-Object { $_.Retea_test -ne "ok" }
        
        if ($remainingUsers.Count -gt 0) {
            $selectedUsers = $remainingUsers | Get-Random -Count $numberToSelect

            # Update 'Retea_test' column for the selected users
            foreach ($user in $selectedUsers) {
                $user.Retea_test = "ok"
            }
        }
    }
}

# Export the updated users back to CSV
$users | Export-Csv -Path $csvPath -NoTypeInformation

# Debug output: Verify the final counts
$finalGroupedUsers = $users | Group-Object -Property Functie
foreach ($group in $finalGroupedUsers) {
    $okCount = ($group.Group | Where-Object { $_.Retea_test -eq "ok" }).Count
    Write-Host "Function: $($group.Name), Ok Count: $okCount"
}
Editor is loading...
Leave a Comment