S
S
Some Guy2017-09-13 11:55:30
PowerShell
Some Guy, 2017-09-13 11:55:30

Script not working in Powershell?

The script works only if the fields homephone and mobile have phones, but if at least one field is empty, then it does not work, can you somehow correct the regular expression or is it better to add if, else conditions?
Get-ADUser -SearchBase "DC=mydomain,DC=local" -Filter * -properties homephone, mobile | Foreach {
# ALL
$homephone = $_.homephone -replace "\D"
$mobile = $_.mobile -replace "\D"
# RU
$mobile = $mobile -replace '^8(\D{10})$ ','7$1'
$homephone = $homephone -replace '^8(\D{10})$','7$1'
Set-ADUser $_ -mobilephone $mobile -homephone $homephone
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alina Andreeva, 2017-09-14
@linacat2305

try -properties * instead of -properties homephone, mobile

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question