结合Powershell脚本调用函数并获取AD属性值

问题描述 投票:-1回答:1

我需要使用PowerShell功能格式化电话号码,如下所示:

Function Format-TelephoneNumber
{
    Param (
        [Parameter(ValueFromPipeline = $true, Position = 0)]
        [Alias('Number')]
        [string]$TelephoneNumber,
        [Parameter(Position = 1)]
        [string]$DefaultCountryCode = '+44'
    )
    Process
    {
        $formattedNumber = $TelephoneNumber -replace '[\x09 ]'
        If ($formattedNumber -match '\A(?<CountryCode>\+[1-9]\d|0)(?<Number>\d*)\Z')
        {
            If ($Matches['CountryCode'] -eq '0')
            {
                $countryCode = $defaultCountryCode
            }
            Else
            {
                $countryCode = $Matches['CountryCode']
            }
            $formattedNumber = $countryCode + ' '
            $formattedNumber += -join $Matches['Number'][0 .. 2] + ' '
            $formattedNumber += -join $Matches['Number'][3 .. 5] + ' '
            $formattedNumber += -join $Matches['Number'][6 .. 8]
            $formattedNumber
        }
        Else
        {
            Write-Error "Unable to parse the string '$($number)' as telephone number!"
        }
    }
}

以下脚本用于从AD属性中检索电话号码的值:

$sysInfo = New-Object -ComObject 'ADSystemInfo'
$userDN = $sysInfo.GetType().InvokeMember('UserName', 'GetProperty', $null, $sysInfo, $null)
$adUser = [ADSI]"LDAP://$($userDN)"
[void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($sysInfo)

Write-Host $adUser.mobile.ToString() -ForegroundColor Green

我该如何调用脚本?

我试过下面但是失败了:

Write-Host "This is raw from AD: $($adUser.mobile.ToString())" -ForegroundColor Yellow

$Formatted = Format-TelephoneNumber -TelephoneNumber $adUser.mobile.ToString()
Write-Host "This is processed using Function: " "$($Formatted)" -ForegroundColor Green
powershell powershell-v4.0
1个回答
1
投票

就个人而言,我使用不同的Format-TelephoneNumber函数,因为正如James C所评论的那样,你的函数可能会从数字中截断最后一位数字。以下是我的尝试:

function Format-TelephoneNumber {
    Param(
        [Parameter(ValueFromPipeline = $true, Position = 0)]
        [Alias('Number')]
        [string]$TelephoneNumber,

        [Parameter(Position = 1)]
        [string]$DefaultCountryCode = '+44'
    )
    Process {
        # replace all hyphens and other possible joining characters with space and trim the result
        $number = ($TelephoneNumber -replace '[._~-]', ' ').Trim()
        # test if the number starts with a country code
        if ($number -match '^(\+\d+)\s') {
            $countryCode = $Matches[1]
            $number = $number.Substring($countryCode.Length).Trim()
        }
        else {
            $countryCode = $DefaultCountryCode
        }

        # remove leading zero and any non-digits
        $number = $number -replace '^0|\D', ''

        if ($number.Length -lt 9) {
            Write-Warning "Unable to parse the string '$($TelephoneNumber)' as telephone number!"
        }
        else {
            $parts = @($countryCode)
            # split the remaining string in to 3-character parts (+ possible remainder)
            $parts += $number -split '(\d{3})' | Where-Object { $_ }
            return $parts -join ' '
        }
    }
}

为什么不使用Get-ADUser cmdlet来查找mobile属性?就像是:

Import-Module ActiveDirectory

# return the mobile phone number for a user as string or nothing if not found
# $userID is either the users distinguished name, the GUID, the user SID, or the SamAccountName.
$mobile = Get-ADUser -Identity $userID -Properties MobilePhone | Select-Object -ExpandProperty MobilePhone

注意:MobilePhonemobile属性的PowerShell或GUI名称,但您也可以使用。

然后,如果您使用Format-TelephoneNumber函数将此移动号码作为字符串格式:

if ($mobile) { 
    Write-Host "This is raw from AD: $mobile" -ForegroundColor Yellow
    $formatted = Format-TelephoneNumber -TelephoneNumber $mobile
    Write-Host "This is formatted: $formatted" -ForegroundColor Green
}

希望这能回答你的问题

© www.soinside.com 2019 - 2024. All rights reserved.