简体   繁体   中英

How to get via Powershell AD computer owner attributes like email and account name?

I have a computers that have assigned to users as managedby, I want to get list in JSON format where hostname is a key, and user attributes are values.

But I stuck to get that in one command :/ and put that in json, so use csv for a while.

I run these 2 commands succesfuly:

  1. Get-ADComputer -Filter * -property managedby | select name, managedby > C:\\Windows\\Temp\\computerowners.csv

  2. Get-ADUser -Filter * -SearchBase 'CN=My User,DC=example,DC=com' -Properties SamAccountName | Format-Table -Property Name, samaccountname, userprincipalname -AutoSize

where search base is managedby value from first one.

I expect to have output like that: hostname, name, samaccountname, userprincipalname

I try to combine above 2 commands like that:

Get-ADComputer -Filter * -property managedby | foreach {get-aduser -Filter * -SearchBase $managedby -Properties name, samaccountname, userprincipalname} | select name, samaccountname, userprincipalname > C:\Windows\Temp\computerowners.csv

but it want work - as not pickup managedby properly as I understand... any help with saving that in json will be more than welcome.

You didn't define the variable $managedby that you use in the ForEach-Object loop, hence the variable is $null . You need to use the property ManagedBy of the current object in the pipeline ( $_.ManagedBy ).

With that said, you're making the whole thing way more complicated than it needs to be. PowerShell can do a lot of the heavy lifting for you if you allow it to. Get-ADUser can read from the pipeline, so all you need to do is pass the owner's distinguished name. You also don't need to explicitly specify the properties Name , SamAccountName and UserPrincipalName , because Get-ADUser returns them by default. Plus, since you want CSV output anyway, use Export-Csv instead of the redirection operator.

Get-ADComputer -Filter * -Property managedby |
  Select-Object -Expand ManagedBy |
  Get-ADUser |
  Select-Object Name, SamAccountName, UserPrincipalName |
  Export-Csv C:\Windows\Temp\computerowners.csv -NoType

To include the computername in the output adjust the above code as follows:

Get-ADComputer -Filter * -Property managedby |
  ForEach-Object {
    $computer = $_.Name
    if ($_.ManagedBy) { Get-ADUser $_.ManagedBy } else { '' }
  } |
  Select-Object @{n='ComputerName';e={$computer}}, Name, SamAccountName,
                UserPrincipalName |
  Export-Csv C:\Windows\Temp\computerowners.csv -NoType

To get a datastructure that can be exported to JSON using the computername as the key for the nested user attributes a different approach would be more elegant, though. Collect all relevant user attributes in a hashtable with the computername as the key:

$computers = @{}
Get-ADComputer -Filter * -Property managedby | ForEach-Object {
  $computers[$_.Name] = if ($_.ManagedBy) {
    Get-ADUser $_.ManagedBy | Select-Object Name, SamAccountName, UserPrincipalName
  } else {
    New-Object -Type PSObject -Property @{
      Name              = ''
      SamAccountName    = ''
      UserPrincipalName = ''
    }
  }
}

Then create an object from that hashtable and convert it to JSON:

New-Object -Type PSObject -Property $computers | ConvertTo-Json

This should get you pretty far:

Get-ADComputer -Filter * -Property ManagedBy,CN | ForEach-Object {

    # only query AD if there actually is a manager
    if ($_.ManagedBy) {
        $manager = $_.ManagedBy | Get-ADUser
    } else {
        $manager = $null
    }

    # return a custom object with 4 properties    
    [pscustomobject]@{
        hostname = $_.CN
        name = $manager.Name
        samaccountname = $manager.SamAccountName
        userprincipalname = $manager.UserPrincipalName
    } 
}

Note: Any value created in a script block and not explicitly captured in a variable or explicitly discarded via Out-Null automatically becomes a return value of that block. In this case, the ForEach-Object body will emit a series of PSCustomObject instances.

Use the result in any way you like, for example format it as JSON or CSV.

Related reading

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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