简体   繁体   English

如何过滤Get-ADComputer输出

[英]How to filter Get-ADComputer output

My Get-ADComputer script gives too much information. 我的Get-ADComputer脚本提供了太多信息。 I would like to shorten it out a little. 我想把它缩短一点。

$Computer = Read-Host -Prompt 'Input computer name'
$ManagedBy = Get-ADComputer $Computer -Properties ManagedBy |
             foreach { $_.ManagedBy }
Write-Output $ManagedBy

When I tried to run my scrip it gives this to output 当我尝试运行脚本时,将其输出

CN=Last Name First Name ,OU=XX ,OU=XXX ,OU=XXX ,DC=XXX,DC=XXX

I would like to get only CN in the output (First name and Las Name). 我只想在输出中得到CN(名字和Las Name)。

Your code returns the distinguished name of the computer's manager. 您的代码返回计算机管理员的专有名称。 You can use that DN to query the AD user object and obtain the desired properties from that (like FullName , or DisplayName , or the individual values FirstName and LastName ). 您可以使用该DN查询AD用户对象,并从中获取所需的属性(例如FullNameDisplayName ,或单个值FirstNameLastName )。

Get-ADComputer $Computer -Properties ManagedBy |
    Select-Object -Expand ManagedBy |
    Get-ADUser -Property FullName |
    Select-Object -Expand FullName

Firstly have you looked at the objects properties? 首先,您是否查看了对象属性? These Properties are auto assigned to the variable, when created. 这些属性在创建后会自动分配给变量。

You can see them with: 您可以通过以下方式看到它们:

$ManagedBy | $ ManagedBy | Get-Member 获得会员

You may well find that $ManagedBy.Name will give exactly what you want. 您可能会发现$ ManagedBy.Name会完全提供您想要的。

Further reading for you: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/get-member?view=powershell-6 为您进一步阅读: https : //docs.microsoft.com/zh-cn/powershell/module/microsoft.powershell.utility/get-member?view=powershell-6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM