Skip to content

Instantly share code, notes, and snippets.

@mabster
Created October 25, 2022 23:14
Show Gist options
  • Save mabster/262060576dd0bb1afda65ecca57173eb to your computer and use it in GitHub Desktop.
Save mabster/262060576dd0bb1afda65ecca57173eb to your computer and use it in GitHub Desktop.
Starts a call with a random, available member of a group.
[CmdletBinding(SupportsShouldProcess)]
param(
[Parameter(Mandatory, Position = 0)]
[string] $GroupName
)
function Test-Module($moduleName) {
if (-not (Get-Module $moduleName -ListAvailable)) {
Install-Module $moduleName -Scope CurrentUser -Force
}
}
Test-Module Microsoft.Graph.Users
Test-Module Microsoft.Graph.Groups
Test-Module Microsoft.Graph.CloudCommunications
$ctx = Get-MgContext
if ($null -eq $ctx) {
Connect-MgGraph -Scope Directory.Read.All,Presence.Read.All | Out-Null
}
try {
$group = Get-MgGroup -Filter "displayName eq '$GroupName'"
if ($null -eq $group) {
Write-Warning "Group '$GroupName' was not found."
return
}
$members = Get-MgGroupMember -groupid $group.id -Filter "userPrincipalName ne '$((Get-MgContext).Account)'" -CountVariable c -ConsistencyLevel eventual
if ($null -eq $members) {
Write-Warning "Group '$GroupName' has no members."
return
}
$available = Get-MgCommunicationPresenceByUserId -Ids $members.id | Where-Object Availability -eq 'Available'
if ($null -eq $available) {
Write-Warning "No members of group '$GroupName' are available."
return
}
$id = ($available | Get-Random).id
$callee = Get-MgUser -UserId $id
if ($null -eq $callee) {
Write-Warning "Could not get details for user '$id'."
return
}
if ($PSCmdlet.ShouldProcess("$($callee.DisplayName) ($($callee.UserPrincipalName))", "Calling")) {
start "callto:$($callee.userPrincipalName)"
}
}
finally {
if ($null -eq $ctx) {
Disconnect-MgGraph | Out-Null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment