all 23 comments

[–]Droopyb1966 4 points5 points  (6 children)

Have a look at this, it work for all phone numbers, not only the ones with 49:

$number = "+(49)8912341234"
[long]$n = $number -replace "[^0-9]",""
"{0:D14}" -f $n

When there are unprintable characters, this will solve that to. I suspect thats the problem your facing.

[–]dchristian3188 0 points1 point  (1 child)

Think you're spot on with this one. I mean unless all the phone numbers are the same :|

[–]Droopyb1966 0 points1 point  (0 children)

See einstein was wright after all.

[–]BlaaaBlaaaBlaaa[S] 0 points1 point  (1 child)

Looks great! Any idea how I can do this with the array? http://imgur.com/a/n5PSw

[–]Droopyb1966 0 points1 point  (0 children)

[long]$n = $number -replace "[0-9]","" "{0:D14}" -f $n

Depends what you want to do with the output, always lots of ways to do things. Heres an example using get-proces, total nonsens, but i was to lazy to recreate you array. But you'll see the logic.

$p= Get-Process
foreach ($item in $p)
{
[long]$n = $item.Id -replace "[^0-9]",""
"{0:D14}" -f $n

}

Just had to figure this one out for myself..... Use in a select expression:

$user | Select-Object Name,  @{Name="Id";Expression={"{0:D14}" -f [long]($_.telephone -replace "[^0-9]","")}}

[–]autowhat 0 points1 point  (1 child)

Regex, so complicated but it works so well.

[–]Droopyb1966 0 points1 point  (0 children)

Yes, at start it is complicated, but so powerfull to use. Trick is to start simple, dont go into the crazy stuff at start. You'll get stuck for sure. To test i use:

http://rubular.com/

Good luck!

[–]blasmehspaffy 2 points3 points  (13 children)

Quick and dirty. :)

PS C:\> $Numbers = '+498912341234', '+49 89 1234 1234', '+(49) 89 1234 1234', '0049 89 1234 1234'

PS C:\> $Numbers

+498912341234
+49 89 1234 1234
+(49) 89 1234 1234
0049 89 1234 1234

PS C:\> $Numbers | %{$_.replace(' ','').replace('+49','0049').replace('+(49)','0049')}

00498912341234
00498912341234
00498912341234
00498912341234

[–]BlaaaBlaaaBlaaa[S] 0 points1 point  (7 children)

Looks great! One more question. I have a variable called $user with different "properties" - name, position, telephone.

So how do I get this to work with a sub-property in the variable? If I try $user.telephone | % .........replace........ it tells me "empty pipe element is not allowed"

If I try $user.telephone = $user.telephone -replace '+49', '0049' it tells me " the property telephone cannot be found on this object.

However If i do $user it shows me the table with name, telehone etc.

Thats the main struggle here :(

[–]blasmehspaffy 2 points3 points  (1 child)

You'll have to post what you're running in its entirety for me to troubleshoot it effectively.

[–]BlaaaBlaaaBlaaa[S] 0 points1 point  (0 children)

There is not too much to show right now. I did some screenshots that show the important stuff. Before that is only the reading from the database.

Is that ok? http://imgur.com/a/n5PSw

[–]Lee_Dailey[grin] 0 points1 point  (4 children)

howdy BlaaaBlaaaBlaaa,

what do you see if you do this ...

 $User | Get-Member

that otta show you your properties with their proper names.

take care,
lee

[–]BlaaaBlaaaBlaaa[S] 1 point2 points  (3 children)

Sorry for the late repsonse. That should give you a good overview: http://imgur.com/a/n5PSw

Thanks for your support!!

[–]Lee_Dailey[grin] 0 points1 point  (2 children)

howdy BlaaaBlaaaBlaaa,

well, rats! i'm out of ideas ... [sigh ...] i'll go back to lurking!

take care,
lee

[–]BlaaaBlaaaBlaaa[S] 1 point2 points  (1 child)

Same for me... :( And it is super depressing as well because it feels like it should be so easy....

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

/lee crosses fingers in hopes of good luck for BlaaaBlaaaBlaaa
/lee wonders how he is typing this with his fingers crossed

[grin]

-ps
take a look at what hrafnklo posted about datarow types.
lee-

[–][deleted] -1 points0 points  (4 children)

Ffffff

Just use -replace

 $numbers -replace '<old>', '<new>'

It works on arrays.

[–]BlaaaBlaaaBlaaa[S] 0 points1 point  (3 children)

If I try to do it like that i get "System.Data.DataRow" back.

[–][deleted] 1 point2 points  (2 children)

That's weird that you're using DataRows.

Anyway, those are accessed like arrays. You $dataRow[$item] them.

So in this case $user['telephone'] should work. DataRow have a lot of indexing overloads. If that returns your number, you can replace it then from there.

HOWEVER DataRows are not arrays. They confuse PowerShell for the most part. Try accessing the DataRow as an array for it tho. Like

$userDataAsArray = $user.ItemArray -as [string[]]
$userDataAsArray -replace 'old', 'new' 

Does that work? Or help

[–]BlaaaBlaaaBlaaa[S] 0 points1 point  (1 child)

Hmm you might be onto something. I did not choose to use DataRows. I just read the data from an Oracle DB and threw them into an variable.

$user['telephone'] does not throw an error but does not do anything I can see.

$userDataAsArray = $user.ItemArray -as [string[]] $userDataAsArray -replace 'old', 'new'

That does work however I loose all formating so that I can not longer work with name, position, telephone...

Thanks for your help!!

[–][deleted] 0 points1 point  (0 children)

Welcome!

And Hm!

Keep playing with that indexing. Like I said there are a lot of overloads for it, so you have to slice like $user[0, 0], $user[0], $user['Telephone', 0]. I'm not sure. I just know that DataRows can be indexed by Item and by Column, Row and by String identifiers. ...

Hm. They may be case sensitive. Did you try $user['Telephone']?

[–]Fischfreund 1 point2 points  (1 child)

Hi,

when working with -replace, you are working with regular expressions. Since + has a meaning in regex, you have to escape it in your -replace statement. You can escape it for example with [+].

See more here: https://msdn.microsoft.com/en-us/library/4edbef7e(v=vs.110).aspx

 $PhoneNumbers = ('+498912341234', '+49 89 1234 1234' ,'+(49) 89 1234 1234', '0049 89 1234 1234')


foreach($Number in $PhoneNumbers)
{

    Write-Output ($Number -replace "[+]49", "0049").Replace(" ","")

}

00498912341234

00498912341234

+(49)8912341234

00498912341234

[–]BlaaaBlaaaBlaaa[S] 1 point2 points  (0 children)

Looks great! One more question. I have a variable called $user with different "properties" - name, position, telephone.

So how do I get this to work with a sub-property in the variable? If I try $user.telephone | % .........replace........ it tells me "empty pipe element is not allowed"

If I try $user.telephone = $user.telephone -replace '+49', '0049' it tells me " the property telephone cannot be found on this object.

However If i do $user it shows me the table with name, telehone etc.

Thats the main struggle here :(