Rav1 Posted July 14, 2015 Share Posted July 14, 2015 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.expandcollapse popup;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() NextIt'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 More sharing options...
JohnOne Posted July 14, 2015 Share Posted July 14, 2015 The error string would come in handy. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
Rav1 Posted July 14, 2015 Author Share Posted July 14, 2015 Yeah - My bad. Sorry.$oReturn = $objComputer.rename($strNewName)$oReturn = $objComputer^ ERROR Link to comment Share on other sites More sharing options...
trancexx Posted July 14, 2015 Share Posted July 14, 2015 You need to add COM error handler to survive that error and to get extended info about it. Look for ObjEvent function. ♡♡♡ . eMyvnE Link to comment Share on other sites More sharing options...
JohnOne Posted July 14, 2015 Share Posted July 14, 2015 There is usually more to it than that.See COM Error Handling in COM reference in help file. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
AutID Posted July 14, 2015 Share Posted July 14, 2015 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 linesCase "Opt"Case "Pre"do... https://iblockify.wordpress.com/ Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now