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

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

问题描述

在几个不同的域中工作,这些域具有不同的命名模式.所以我正在编写一个脚本,进入每个域,并检查他们的组成员身份.

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 并将其绑定到一个变量(samaccountnameGet-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 -Identity 不接受变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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