Forum Discussion

newtotechcom-J's avatar
newtotechcom-J
Brass Contributor
Apr 24, 2024

Unable to delete Archived users from Viva Engage/yammer using powershell script

I want to delete Archived users who are there in VivaEnage/Yammer.
I'm able to export the list but not able to delete users.

Probably, some issues with this uri:
$uri = "https://graph.microsoft.com/v1.0/yammer/users/$userId"
Please suggest, what should I do.

I have created this script, but getting this error in csv:
Failed to remove: Response status code does not indicate success: BadRequest (Bad Request).

 

Script:

 

Set-ExecutionPolicy RemoteSigned
$cred = Import-CliXml -Path 'C:\Script\Vaut\cred2.xml'
$cert_graph = Get-ChildItem Cert:\LocalMachine\My\49054ea0593c0920e42b99fe99e9892833e651ec
$appid_graph="MY_APPID_GRAPH"
$tenantid="MY_TENANT_ID"
$certid="MY_CERT_ID"
$appid="MY_APP_ID"
Connect-MgGraph -ClientID $appid_graph -TenantId $tenantid -Certificate $cert_graph
# Fetch users whose display name contains "Archive"
$users = Get-MgUser -Filter "startswith(displayName, 'Archive')" -All
# Initialize a list to store operation results
$results = @()
# Loop through each user and remove from Viva Engage
foreach ($user in $users) {
$userId = $user.Id
# Attempt to remove the user from Viva Engage (assuming correct API endpoint)
try {
# API endpoint might need modification based on exact requirements
$uri = "https://graph.microsoft.com/v1.0/yammer/users/$userId"
Invoke-MgGraphRequest -Method DELETE -Uri $uri
$results += [PSCustomObject]@{
UserId = $userId
UserPrincipalName = $user.UserPrincipalName
Status = "Removed"
}
} catch {
$errorDetails = $_.Exception.Message
$results += [PSCustomObject]@{
UserId = $userId
UserPrincipalName = $user.UserPrincipalName
Status = "Failed to remove"
ErrorDetails = $errorDetails # Add this line to record the error details
}
}
}
# Export results to CSV
$results | Export-Csv -Path "C:\UserRemovalResults.csv" -NoTypeInformation
# Disconnect the session
Disconnect-MgGraph

 

No RepliesBe the first to reply

Resources