I am trying to obtain the machine name from the user name which makes up part of the machine name, using PowerShell and Active Directory. Our PC naming convention is like this: XXXZZZ01BLOGGSJ where the last characters after the last number is the username of the PC owner.
I know that to derive the user name from the PC name using REGEX, I can do this:
"XXXZZZ01BLOGGSJ" -replace '\D*\d*(\w*)', '$1'
(result is BLOGGSJ)
What I need to be able to do is derive the computer name from the username via Active directory and have tried this and various version of this:
"BloggsJ" | % { Get-ADComputer -Server blahblah.com -Filter {name -like "xxxzzz*"} | ? "$_.Name -replace '\D*\d*(\w*)', '$1' -eq '$_'" } | select Name
This fails with no result and I am struggling to find the correct method of doing this, any help please?