FN-GM Posted September 16, 2019 Posted September 16, 2019 Hello, This will be an advance script so there is a chance I won't be successful, but you never know.... I was wondering if anyone can help with a script I want to create. Currently our Windows computers are named after their asset numbers (and a few other inconsistent random names!). This makes it difficult to determine from a name as to where of our 20 sites a computer is based. I am looking to rename the computers based on the below system. - eg: BAL-001 or PAN-001 Each site has its own IP range so the code to use could be determined from that. The device number will be the next free number available in AD Better still if we could stretch it so I can differentiate between desktops and laptops even better. eg: BAL-PC001 or PAN-LTOP001 Any help would be gratefully received. Thanks
FishCustard Posted September 16, 2019 Posted September 16, 2019 I'm not sure that you can script renames of domain PCs, as I seem to recall it requiring two restarts to achieve (one to remove from domain, one to rename and put back). However, it may be that things have moved on since the last time I looked into this!
FN-GM Posted September 16, 2019 Author Posted September 16, 2019 I'm not sure that you can script renames of domain PCs, as I seem to recall it requiring two restarts to achieve (one to remove from domain, one to rename and put back). However, it may be that things have moved on since the last time I looked into this! You don't need to remove from the domain to rename. I used to work at a bank that had a script to auto rename PCs. Better still if we could stretch it so I can differentiate between desktops and laptops even better. eg: BAL-PC001 or PAN-LTOP001 Just thinking about this. I can have 2 scripts. One for the desktop naming convention and one for laptops. I could use SCCM to target the correct devices. As a result I won't need to have this function in the script. So the easier option will be good. Thanks
stevec_ Posted September 16, 2019 Posted September 16, 2019 Not tested but you could try the following, just modify the IP_Ranges table as required $IP_Ranges = @{ "10.30.*.*"="PAL-"; "10.10.10.*"="BAL-" } $My_IP = ( Get-NetIPConfiguration | Where-Object { $_.IPv4DefaultGateway -ne $null -and $_.NetAdapter.Status -ne "Disconnected" } ).IPV4Address.IPAddress $IP_Ranges.Keys | ForEach-Object { if ($My_IP -like $_) { $i = 1 while ($true) { $PC_Name = "$($IP_Ranges[$_]){0:d3}" -f $i if (@(Get-ADComputer $PC_Name -ErrorAction SilentlyContinue).Count) { $i++ continue } Rename-Computer -NewName "$($PC_Name)" -Force -Restart break } } } 1
HPlum78 Posted September 16, 2019 Posted September 16, 2019 I think @stevec_ has got it, i was trying to work out a way of doing it without the need for the AD module having to be on every computer.... lots of ideas but all need other things to be done. Need to think about if you care about having the modules on every computer in your environment or not i suppose. If its going to be a show stopper then would have to look at other options.
FN-GM Posted September 17, 2019 Author Posted September 17, 2019 Not tested but you could try the following, just modify the IP_Ranges table as required $IP_Ranges = @{ "10.30.*.*"="PAL-"; "10.10.10.*"="BAL-" } $My_IP = ( Get-NetIPConfiguration | Where-Object { $_.IPv4DefaultGateway -ne $null -and $_.NetAdapter.Status -ne "Disconnected" } ).IPV4Address.IPAddress $IP_Ranges.Keys | ForEach-Object { if ($My_IP -like $_) { $i = 1 while ($true) { $PC_Name = "$($IP_Ranges[$_]){0:d3}" -f $i if (@(Get-ADComputer $PC_Name -ErrorAction SilentlyContinue).Count) { $i++ continue } Rename-Computer -NewName "$($PC_Name)" -Force -Restart break } } } Thanks for the help - I will give this a try.
FN-GM Posted October 16, 2019 Author Posted October 16, 2019 Not tested but you could try the following, just modify the IP_Ranges table as required $IP_Ranges = @{ "10.30.*.*"="PAL-"; "10.10.10.*"="BAL-" } $My_IP = ( Get-NetIPConfiguration | Where-Object { $_.IPv4DefaultGateway -ne $null -and $_.NetAdapter.Status -ne "Disconnected" } ).IPV4Address.IPAddress $IP_Ranges.Keys | ForEach-Object { if ($My_IP -like $_) { $i = 1 while ($true) { $PC_Name = "$($IP_Ranges[$_]){0:d3}" -f $i if (@(Get-ADComputer $PC_Name -ErrorAction SilentlyContinue).Count) { $i++ continue } Rename-Computer -NewName "$($PC_Name)" -Force -Restart break } } } I am picking this back up after some other stuff jumped the queue. I was wondering if you know how it could be done via subnets instead please? It will make it easier with VLANS.
fordea Posted October 18, 2019 Posted October 18, 2019 There's a nice function here that checks if an IP address is within a subnet using CIDR notation: https://github.com/omniomi/PSMailTools/blob/v0.2.0/src/Private/spf/IPInRange.ps1. So if you plugged that function into the example by @stevec_ you would need to alter the $IP_Ranges hash table to contain subnets using CIDR notation: $IP_Ranges = @{ "10.30.1.0/24"="PAL-"; "10.10.10.0/24"="BAL-" } And then you'd just need change the check in the loop of the hash table to: if (IPInRange $My_IP $_) { ... } 1
dapaulio Posted October 20, 2019 Posted October 20, 2019 I have remotely renamed domain computers using the following ps cmd Rename-computer -computername “name” -newname “newname” -domaincredential username password -force -restart I think with a few other suggestions you can make a script that would automate the whole process injecting a csv maybe. Hope it helps
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now