Jump to content

Help converting vb script to autoit


Recommended Posts

Hey gang! I have an issue that maybe someone can help me with (I'm still learining).  I have a vb script that I am trying to convert to autoit.  I got rid of all the errors and I thought things were going swimmingly but alas I am coming up with an error.  The script should change the computer name to the name and model of the pc\laptop.

;Set Computer name based on BIOS Service Tag and computer model type
;Format is: ssssssst-model
;     where: sssssss = Dell Service Tag from BIOS
;            t = System type: L = Laptop, D = Desktop/Tower
;            model = Dell model code (i.e. 7010, T4700, E6330, etc)

Dim $strComputer, $objWMIService,  $colmodel, $objitem, $strServiceTag, $strModelDesc, $strModelCode, $strModelClass, $numFirstPos, $numLastPos, $strModelType, $strNewName, $numLen

;Retrieve service tag from bios
$strComputer = "."
$objWMIService = ObjGet("winmgmts:" & "{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")
$colBIOS = $objWMIService.ExecQuery("Select * from Win32_BIOS")
For $objBIOS in $colBIOS
$strServiceTag = StringTrimLeft($objBIOS.SerialNumber,1)
Next

; Retrieve hardware model
If @error Then
   ;handle it
EndIf
$colmodel = $objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem")

For $objitem in $colmodel
     $strModelDesc = StringTrimLeft($objitem.Model,9)
    ;locate model name as second item within model description
     $numFirstPos = StringInStr(1, $strModelDesc, " ", 1) + 1
     $numLastPos = stringInStr($numFirstPos, $strModelDesc, " ", 1) -1
     If $numLastPos <= 0 Then
        $numLastPos = StringLen($strModelDesc)
     EndIf
     $numLen = $numLastPos - $numFirstPos + 1
     $strModelCode = StringMid($strModelDesc, $numFirstPos, $numLen)    ;Model code: E6320, T3600, 790, 7010, etc
     $strModelShort = stringMid($strModelDesc,1, 3)     ;First 3 characters of model name
     $numLen = StringLen($strModelCode)

     Select
         case $strModelShort
             Case "Lat"
               $strNewName = $strServiceTag & "L-" & $strModelCode
          Case "Opt"
               If $numLen <= 3 Then
                    $strModelType = "OPT"
               Else
                    $strModelType = "OP"
               EndIf
               If stringMid($strModelCode, 1, 1) >= "A" AND stringMid($strModelCode, 1, 1) <= "Z" Then
                    $strNewName = $strServiceTag & "D-" & $strModelCode
               Else
                    $strNewName = $strServiceTag & "D-" & $strModelType & $strModelCode
               EndIf
          Case "Pre"
               If stringMid($strModelCode, 1, 1) = "M" Then
                    $strNewName = $strServiceTag & "L-" & $strModelCode
               Else
                    $strNewName = $strServiceTag & "D-" & $strModelCode
               EndIf
     EndSelect
Next
;Limit computer name to a maximum of 15 characters
$strNewName = StringLeft($strNewName, 15)

;Set new computer name
$colComputers = $objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
For $objComputer in $colComputers
     $oReturn = $objComputer.rename($strNewName)
Next
;Reboot system
$objWMIService = Objget("winmgmts:" & "{impersonationLevel=impersonate,(Shutdown)}!\\" & $strComputer & "\root\cimv2")
$colOperatingSystems = $objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem")
For $objOperatingSystem in $colOperatingSystems
     $objOperatingSystem.Reboot()
Next

It's not working and comes up with an error with this line 

$oReturn = $objComputer.rename($strNewName)

I've attached the original vbs script. Can anyone point me in the right direction? I've never tried to migrate a script to autoit before.

 

Thoughts?

rename.vbs

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...