title: VBScript Renaming Windows Computer with Options
body: <p>You guys did great helping me with one VBScript, so I'm going to throw another your way. Perhaps you can help me iron out the wrinkles.</p>
  
  <p>My intention with this script is to rename the PC. The computer name is a combination of prompted text (something I provide, say a location code), a hyphen, and the last 10 digits of the computer's serial number called from WMIC. i.e. 1275-XXXXXXXXXX</p>
  
  <p>My problems here are the following:</p>
  
  <ol>
  <li>If my code is over 10 characters, it just errors out. I want to fix
  that. I'm sure it's just the way I have it coded and nothing to do
  with pulling from WMIC.</li>
  <li>If it does error out pulling the serial number from WMIC, say because there's no value, I want to be prompted to enter something in it's place. Then at the end it will take Input1 (the location code) and Input2 (what I provide if the SN pull fails), smack a hyphen in the middle, and apply it.</li>
  <li>My error out isn't working if it does fail. I don't know why.</li>
  </ol>
  
  <p>I've found so many different solutions for renaming PC's either what I type in or specifically for pulling the SN, but not for my specific situation. Any help would be GREATLY appreciated, as always. :)</p>
  
  <p>Here's my code:</p>
  
  <pre><code>'Rename computer by serial # v1.0 November 2009
  dim Bios, BiosSerial, objFSO, objTextFile
  'Const ForReading = 1, ForWriting = 2, ForAppending = 8
  
  'get PropertyID
  strInput = UserInput( "Enter the BHMS Property ID:" )
  
  Function UserInput( myPrompt )
  ' This function prompts the user for some input.
  ' When the script runs in CSCRIPT.EXE, StdIn is used,
  ' otherwise the VBScript InputBox( ) function is used.
  ' myPrompt is the the text used to prompt the user for input.
  ' The function returns the input typed either on StdIn or in InputBox( ).
  ' Written by Rob van der Woude
  ' http://www.robvanderwoude.com
  ' Check if the script runs in CSCRIPT.EXE
  If UCase( Right( WScript.FullName, 12 ) ) = "\CSCRIPT.EXE" Then
      ' If so, use StdIn and StdOut
      WScript.StdOut.Write myPrompt &amp; " "
      UserInput = WScript.StdIn.ReadLine
  Else
      ' If not, use InputBox( )
      UserInput = InputBox( myPrompt )
  End If
  End Function
  
  'Obtain Serial Number.
  for each Bios in GetObject("winmgmts:").InstancesOf ("win32_bios")
  BiosSerial = Bios.SerialNumber
  exit for
  next
  
  strNewSN = BiosSerial
  
  ' If the SN is longer than 10 characters, truncate to the last 10.
  If Len(strNewSN) &lt; 9 Then
      strNewSN = Right(BiosSerial, 10)
      strNewPCName = strInput+"-"+strNewSN
  End If
  
  Set WshNetwork = WScript.CreateObject("WScript.Network")
  
  strComputer = "."
  Set objWMIService = GetObject("winmgmts:" _
  &amp; "{impersonationLevel=impersonate}!\\" &amp; strComputer &amp; "\root\cimv2")
  
  Set colComputers = objWMIService.ExecQuery _
  ("Select * from Win32_ComputerSystem")
  
  For Each objComputer in colComputers
  err = objComputer.Rename(strNewPCName)
  
  if err &lt;&gt; 0 then
  wscript.echo "There was an error renaming the PC. Please restart and try again, or rename it manually."
  else
  wscript.echo "PC successfully renamed: " &amp; strNewPCName
  end if
  
  Next
  </code></pre>
  
  <p><strong>5/29/2013 EDIT: I've made some changes based on your suggestion, and I'm getting on error on line 36 char 1 "Expected Statement" Code 800A0400. It looks fine to me so what am I missing? Here's a new paste of my code with line 36 notated.</strong></p>
  
  <pre><code>dim Bios, BiosSerial, objFSO, objTextFile
  'Const ForReading = 1, ForWriting = 2, ForAppending = 8
  
  ' Prompt for PropertyID
  strInput = UserInput( "Enter the BHMS Property ID:" )
  
  Function UserInput( myPrompt )
      ' Check if the script runs in CSCRIPT.EXE
      If UCase( Right( WScript.FullName, 12 ) ) = "\CSCRIPT.EXE" Then
          ' If so, use StdIn and StdOut
          WScript.StdOut.Write myPrompt &amp; " "
          UserInput = WScript.StdIn.ReadLine
      Else
          ' If not, use InputBox( )
          UserInput = InputBox( myPrompt )
      End If
  End Function
  
  ' Obtain Serial Number.
  for each Bios in GetObject("winmgmts:").InstancesOf ("win32_bios")
  BiosSerial = Bios.SerialNumber
  exit for
  next
  
  strNewSN = BiosSerial
  
  If IsEmpty(BiosSerial) Then
    strNewSN = UserInput("There is no serial number listed in the BIOS. Provide an alternative: ")
  Else
    strNewSN = BiosSerial
  End If
  
  ' If the SN is longer than 10 characters, truncate to the last 10.
  If Len(strNewSN) &gt; 10 Then strNewSN = Right(BiosSerial, 10)
  strNewPCName = strInput &amp; "-" &amp; strNewSN
  End If                                                           'LINE36'
  
  Set WshNetwork = WScript.CreateObject("WScript.Network")
  
  strComputer = "."
  Set objWMIService = GetObject("winmgmts:" _
  &amp; "{impersonationLevel=impersonate}!\\" &amp; strComputer &amp; "\root\cimv2")
  
  Set colComputers = objWMIService.ExecQuery _
  ("Select * from Win32_ComputerSystem")
  
  For Each objComputer in colComputers
  err = objComputer.Rename(strNewPCName)
  
  On Error &lt;&gt; 0 Then
  wscript.echo "There was an error renaming the PC. Please restart and try again, or rename it manually."
  Else
  wscript.echo "PC successfully renamed: " &amp; strNewPCName
  end if
  
      Next
  </code></pre>