PowerShell的统计脚本问题

我正在尝试为我们的人力资源团队生成一份csv报告,列出:

  • 显示名称
  • 主要的smtp地址
  • SAM帐户
  • 标题
  • 办公室
  • 经理
  • 帐户状态(启用,禁用)
  • 设备模型
  • DeviceOS
  • LastSyncAttemptTime

我的脚本如下:

$users = get-aduser -Filter * ` -SearchBase "ou=hr,ou=burbank,ou=corp,dc=corp,dc=castandcrew,dc=com" ` -Properties * | select DisplayName,EmailAddress,samAccountName,Department,Title,Office,Manager,Enabled "DisplayName;EmailAddress;samAccountName;Department;Title;Office;Manager;Enabled;DeviceModel;DeviceOS;LastSyncAttemptTime" foreach ($u in $users) { $a = Get-MobileDeviceStatistics -Mailbox $u.EmailAddress | select DeviceModel,DeviceOS,LastSyncAttemptTime "{0};{1};{2};{3};{4};{5};{6};{7};{8};{9};" -f $u.DisplayName, $u.EmailAddress, $u.samAccountName, $u.Department, $u.Title, $u.Office, $u.Manager, $u.Enabled, $a.DeviceModel, $a.DeviceOS, $a.LastSyncAttemptTime } 

似乎工作正常,除了一些用户信息不拉如下所示(见林恩约翰逊):

 Lynn Johnson;[email protected];ljohnson;Human Resources;SVP, Human Resources;Burbank;CN=Eric Belcher,OU=Users,OU=Restricted,OU=Burbank,OU=Corp,DC=CORP,DC=CASTANDCREW,DC=COM;True;System.Object[];System.Object[]; Renee Thurau;[email protected];rthurau;Human Resources;Executive Asisstant to SVP, Human Resources;Burbank;CN=Lynn Johnson,OU=Users,OU=HR,OU=Burbank,OU=Corp,DC=CORP,DC=CASTANDCREW,DC=COM;True;iPhone8C1;iOS 10.3.3 14G60; 

有任何想法吗? 谢谢!

问题的根源在于Lynn Johnson有多个设备,您的代码假设Get-MobileDeviceStatistics只会返回一个结果。

因为Lynn的查询返回多个结果,所以你对$a.DeviceModel的引用是对象数组(可能是string)。 您需要根据您的业务需求以某种方式进行说明。 我猜测对于使用CSV的人来说最合乎逻辑的就是每个设备都有一个logging。 所以如果Lynn有3个设备,她会在CSV中有3行。 但是,您也可以尝试将数组的元素连接到单个组合列值中。 或者您可以select只在第一个设备上报告等

还有一些其他的build议,我也为你的脚本。

  • 不要在您的Get-ADUser查询中使用-Properties * 。 处理时间和带宽是浪费的。 你已经知道你想要什么属性。 所以把它们写在脚本里
  • 不要尝试手动格式化CSV输出。 让Powershell的本地Export-CSV cmdlet为您完成工作。 它有一个-Delimiter参数,所以如果你不喜欢逗号默认,你仍然可以使用分号。

与Ryan非常相似,但我喜欢构build一个包含所需内容的对象,然后使用它创build一个包含所有数据的数组。 然后,您可以导出或根据需要使用它。

  • 为查询添加显式属性
  • 使用ForEach循环处理重复的移动设备
  • 使用新的$outputvariables来利用Export-CSV和-Delimtter ';' 选项

脚本代码:

 $users = get-aduser -Filter * ` -SearchBase "ou=hr,ou=burbank,ou=corp,dc=corp,dc=castandcrew,dc=com" ` -Properties DisplayName,EmailAddress,Department,Title,Office,Manager # Set up output as an empty array $output = @() foreach ($u in $users) { # Get Statistics for that user, making an explicit array $Stats = @(Get-MobileDeviceStatistics -Mailbox $u.EmailAddress) $Stats | ForEach-Object { # Create an anonomous object with the properties we want $current = "" | Select-Object "DisplayName","EmailAddress","samAccountName","Department","Title","Office","Manager","Enabled","DeviceModel","DeviceOS","LastSyncAttemptTime" $current.DisplayName = $u.DisplayName $current.EmailAddress =$u.EmailAddress $current.samAccountName =$u.samAccountName $current.Department = $u.Department $current.Title = $u.Title $current.Office = $u.Office $current.Manager = $u.Manager $current.Enabled = $u.Enabled $current.DeviceModel = $_.DeviceModel $current.DeviceOS = $_.DeviceOS $current.LastSyncAttemptTime = $_.LastSyncAttemptTime # Add the current object to the output for later $output += $current } } $output | Export-CSV -Path C:\report.csv -Delimiter ';' -NoTypeInformation -NoClobber