This section contains Microsoft 365 (M365) workflow script samples for reference.
$context.O365ImportModules(@(array-of-modules))
The O365ImportModules function lets you load an array of Azure and M365 Windows PowerShell modules. The function supports loading the following modules:
-
Az.Accounts
-
Az.Resources
-
Exchange Online Management
-
Microsoft.Graph
Once the modules are loaded, the function creates a connection to the specified modules with the connection details specified in the O365 script execution configuration workflow activity. For more information, see Creating a Microsoft 365 automation workflow.
Example: Importing all supported Azure and M365 Windows PowerShell modules
In this example, the O365ImportModules function is used to import all Windows PowerShell modules that M365 automation workflows support. After that, one command is invoked for each imported PowerShell module, respectively.
function Microsoft365ScriptTest() { $context.O365ImportModules(@("Az.Accounts", "Az.Resources", "ExchangeOnlineManagement", "Microsoft.Graph")) $context.O365ExecuteScriptCmd("Get-Module | Select-Object -Property ModuleType,Version,Name | Out-File -FilePath C:\WS\Files\ImportedModulesInnerRunspace.txt") Get-AzContext | ConvertTo-Json | Out-File -FilePath C:\WS\Files\Az.txt Get-EXOMailbox -Identity user | ConvertTo-Json | Out-File -FilePath C:\WS\Files\ExchangeOnlineManagement.txt Get-MgUser -UserId "e38349d9-978a-4e4c-809b-189b68fe713a" | ConvertTo-Json | Out-File -FilePath C:\WS\Files\Microsoft.Graph.txt Get-Module | Select-Object -Property ModuleType,Version,Name | Out-File -FilePath C:\WS\Files\ImportedModulesOuterRunspace.txt }
$context.O365ImportModule (module)
The O365ImportModule function lets you load a single M365 or Azure Windows PowerShell module. If you have multiple versions of the specified module installed, you can also specify the module version to load.
NOTE: The O365ImportModule function supports specifying major module versions only (such as version 2.x).
Example: Importing the Azure Az PowerShell module
In this example, the O365ImportModule function is used to import version 2.x of the Microsoft Azure Az Windows PowerShell module.
function TestImportTeamsModule() { $context.O365ImportModule("AzureAz", 2) }
$context.O365ExecuteScriptCmd(string-or-cmd )
The O365ExecuteScriptCmd function passes any string or command specified in the script, then runs and returns the results as a PSObject.
$context.O365RemoveAllModulesSessions()
The O365RemoveAllModulesSessions script disconnects all PSSessions and removes all modules from the PowerShell pool, allowing Active Roles to import new modules again.
Example: Removing all Windows PowerShell module sessions
In this example, the O365RemoveAllModulesSessions function is used to disconnect the PSSession related to a previously loaded Azure Az module, and then remove the Azure Az module from the PowerShell pool.
#Get a list of disabled users and Directory Roles available $_usersinroles= @() $_default_log = "C:\temp\Roles.csv" $context.O365ImportModule("Microsoft.Graph", 1) $context.O365ExecuteScriptCmd("Get-MgUser -filter 'accountEnabled eq false'" +" | Export-Csv " +"c:\temp\DisabledUsers.csv" +" -NoTypeInformation") $context.O365ExecuteScriptCmd("Get-MgDirectoryRole | Export-csv "+$_default_log ) $context.O365RemoveAllModulesSessions()