sameer-kumar / adhoc-posh

My adhoc powershell scripts and modules contributions for the community
MIT License
2 stars 1 forks source link

OU.TFS.Administration references non-existent Get-SidByName #1

Open sztaylorakgov opened 8 years ago

sztaylorakgov commented 8 years ago

Tried using these TFS PS modules. Work pretty well to create a Team in TFS, but trying to add members to the team fails b/c there is no definition for Get-SidByName.

sameer-kumar commented 8 years ago

Thanks for pointing it out. I need to add a helper module. Will do this weekend. Meanwhile you can inject these functions as a temporary fix. HTH

function Get-NameBySid {
    #region Parameters
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=1)]
        [string]
        $Sid,

        [Parameter(Mandatory=$false, ValueFromPipeline=$true, Position=2)]
        [bool]
        $TruncateDomainName = $false
    )
    #endregion Parameters
    Begin {}
    Process {
        try {
            $objSID = New-Object System.Security.Principal.SecurityIdentifier -ArgumentList "$Sid"
            $objUser = $objSID.Translate( [System.Security.Principal.NTAccount])
            $displayName = $objUser.Value
            if($displayName.Contains("\") -and $TruncateDomainName -eq $true) {
                # remove domain name if exists
                $displayName = $displayName.Substring($displayName.IndexOf('\')+1)
            }

            return $displayName
        }
        catch {
            Write-Output "Exception: " + $_
        }
    }
    End {}
}

function Get-SidByName {
    #region Parameters
    [CmdletBinding()]
    param (
        [Parameter(ValueFromPipeline=$true, Position=1)]
        [string]
        $DomainName = "IAM",

        [Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=2)]
        [string]
        $AccountName
    )
    #endregion Parameters
    Begin {}
    Process {
        try {
            $remoteUserIdentity = New-Object System.Security.Principal.NTAccount -ArgumentList "$DomainName", "$AccountName"
            $remoteUserSID = $RemoteUserIdentity.Translate([System.Security.Principal.SecurityIdentifier])
            return $remoteUserSID.Value
        }
        catch {
            Write-Output "Exception: " + $_
        }
    }
    End {}
}