结合Powershell脚本调用函数并获取AD属性值
我需要使用PowerShell功能格式化电话号码,如下所示:结合Powershell脚本调用函数并获取AD属性值,powershell,powershell-4.0,Powershell,Powershell 4.0,我需要使用PowerShell功能格式化电话号码,如下所示: Function Format-TelephoneNumber { Param ( [Parameter(ValueFromPipeline = $true, Position = 0)] [Alias('Number')] [string]$TelephoneNumber, [Parameter(Position = 1)] [string]$D
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!"
}
}
}
如何调用脚本
我尝试了以下内容,但失败了:
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
就我个人而言,我会使用不同格式的电话号码函数,因为正如所评论的,您的函数可能会截断号码的最后一位。 以下是我的尝试:
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
注意:MobilePhone
是mobile
属性的PowerShell或GUI名称,但您可以使用其中一种
然后,如果您将此手机号码设置为字符串格式,请使用格式化电话号码
功能:
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
}
希望这能回答您的问题到底是什么问题?我看不出您如何调用函数有任何问题。您的函数删除电话号码的最后一位是故意的吗?我会将范围更新为
0。。3
/4。。6
/7。。9
由于这不会分解区号,并且包含最后一位数字。@JamesC.no,这不是故意的,因此如何在脚本上更新该数字?@guiwhats结果显示不正确?我需要正确的方法调用函数并将结果赋给变量。
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
}