【发布时间】: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 并将其放入第三列。
一直在测试从 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