【问题标题】:How to get the samAccountName from a csv file exported from Teams via powershell如何从通过 powershell 从 Teams 导出的 csv 文件中获取 samAccountName
【发布时间】:2020-07-30 16:30:59
【问题描述】:


我从 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"

这将显示以下内容:

显示名称 LastActivity(UTC 时间) ------------ ----------- 汤姆·史密斯 2020-04-16T01:00:47Z 乔博客 2020-04-16T01:00:47Z 哈里·布里格斯 2020-04-16T01:00:47Z 杰夫克里 2020-04-16T01:00:47Z 简·布里格斯 2020-04-15T23:17:29Z 贝蒂·史密斯 2020-04-06T02:56:51Z

我需要删除“LastActivity(UTC 时间)”下低于第一个日期的记录
以下内容:2020-04-16
然后在 Active Directory 上运行 Get-ADUser 命令以获取每条记录的 samAccountName 并将其放入第三列。

DisplayName LastActivity(UTC 时间)用户 ID ------------ ----------- ------ 汤姆史密斯 2020-04-16T01:00:47Z tsmith 乔博客 2020-04-16T01:00:47Z 哈里·布里格斯 2020-04-16T01:00:47Z 杰夫克里 2020-04-16T01:00:47Z

一直在测试从 ActiveDirectory 获取 ADUser 以返回 samaccountname 的一大堆方法,当我使用静态文本时,它仅适用于一条记录

Get-ADUser -filter { DisplayName -eq 'Tom Smith' } | Select samAccountName

当我导入 csv 文件并为每一行运行一个 foreach 循环时不会。
我测试过的代码,我认为应该返回我需要的代码如下:

$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

以以下错误结束,前行:

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

【问题讨论】:

  • 在导入 csv 并运行 foreach 循环的位置显示您尝试的代码
  • @Itchydon - 我添加了用于测试 foreach 循环的代码

标签: powershell csv active-directory


【解决方案1】:

你快到了。您需要一个自定义字段:

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"

【讨论】:

  • 哦,你说得对,快到了。这给了我第三列与 samaccountname。谢谢。
  • 如何删除“LastActivity(UTC 时间)”之后的值:2020-04-16.....?例如2020-04-15……或2020-04-14……等
  • 或者我是否需要在其中放置一个手动 Where 子句来过滤 LastActivity 值? Where-Object { $PSItem.Activity -eq '2020-04-16*' }
  • 您可能希望将该字段作为自定义字段转换为 [datetime] 值,然后您将能够使用 Where-Object {}(或简称:? {})对其进行过滤。跨度>
  • 添加自定义时间字段后可能类似于? {$_.Time -gt ((Get-Date).AddDays(-10))}
猜你喜欢
  • 2015-06-14
  • 2020-09-16
  • 2015-09-29
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 2022-10-07
  • 2023-02-07
  • 2017-01-05
相关资源
最近更新 更多