【问题标题】:How can I get a report of changes made to Teams?如何获取对 Teams 所做更改的报告?
【发布时间】:2022-11-18 12:35:26
【问题描述】:
我正在使用下面的脚本从 .csv 填充多个团队:
# Read team users from CSV file
$TeamUsers = Import-CSV "Import_Path.csv"
$i = 0
# Group the objects by their TeamDesc property, so we only query each group once
$TeamUsers | Group-Object TeamDesc | ForEach-Object {
#Get the GroupId of this Group
$groupId = (Get-Team -DisplayName $_.Name).GroupId
#If the Group couldn't be found, just skip below logic
if(-not $groupId) { return }
#If the Group could be found, create a hashtable for future splatting
$params = @{ GroupId = $groupId }
#now we can enumerate each object in each group of objects
foreach($user in $_.Group){
try {
# create a hashtable for splatting progress
$progress = @{
PercentComplete = $i++ / $TeamUsers.Count * 100
Activity = 'Adding Users to MS Teams!!'
Status = 'Working on Team: "{0}" and User: "{1}"' -f $_.Name, $user.UserPrincipalName
}
Write-Progress @progress
# add this user with this role to the hashtable
$params['User'] = $user.UserPrincipalName
$params['Role'] = $user.Role
Add-TeamUser @params
}
catch {
('Error occurred for {0} - {1}' -f $user.TeamName, $user.UserPrincipalName),
$_.ToString() | Write-Warning
}
}
}
我想包括导出运行期间所采取操作的报告的功能。理想情况下,在脚本结束时,我将能够收集一个文件来描述哪些用户已添加到哪些团队。目前,该脚本将在 PowerShell 界面中使用 Write-Progress 行显示操作,但我也希望能够有一个切实的报告。
【问题讨论】:
标签:
powershell
export
report
microsoft-teams
【解决方案1】:
不确定是否有更简单的方法,但我会创建一个包含相关信息的新对象,然后在流程结束时导出一个新的 CSV。
所以在开始时,声明一个新数组:
$AllActions = @()
然后,在“Add-TeamUser”行之后,将这些添加到:
$ActionObj = New-Object PSObject
$ActionObj | Add-Member NoteProperty -Name GroupID -value $GroupID
$ActionObj | Add-Member NoteProperty -Name User -Value $user.UserPrincipalName
$ActionObj | Add-Member NoteProperty -Name Role -Value $User.Role
$AllActions += $ActionObj
现在,在最后添加:
$AllActions | Export-csv .AllActionsTaken.csv
所以最终的脚本是:
# Read team users from CSV file
$AllActions = @()
$TeamUsers = Import-CSV "Import_Path.csv"
$i = 0
# Group the objects by their TeamDesc property, so we only query each group once
$TeamUsers | Group-Object TeamDesc | ForEach-Object {
#Get the GroupId of this Group
$groupId = (Get-Team -DisplayName $_.Name).GroupId
#If the Group couldn't be found, just skip below logic
if(-not $groupId) { return }
#If the Group could be found, create a hashtable for future splatting
$params = @{ GroupId = $groupId }
#now we can enumerate each object in each group of objects
foreach($user in $_.Group){
try {
# create a hashtable for splatting progress
$progress = @{
PercentComplete = $i++ / $TeamUsers.Count * 100
Activity = 'Adding Users to MS Teams!!'
Status = 'Working on Team: "{0}" and User: "{1}"' -f $_.Name, $user.UserPrincipalName
}
Write-Progress @progress
# add this user with this role to the hashtable
$params['User'] = $user.UserPrincipalName
$params['Role'] = $user.Role
Add-TeamUser @params
$ActionObj = New-Object PSObject
$ActionObj | Add-Member NoteProperty -Name GroupID -value $GroupID
$ActionObj | Add-Member NoteProperty -Name User -Value $user.UserPrincipalName
$ActionObj | Add-Member NoteProperty -Name Role -Value $User.Role
$AllActions += $ActionObj
}
catch {
('Error occurred for {0} - {1}' -f $user.TeamName, $user.UserPrincipalName),
$_.ToString() | Write-Warning
}
}
}
$AllActions | Export-csv .AllActioonsTaken.csv