3

We are a learning center and sometimes we are not able to sysprep the machines for different reasons. When I do not sysprep the machines, I end up cloning a machine 25 times with the same computername.

I then have to go manually on each station and change the computername and restart them after.

I was wondering if I could use a batch file or powershell script that I would do before shutting down my machine (pre-cloning). Then, on next reboot (only once) the computer would randomly change the Computername and therefore save me alot of time.

I am doing this under Windows XP to Windows Server 2012R2. A unique solution working under all those OSes would be magic but I mostly do this on Server 2008+. I dont mind using a batch file for WinXP-Win7 and powershell for Windows 2008 to 2012 for example!

Thank you everyone!

Menkadelic
  • 79
  • 2
  • 7
  • It might be helpful to look here first -- http://stackoverflow.com/questions/54989/change-windows-hostname-from-command-line – JustSomeQuickGuy Feb 10 '14 at 14:58
  • The last topic did not covered the randomization of the hostname nor how to add this to the runonce. I also posted an update on this topic. Thank you for that. I now have part of my answer with this topic – Menkadelic Feb 10 '14 at 15:23
  • 1
    Why are you not able to use sysprep? Perhaps we can help you solve that problem too. If you are going to use any kind of Active Directory having cloned machines that did not have sysprep run on them [will give you problems](http://serverfault.com/questions/360863/domain-user-invisable-on-local-remote-desktop-users-group). – Scott Chamberlain Feb 10 '14 at 19:46
  • When I use sysprep sometimes (specially on win7) i get "Fatal error occured" and nothing works, nut sure how to troubleshoot this – Menkadelic Feb 11 '14 at 14:06

2 Answers2

4

You can generate a random name using the Get-Random cmdlet.

# Set allowed ASCII character codes to Uppercase letters (65..90), 
$charcodes = 65..90

# Convert allowed character codes to characters
$allowedChars = $charcodes | ForEach-Object { [char][byte]$_ }

$LengthOfName = 10
# Generate computer name
$pw = ($allowedChars | Get-Random -Count $LengthOfName) -join ""

You can change a computer name with the cmdlet Rename-Computer. And to set it to run once, the easiest way would be to add an entry to the registry key

HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce   

that will invoke PowerShell with your script.

KevinD
  • 3,023
  • 2
  • 22
  • 26
  • 1
    Thank you ! I will test this by the end of the week. So im good with making the script in a file, to allow it to run. Im not sure what to enter in the reg key so that it will call the script when the computer boots. I just put the path to the script and script name? – Menkadelic Feb 11 '14 at 14:07
  • 1
    No, you need to invoke powershell, with the script as an argument. Something like powershell -ExecutionPolicy ByPass -File C:\path\to\script.ps1. – KevinD Feb 11 '14 at 15:29
  • 1
    I don't think you need it for this, but if it turns out the script needs admin privileges, add -Verb runas to the end of reg key. – KevinD Feb 11 '14 at 15:35
2

I'd like to add two ways to generate names with all allowed chars, for convenience starting with an uppercase letter and a length of 15 chars

Batch

@Echo off&SetLocal EnableExtensions EnableDelayedExpansion
Set "Chars=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
Call :RandChar 26 Name
For /L %%A in (1,1,14) Do Call :RandChar 62 Name
Echo %Name%
Goto :Eof
:RandChar Range Var
Set /A Pnt=%Random% %% %1 & Set %2=!%2!!Chars:~%Pnt%,1!

PowerShell

$chars = [char[]]"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
[string](($chars[0..25]|Get-Random)+(($chars|Get-Random -Count 14) -join ""))