Skip to content

Instantly share code, notes, and snippets.

@ned1313
Created September 7, 2016 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ned1313/1ef2612ca8313d5d49f33a594bb873d9 to your computer and use it in GitHub Desktop.
Save ned1313/1ef2612ca8313d5d49f33a594bb873d9 to your computer and use it in GitHub Desktop.
param(
[string] $ServerPrefix,
[pscredential] $credentials,
[string] $DCIpAddress
)
Write-Output "ServerPrefix: $ServerPrefix"
$names = Invoke-Command -ComputerName $DCIpAddress -ScriptBlock {param($searchString) Get-ADComputer -Filter * | Where{$_.Name -like "$($searchString)*"} | select Name} -Credential $credentials -ArgumentList $ServerPrefix
Write-Output "Names found: $names"
if($names -eq $null){$hostname = $ServerPrefix + "-01"}
else{
$last = ($names | Sort-Object -Property Name -Descending)[0].Name
[int] $num = $last.Substring($last.Length-2)
$num++
if($num -lt "10"){$hostname = $last.Substring(0,$last.Length-2) + "0" + [string]$num}
else {$hostname = $last.Substring(0,$last.Length-2) + [string]($num)}
}
Write-Output "Hostname set to: $hostname"
Rename-Computer -NewName $hostname -Restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment