在Powershell中从Active Directory修改电话号码变量

时间:2018-12-12 04:23:32

标签: powershell outlook active-directory

我们有一个Powershell脚本,可以使用从Active Directory中提取的数据自动创建Outlook签名。

当前,我们通过以下行提取电话号码:

Update-Sig -attribute "TelephoneNumber" -value "$([string]($ADUser.TelephoneNumber))"

这将返回格式为+61112345678的数字。

我想做的就是更改该字符串,使其始终为+61 1 1234 5678格式,并带有空格。

这可能吗?我想我需要将字符串转换为变量,然后在一定数量的字符后添加一个空格。.但是我不确定该怎么做。

谢谢

2 个答案:

答案 0 :(得分:2)

您可以使用字符串格式运算符来执行此操作。它具有“数字占位符”选项。但是,这仅适用于实际的 numbers ,并且您的数据几乎可以肯定是字符串。所以...我将数字部分转换为<com.shobhitpuri.custombuttons.GoogleSignInButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="@string/google_sign_up" app:isDarkTheme="true" /> 。 [咧嘴]

[int64]

output = $SourceValue = '+61112345678' '+{0:## # #### ####}' -f ([int64]$SourceValue.TrimStart('+'))

答案 1 :(得分:0)

由于字符串格式,此功劳应转到@Lee_Daily。 您可以执行以下操作来格式化期望的“ +61”数字,如果属性中还有其他内容,它将打印出来,以便您进行检查。

$phone = '+61112345678'

# test if the string starts with a `+` or at least the digits `61`
if ($phone.Trim() -match '^(\+|61)') {
    $phone = '+{0:## # #### ####}' -f ([int64]($phone -replace '\D',''))
    Write-Host "Formatted TelephoneNumber: '$phone'" -ForegroundColor Green
}
else {
    # not a '+61' number. Write it out so you can check/correct manually
    Write-Host "Unexpected TelephoneNumber: '$phone'" -ForegroundColor Red
}


更新

实际上,要回答您的最新评论,上面的代码未在ADUser的属性中设置新设置的电话号码。
我认为问题只在于格式,而不是设置。

尽管您的链接已损坏,但在广告中轻松设置它很容易。
看来您已经有一个名为$ADUser的对象,并且我想它来自您之前进行的Get-ADUser调用。 设置使用上面的代码获得的格式化号码,您可以使用以下示例简单地更新用户号码:

# using piping the user object to Set-ADUser
$ADUser | Set-ADUser -OfficePhone $phone

# using one of the users properties as 'Identity' and update using the LDAP attribute name
# for parameter Identity, the cmdlet accepts either the DistinguishedName, objectGUID, objectSid, or the SamAccountName
Set-ADUser -Identity $ADUser.DistinguishedName -Replace @{telephoneNumber = $phone }

p.s。还有一个名为HomePhone(LDAP名称为homePhone)的属性,但这不是您在代码中使用的属性。

希望有帮助