Thank you! Invoke-Command did the trick since it's possible to pass arguments to it.
This is what I ended up doing:
$scriptBlock = {
import-Module AzureAD
$creds = $args[0]
$userid = $args[1]
Connect-AzureAD -Credential $creds
Revoke-AzureAdUserAllRefreshToken -objectID $userid
}
# Bind to member
$member = $Context.BindToObject("Adaxes://[domain]/%distinguishedName%")
# Get Office 365 Properties
$office365Properties = $member.GetOffice365Properties()
try {
$objectId = [Guid]$member.Get("adm-O365ObjectId")
$objectId = $objectId.ToString()
}
catch {
$Context.LogMessage("The member doesn't have an Office 365 account", "Warning")
return
}
# Get Office 365 Tenant credentials
$office365Cred = $Context.GetOffice365Credential()
Invoke-Command -ComputerName localhost -ScriptBlock $scriptBlock -ArgumentList $office365Cred, $objectId