[英]How to get the samAccountName from a csv file exported from Teams via powershell
I have exported a CSV file from Microsoft Teams and got the data that I required after running this command:我从 Microsoft Teams 导出了 CSV 文件,并在运行此命令后获得了所需的数据:
Import-Csv -Path "C:\TeamsUserActivity.csv" |
Where-Object { $PSItem.DisplayName -notlike "*-*" } |
Select-Object -Property DisplayName,'LastActivity (UTC Time)' |
Sort-Object -Property 'LastActivity (UTC Time)' -Descending |
Export-Csv -Path "C:\TeamsUsers.csv"
This displays the following:这将显示以下内容:
DisplayName LastActivity (UTC Time) ----------- ----------------------- Tom Smith 2020-04-16T01:00:47Z Joe Bloggs 2020-04-16T01:00:47Z Harry Briggs 2020-04-16T01:00:47Z Jeff Kerry 2020-04-16T01:00:47Z Jane Briggs 2020-04-15T23:17:29Z Betty Smith 2020-04-06T02:56:51Z
I need to remove the records under "LastActivity (UTC Time)" that are below the first date我需要删除“LastActivity(UTC 时间)”下低于第一个日期的记录
Anything below: 2020-04-16以下任何内容:2020-04-16
Then run a Get-ADUser command on Active Directory to get the samAccountName for each record and put it into a third column.然后在 Active Directory 上运行 Get-ADUser 命令以获取每条记录的 samAccountName 并将其放入第三列。
DisplayName LastActivity (UTC Time) UserID ----------- ----------------------- ------ Tom Smith 2020-04-16T01:00:47Z tsmith Joe Bloggs 2020-04-16T01:00:47Z jbloggs Harry Briggs 2020-04-16T01:00:47Z hbloggs Jeff Kerry 2020-04-16T01:00:47Z jkerry
Been testing a whole bunch of methods to Get-ADUser from ActiveDirectory to return the samaccountname, it will only work for one record when I use static text一直在测试从 ActiveDirectory 获取 ADUser 以返回 samaccountname 的一大堆方法,当我使用 static 文本时,它仅适用于一条记录
Get-ADUser -filter { DisplayName -eq 'Tom Smith' } | Select samAccountName
Not when I import the csv file and run a foreach loop for each row.不是当我导入 csv 文件并为每一行运行一个 foreach 循环时。
The code I have tested, which I would think shuld return what I need is below:我测试过的代码,我认为应该返回我需要的代码如下:
$in_file = "C:\PS\SessionData\sessionTMSct.csv"
$out_file = "C:\PS\SessionData\sessionTMSctout.csv"
$out_data = @()
ForEach ($row in (Import-Csv $in_file)) {
If ($row.DisplayName) {
$out_data += Get-ADUser $row.DisplayName -Properties samAccountName
}
}
$out_data |
Select DisplayName,'LastActivity (UTC Time)',SamAccountName |
Export-Csv -Path $out_file -NoTypeInformation
Ends in the following error, pre row:以以下错误结束,前行:
Get-ADUser : The term 'Get-ADUser' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:8 char:22
+ $out_data += Get-ADUser $row.DisplayName -Properties samAccou ...
+ ~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-ADUser:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
You were nearly there.你快到了。 A custom field is what you need:
您需要一个自定义字段:
Import-Csv -Path "C:\TeamsUserActivity.csv" | `
Where-Object { $PSItem.DisplayName -notlike "*-*" } | `
Select-Object -Property DisplayName,'LastActivity (UTC Time)',`
@{l="samAccountName";e={$DN = $_.DisplayName; `
(Get-ADUser -filter {DisplayName -eq $DN}).samAccountName}} | `
Export-CSV -path "C:\ExportedTeamsUserActivity.csv"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.