在指定的日期范围内,用户没有用户会话数据

时间:2018-06-11 12:29:36

标签: powershell

我正在使用以下查询来检索o365 Skype for business service的指定日期范围内的用户会话信息。

$mbxes = Get-CsOnlineUser | select UserPrincipalName
$startTime = "5/1/2018"
foreach ($mbx in $mbxes) {
  Get-CsUserSession -User $mbx -StartTime $startTime
}

运行此查询会为我检索的所有邮箱提供以下警告:

WARNING: There is no user session data for the user @{UserPrincipalName=account1@companyX.onmicrosoft.com} within the specified date range 01/05/2018 00:00:00 -07:00 to
11/06/2018 05:22:31 -07:00.

但是,当我只是运行此命令时:

$mbxes = "account1@companyX.onmicrosoft.com"
$startTime = "5/1/2018"
foreach ($mbx in $mbxes) {
  Get-CsUserSession -User $mbx -StartTime $startTime
}

我正在获取响应此邮箱的所有会话数据。 我想知道为什么当我使用所有邮箱地址创建变量时,即使会话数据存在,也没有给我回数据。

2 个答案:

答案 0 :(得分:4)

在第一个命令中,您包含| select UserPrincipalName。这会将$mboxes指定为仅具有UserPrincipalName属性的对象数组。

从第二个命令看起来,Get-CsUserSession期望-User参数是一个字符串,但您仍然传递一个对象。你可以解决以下任何一个问题:

UserPrincipalName扩展为字符串,然后你有一个字符串数组:

$mbxes = Get-CsOnlineUser | select -ExpandProperty UserPrincipalName
$startTime = "5/1/2018"
foreach ($mbx in $mbxes) {
  Get-CsUserSession -User $mbx -StartTime $startTime
}

扩展以获取字符串数组的另一种方法:

$mbxes = (Get-CsOnlineUser).UserPrincipalName
$startTime = "5/1/2018"
foreach ($mbx in $mbxes) {
  Get-CsUserSession -User $mbx -StartTime $startTime
}

获取会话时显式选择属性:

$mbxes = Get-CsOnlineUser | select UserPrincipalName
$startTime = "5/1/2018"
foreach ($mbx in $mbxes) {
  Get-CsUserSession -User $mbx.UserPrincipalName -StartTime $startTime
}

答案 1 :(得分:1)

用户:@{UserPrincipalName=account1@companyX.onmicrosoft.com}不是有效的userPrincipalName,

改变这个:

$mbxes = Get-CsOnlineUser | select UserPrincipalName

要:

$mbxes = Get-CsOnlineUser | select -Expand UserPrincipalName

请参阅:https://blogs.msdn.microsoft.com/powershell/2009/09/13/select-expandproperty-propertyname/