【问题标题】:Disable Hash Result?禁用哈希结果?
【发布时间】:2019-04-22 18:49:28
【问题描述】:

我确定这可能是一个让我难以理解的非常简单的答案,但我现在已经使用这种编码进行了相当一段时间,并且需要以更用户友好的方法对其进行格式化。

代码如下:

$OU1 = 'CN=MbrshpOU, OU=Group, DC=place,DC=com'

Write-Host "Select user:"

$UName=Get-ADGroupMember -identity "$OU1" | Select sAMAccountName | Out-GridView -PassThru
$UserName = $UName 

--Using this displays nothing in the Write-Host below: $UserName = ($UName).sAMAccountName

Write-Host ""
Write-Host " The user is: " -NoNewline;
Write-Host -ForegroundColor Green $UserName -NoNewline;
Write-Host " is this correct? Y/N " -NoNewline

(Choice code follows)

这是目前发生的情况:

“用户是:@{sAMAccountName=first.last} 这是正确的吗?Y/N >”

我需要的是:

“用户是:first.last 是否正确?Y/N >”

想法?

【问题讨论】:

  • 使用-PassThru,您可以选择多个SamAccountNames,以剥离我建议仅将$UserName = $UName[0].SamAccountName 用于第一个条目的属性名称。
  • @LotPings 添加 [0] 解决了问题兄弟,我很感激。如果您想将您的评论更改为答案,我会标记它

标签: powershell active-directory powershell-ise


【解决方案1】:
$OU1 = 'CN=Disabled Users,OU=SecurityGroups,OU=Groups,OU=Production,DC=ad,DC=evotec,DC=xyz'

Write-Host "Select user:"

$UName = Get-ADGroupMember -identity "$OU1" | Get-ADUser -Properties GivenName, SurName | Out-GridView -PassThru
$UserName = $UName

Write-Host ""
Write-Host " The user is: " -NoNewline;
Write-Host -ForegroundColor Green ("$($UserName.GivenName) $($UserName.SurName)") -NoNewline;
Write-Host " is this correct? Y/N " -NoNewline

我使用 Write-Color 的版本(我的 PowerShell 模块):

$OU1 = 'CN=Disabled Users,OU=SecurityGroups,OU=Groups,OU=Production,DC=ad,DC=evotec,DC=xyz'

Write-Host "Select user:"

$UName = Get-ADGroupMember -identity "$OU1" | Get-ADUser -Properties GivenName, SurName | Out-GridView -PassThru
$UserName = $UName

Write-Color -LinesBefore 1 -Text "The user is: ", "$($UserName.GivenName) $($UserName.SurName)", ". Is this correct? Y/N" -Color White, Green, White

您需要知道的是Get-ADGroupMember 具有一组有限的属性。这意味着您需要使用Get-ADuser 来获取所需的缺失属性。

我建议使用 Write-Color 进行显示,因为它更容易玩您正在使用的那种场景。这是链接:https://github.com/EvotecIT/PSWriteColor

当然,我假设您需要名字和姓氏。如果您只想要 SamAccountName,那就容易多了。

$OU1 = 'CN=Disabled Users,OU=SecurityGroups,OU=Groups,OU=Production,DC=ad,DC=evotec,DC=xyz'

Write-Host "Select user:"

$UName = Get-ADGroupMember -identity "$OU1" | Out-GridView -PassThru
$UserName = $UName

Write-Color -LinesBefore 1 -Text "The user is: ", $UserName.SamAccountName, ". Is this correct? Y/N" -Color White, Green, White

或者如果是您的代码:

$OU1 = 'CN=Disabled Users,OU=SecurityGroups,OU=Groups,OU=Production,DC=ad,DC=evotec,DC=xyz'

Write-Host "Select user:"

$UName = Get-ADGroupMember -identity "$OU1" | Get-ADUser -Properties GivenName, SurName | Out-GridView -PassThru
$UserName = $UName

Write-Host ""
Write-Host " The user is: " -NoNewline;
Write-Host -ForegroundColor Green $UserName.SamAccountName -NoNewline;
Write-Host " is this correct? Y/N " -NoNewline

【讨论】:

  • 谢谢!哇,这个答案做了很多工作,非常有见地且易于阅读 - 谢谢!
【解决方案2】:

我知道你得到了答案,但这是另一种尝试方法......

Get-ADGroupMember -Identity $(Get-ADOrganizationalUnit -Filter '*' | 
Select-Object -Property Name, DistinguishedName | 
Out-GridView -PassThru -Title 'Select a Organizational Unit to see the users in that OU').Name | 
Select-Object -Property Name, SamAccountName | 
Out-GridView -PassThru -Title 'Select a username to review' 
Read-Host -Prompt 'The user is displays as: first.last. Is this correct? Y/N >'

…所有 OGV 驱动,无需 Write-*。

【讨论】:

  • 我喜欢这个,Write-Host 一直是一个奇怪的东西,我会制作另一个程序,使用类似于我上面列出的东西,但是,我会尝试你的方法前进.感谢您花时间向我展示这个!
  • 不用担心。很高兴它会有用。通常应避免使用 Write-Host,除非您出于某些其他自定义原因将彩色文本写入屏幕。输出到屏幕是任何结果、简单字符串或变量的默认值,除非您将其设置为不输出。使用 OGV (reddit.com/r/PowerShell/comments/b3q0qo/… 和 mcpmag.com/articles/2016/02/17/…) 和 Show -Command (powershell.getchell.org/2018/02/13/poor-mans-gui) 构建 GUI 很快,但并不优雅。但是 WPF 很酷。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-06
  • 2015-08-31
  • 1970-01-01
  • 2017-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多