【问题标题】:Get-ADUser : Invalid type 'System.String[]'Get-ADUser:无效类型“System.String[]”
【发布时间】:2020-05-14 21:44:37
【问题描述】:

我正在尝试获取连接到 Windows RAS 服务器的用户列表(基本上是连接 VPN 的用户列表)并获取他们的 AD 帐户属性,

以下命令返回数据如下(它返回 100 多个用户,但为了简洁我只展示了一个)

Get-RemoteAccessConnectionStatistics | fl
ClientIPAddress       : 1.2.3.4
UserName              : {Bob.Jones@company.com}
ConnectionDuration(s) : 696055
ConnectionType        : Vpn

所以我写了以下脚本:

$users = Get-RemoteAccessConnectionStatistics
foreach ($user in $users) {
$UPN = $user.username
Get-ADUser -Filter {UserPrincipalName -eq $UPN }
}

但是在运行脚本时出现以下错误:

Get-ADUser : Invalid type 'System.String[]'.
Parameter name: userPrincipalName
At line:7 char:1
+ Get-ADUser -Filter {UserPrincipalName -eq $UPN }
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ArgumentException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.GetADUser

有人可以帮忙吗?我不知道我做错了什么

【问题讨论】:

  • 错误提示$UPN 包含一个字符串数组。仔细检查其内容。

标签: powershell


【解决方案1】:

Get-RemoteAccessConnectionStatistics 返回的UserName 属性是一个数组(注意Format-List 输出中值周围的{}),只获取第一个值:

$users = Get-RemoteAccessConnectionStatistics
foreach ($user in $users) {
    $UPN = $user.username |Select -First 1
    Get-ADUser -Filter "UserPrincipalName -eq '$UPN'"
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-21
    • 1970-01-01
    • 2020-03-31
    • 1970-01-01
    • 2012-09-07
    • 2014-03-14
    • 2017-11-24
    • 1970-01-01
    相关资源
    最近更新 更多