widsnet.com
11Jul/120

Get-ManagedBy and Get-ManagedObjects

Who owns a computer?
Which computers do someone own?
These questions need to be answered sometimes. And the following Powershell script gets that information from AD.

function Get-ManagedBy {
    Param(
        [parameter(Mandatory = $true)][string]$Computer
    )
    Add-Type -AssemblyName System.DirectoryServices.AccountManagement
    $ct = [System.DirectoryServices.AccountManagement.ContextType]::Domain
    $ComputerObject = [System.DirectoryServices.AccountManagement.Principal]::FindByIdentity($ct,$Computer)
    Return [System.DirectoryServices.AccountManagement.Principal]::FindByIdentity($ct,($ComputerObject.GetUnderlyingObject().managedBy))
}

function Get-ManagedObjects {
    Param(
        [parameter(Mandatory = $true)][string]$User
    )
    Add-Type -AssemblyName System.DirectoryServices.AccountManagement
    $ct = [System.DirectoryServices.AccountManagement.ContextType]::Domain
    $UserObject = [System.DirectoryServices.AccountManagement.Principal]::FindByIdentity($ct,$User)
    Return $UserObject.GetUnderlyingObject().managedObjects
}
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

No trackbacks yet.