Exporting Data
Export Organizational Units
gsm orgUnits list --type all --fields "organizationUnits(name,orgUnitPath,parentOrgUnitPath)" | ConvertFrom-Json | Export-Csv -Path "./orgUnits.csv" -Delimiter ',' -NoClobber
The important part here is the use of --type all
, which will make GSM export all organizational units, instead of just those immediately under the root.
Export Group Data
$groups = gsm groups list --fields "groups(email,name,description),nextPageToken" | ConvertFrom-Json
foreach ($group in $groups) {
New-Object PSObject -Property ([Ordered]@{
email = $group.email
name = $group.name
description = $group.description
}) | Export-Csv -Path "./groups.csv" -Delimiter ',' -NoClobber -Append
}
Here we create a new PSObject for each group, before calling Export-Csv. We also use an Ordered Hashtable for the property to make sure the order of the columns is always the same.
List group members for a list of groups
$groupMembers = gsm members list batch --path "./groups.csv" --delimiter "," --groupKey 1 --skipHeader | ConvertFrom-Json
foreach ($group in $groupMembers) {
foreach ($member in $group.members) {
New-Object PSObject -Property ([Ordered]@{
groupKey = $group.groupKey
email = $member.email
role = $member.role
}) | Export-Csv -Path "./groupMembers.csv" -Delimiter ',' -NoClobber -Append
}
}
This code uses a pre-created CSV file in which the first column is a list of group email addresses. We also use the --skipHeader
switch to tell GSM to skip the first row of the CSV file.
This will create a single CSV file containing rows with the email address of the group, member and role for each group-member combination. This is the most easy to use format for further scripting.
List aliases for a list of groups
$groupAliases = gsm groupAliases list batch --path "./groups.csv" --groupKey 1 --skipHeader | ConvertFrom-Json
foreach ($group in $groupAliases) {
foreach ($groupAlias in $group.aliases) {
New-Object PSObject -Property ([Ordered]@{
groupKey = $group.groupKey
alias = $groupAlias.alias
}) | Export-Csv -Path "./groupAliases.csv" -Delimiter ',' -NoClobber -Append
}
}
This code uses a pre-created CSV file in which the first column is a list of group email addresses. We also use the --skipHeader
switch to tell GSM to skip the first row of the CSV file.
This will create a single CSV file containing rows with the email address of the group and the alias for each group-alias combination. This is the most easy to use format for further scripting.
Get group settings for a list of groups
gsm groupSettings get batch --path "./groups.csv" --groupUniqueId 1 --skipHeader | ConvertFrom-Json | Export-Csv -Path "./groupSettings.csv") -Delimiter ',' -NoClobber -Append
As we can see in this exmaple, we don’t always need to create loops and objects when we want to export stuff. Depending on our use case, it is usually possible to just convert our response to JSON and immediately pipe that to the Export-Csv commandlet.
Export User Data
$users = gsm users list --fields "*" --viewType "admin_view" --projection "full" | ConvertFrom-Json
foreach ($user in $users) {
New-Object PSObject -Property ([Ordered]@{
primaryEmail = $user.primaryEmail
familyName = $user.name.familyName
givenName = $user.name.givenName
genderType = $user.gender.type
orgUnitPath = $user.orgUnitPath
recoveryEmail = $user.recoveryEmail
recoveryPhone = $user.recoveryPhone
addresses = Get-Multivalues -multivalues $user.addresses
}) | Export-Csv -Path $exportFiles.users -Delimiter ',' -NoClobber -Append
}
This example shows how we can deal with multi-value attributes. See also Dealing with nested and multi-value properties.
SEE ALSO
- gsm - Command overview
- batch commands - How to use batch commands
- scripting examples - Some examples on how to use GSM in scripts
- example script - Example script to export data
- examples - See some examples