Jump to content
Sign in to follow this  
Rav1

Help converting vb script to autoit

Recommended Posts

Rav1

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

Share this post


Link to post
Share on other sites
Rav1

Yeah - My bad. Sorry.

$oReturn = $objComputer.rename($strNewName)
$oReturn = $objComputer^ ERROR

Share this post


Link to post
Share on other sites
trancexx

You need to add COM error handler to survive that error and to get extended info about it. Look for ObjEvent function.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites
AutID

The script should run flawlessly. It is badly formatted but it should run the way it is. Try elevating your program(run it as admin).
If that doesn't work then you are messing up the strings.

P.S. your select case sucks. I don't even know what you think these lines
Case "Opt"
Case "Pre"

do...

Share this post


Link to post
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
Sign in to follow this  

×