Get-ADPrincipalGroupMembership-身份不接受变量 [英] Get-ADPrincipalGroupMembership -Identity not accepting variable

查看:294
本文介绍了Get-ADPrincipalGroupMembership-身份不接受变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在几个具有不同命名架构的不同域中工作。因此,我正在编写一个进入每个域的脚本,并检查它们的组成员身份。

Working in a few different domains, that have different naming schema's. So I'm writing a script that goes into each domain, and checks on their group membership.

脚本要做的第一件事是询问用户的姓氏。然后我使用 Get-ADUser 选择 samaccountname 并将其绑定到变量( samaccountname Get-ADPrincipalGroupMembership -Identity 参数接受的唯一名称)。

First thing the script does is ask for the user's last name. Then I use Get-ADUser to select the samaccountname and tie it to a variable (samaccountname is the only name that Get-ADPrincipalGroupMembership -Identity parameter accepts).

但是当我使用 -Identity 变量运行脚本时,找不到用户。如果我手动输入-它确实找到了用户。

But when I run the script using the variable for -Identity it doesn't find the user. If I type it in manually - it does find the user.

这里是代码:

$surname = Read-Host "Users Last Name" 

$fullname = Get-ADUser -filter * | Where-Object {$_.surname -eq $surname} |
            select samaccountname | Format-Table -HideTableHeaders | Out-String
Get-ADPrincipalGroupMembership -Identity $fullname | select name |
  Format-Table -HideTableHeaders

我得到的错误表明变量是一个字符串,并且它是要搜索的正确用户,但错误提示它找不到该用户。

The error I get shows that the variable is a string, and it is the correct user that it's searching for, but the error says it can't find that user.

推荐答案

Format-* cmdlet用于向用户显示数据。当需要/打算对数据进行进一步处理时,请勿使用它们。

Format-* cmdlets were made for displaying data to a user. Do not use them when further processing of the data is required/intended.

更改

... | select samaccountname | Format-Table -HideTableHeaders | Out-String

... | select -Expand samaccountname -First 1

,您的问题就会消失。

这篇关于Get-ADPrincipalGroupMembership-身份不接受变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆